-
Notifications
You must be signed in to change notification settings - Fork 18
Filters
The most common activity in a minq query is filtering: winnowing a stream down to a more precisely targeted selection on of objects.
There are a couple of distinct types of filtering.
The only()
function of a stream filters by Maya node types. For example
Scene().only(Meshes)
will yield only mesh nodes: it's equivalent to cmds.ls(type=
mesh`)
where
accepts a one-argument callable as an argument. It will call that function on every element in the stream, passing only those values which return a True or truthy value for the filter function. For example:
def has_p(name):
return 'p' in name
print Cameras().get(Parents).where(has_p)
# Stream([u'|persp', u'|top'])
The function can do anything it wants -- the only requirement is that it accepts a single argument. For convenience you can use where_not()
to negate a filter function:
print Cameras().get(Parents).where_not(has_p)
# Stream([u'|front', u'|side'])
For the common case of querying maya node attributes, there's a special syntax that allows you to avoid writing a function just to call and test getAttr
. The minq keyword item
is actually a query factory which creates getAttr
tests for you, so you can write
Cameras().where(item.orthographic == True)
# Result: Stream([u'|front|frontShape', u'|side|sideShape', u'|top|topShape']) #
instead of
def is_ortho(cam):
return cmds.getAttr(cam + ".orthographic")
Cameras().where(is_ortho)
Using item
is also faster than a getAttr
filter function as well: see Atribute Queries for more details.
like()
is used to filter object names as strings. In its simplest form it simply does a case insensitive partial match: if you pass in a sub-string as an argument, it will match any occurrence:
Cameras().like('r')
# Result: Stream([u'|front|frontShape', u'|persp|perspShape']) #