Skip to content
theodox edited this page Feb 28, 2016 · 17 revisions

class Stream(builtin.object) Represents an arbitrary stream of values. Usually -- but not always -- this is a stream of Maya node names. Usually -- but not always -- this stream is a 'stored query', like a SQL statment: it's not a list of saved values but rather a chain of operations. This allows you to manipulate queries themselves, rather than their contents

Streams can be combined using set-like operators:

stream1 + stream2

returns a new stream containing the combined contents of both streams (duplicate values are removed)

stream1 - stream2

returns items that are only present in stream1 and not stream2

stream1 & stream2

returns only items that are common to both streams.

Most streams are created by a NodeType-derived class: for example, Meshes() will yield all of the poly meshes in the scene as a Stream, while Transforms() will yield all the transforms. However you can 'initialize' a stream with a list or tuple if you already know the names of nodes or objects you're looking for: for example,

Cameras()

and

Stream(['topShape', 'frontShape', 'perspShape', 'sideShape])

are functionally interchangeable in a typical maya scene.

methods

Every stream offers the following methods:

###append(*args, **kwargs)

###cache(self) returns a Stream wrapped around a the cached result of this query.

###distinct(self) Returns a new stream which contains no duplicate elements. If the incoming stream contains items that can't be hashed, will raise an exception

###execute(self) evaluate all of the queries in this Stream and return the result as a list

###first(self) return the first item in this stream as a single value (not a stream)

###flatten(self) turns a stream containing multiple iterables (for example, the result of a get()) into a single stream.

###foreach(self, func) Returns a stream containing the results of applied to everything in the stream

###get(self, *args, **kwargs) Returns a new stream which applies a minq.core.Projection class to this stream. Projections are operations which replace the incoming stream with a new stream (rather than subsetting the original stream). For example:

   transforms.get(Children)

returns a streams containing the children of the stream .

###having(self, attrib) Returns a stream of objects with the supplied attribute

###join(self, **streams) given a collection of named streams, return a table-like stream in which each 'row' has named values corresponding to the original stream. This is used for coordinating multiple streams: for example:

    bones = Joints()
    bone_parents = bones.get(Parents)
    with_parents = bones.join(parent = bone_parents)

will produce a list like

   # stream: [dataRow(object=u'joint3', parent=u'joint2'), dataRow(object=u'joint2', parent=u'joint1')....]

the join process doesn't make any effort to match up the contents, however -- if the different streams have different lengths enmpy 'columns' in the row will be filled with None and if the streams don't match up the results are not predictable.

The primary use for join() is for doing bulk queries using Attributes:

   bones = Joints().like('l_')
   tx = bones.get(Attribute, 'tx').get(Values)
   ty = bones.get(Attribute, 'ty').get(Values)
   bone_translations = bones.join(x = tx, y = ty)
   print bone_translations

  # stream: [dataRow(object=u'L_Ankle', x=-5.0, y=-1.39), dataRow(object=u'L_Heel', x=-0.028, y =6.18)....]

Note in this specific case it would be faster to use .get(Attribute, 't') rather than querying .tx and .ty separately -- but this pattern works for any arbitrary combination of attributes as long as all the objects in the stream have the attribute. In this form the query is issued to maya only once per stream, which is a big advantage over individually calling getAttr many times over.

###like(self, regex, exact=False) given a string regular expression, returns a new stream containing only items where the regex finds a match. By default this allows subset metches using re.search, but if the optional 'exact' flag is set to True, the matches must be complete using re.match

If the incoming stream contains items that can't be converted to strings, will raise an exception

###long(self) Returns a new stream containing the long names of items in this stream. Any items which are not maya nodes in the stream will be filtered out.

###only(self, *pred, **kwargs) Returns a new stream containing only items of the supplied types. The types can be string typenames (such as 'transforms' or 'shapes') or they can be minq.core.NodeType classes. So

   this_stream.only('transform')

and

   this_stream.only(Transform)

are equivalent.

A handful of NodeTypes (such as ObjectsOnly or Assemblies) can't be combined with others in a single call:

if the optional namespace keyword is passed, this limits the stream to a given namespace

###short(self) Returns a new stream containing the short names of items in this stream. Any items which are not maya nodes in the stream will be filtered out.

###sort(self, key) returns a Stream which sorts the incoming stream using the default Python sort. If the optional key function is provided, the result will be sorted with that key.

This operation has to exhaust the incoming stream, so it is more memory and performance intensive than other minq operations.

###split(self, number_of_streams) Returns multiple 'copies' of this stream which can be iterated independently without re-calling the query.

###uuid(self) Returns a new stream containing the uuids of items in this stream. Any items which are not maya nodes in the stream will be filtered out.

###where(self, pred) given a callable filter function, returns a new stream containing only items for which the filter function return a truth-tested value

###where_not(self, pred) a convenience wrapper for 'where' that inverts the result -- useful for not having to make a lambda just to invert a common function

Clone this wiki locally