Tech Art Bites: Search your Maya scene with itemFilter() and lsThroughFilter()
itemFilter and lsThroughFilter are used together to look for specific things in your scene.
As the name suggests, itemFilter() creates a filter object defined by the user. It has useful flags that allow you to create filters by name, object type, etc.
The lsThroughFilter() command returns a list of the objects that passed the filter.
# This filter identifies all spotlight objects spotLights = cmds.itemFilter(byType='spotLight') lightObjs = cmds.lsThroughFilter(spotLights) # Find the objects doSomething(lightObjs) # Do something with these objects # This filter identifies all transform objects transforms = cmds.itemFilter(byType='transform') transfObjs = cmds.lsThroughFilter(transforms) # Find the objects doSomething(transfObjs) # Do something with these objects
# Both lines do the same thing; create a filter that identifies transforms AND spotlights # This one unites the previous itemFilters with the -union flag unionFilter = cmds.itemFilter(union=(transforms, spotLights)) # This one creates a new filter from scratch spotLightsAndTransforms = cmds.itemFilter(byType=('transform','spotLight'))
A note about '*':
'*' (asterisk) is a very useful character when searching things by string name!
aFilter = cmds.itemFilter(byName='a*')
Here we're asking for all objects that start with the letter 'a'. We don't care about what comes after the 'a'.
aFilter = cmds.itemFilter(byName='*a')
Here we're asking for all objects that end with the letter 'a'.
aFilter = cmds.itemFilter(byName='*a*')
And here we're asking for any object name that has at least one 'a'.
Real world example:
At some point I made an export tool that deleted all bindPoses in the scene to prepare the character FBX for the engine. When bindPoses are created in Maya, they're usually given a number after the name (bindPose1, bindPose46, etc.), hence why '*' is used.
nameFilter = cmds.itemFilter(byName = 'bindPose*') bndPs = cmds.lsThroughFilter(nameFilter) cmds.delete(bndPs)
Anyways, that's all for today! Thanks for reading :)