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

One big advantage of using streams, compared to looping over individual values and calling function on them one at at time, is that you can bundle up and dispatch a large number of queries at once. For example, if you wanted to collect the translateX of a list of nodes, you'd usually loop over the list, call cmds.getAttr() on each node, and store the results. This would mean as many getAttr()s as you had node. In contrast the minq equivalent:

my_list_of_stuff.get(Attribute, 'tx').get(Values)

will only call getAttr once: it will retrieve all of the values in a single call (this works with cmds.getAttr() too - it's just cumbersome to set up and code inside a loop). For large scenes this can be as much as 40% faster than the looped alternative.

Since so many query operations fall into this pattern, it's minq provides a utility class for creating these kinds of bulk attribute calls and using them as filters without extra operations. The class minq.item (note the casing!) is a query factory; if you pass it to a where() clause it will create a stream-based query for you automatically:

Meshes().get(Parents).where(item.tx > 50)

will return the parents of any mesh where the parent node has a translateX larger than 50. Under the hood item.tx > 50 is evaluated to produce an anonymous callable function: in fact, you can use it outside of the where clause:

test = item.tx > 50
print test('pCube1')
# False

More importantly, using the item class to generate this kind of query tells minq to bundle up all of the getAttr commands in a single call, rather than issuing them one at a time. This significantly speeds up large queries. When in doubt, of course, you can still use a regular function, the item syntax is intended as a convenience for common cases and not the answer to all possible problems.

Clone this wiki locally