Tutorial by Examples

Use the ls() commands to find objects by name: freds = cmds.ls("fred") #finds all objects in the scene named exactly 'fred', ie [u'fred', u'|group1|fred'] Use * as a wildcard: freds = cmds.ls("fred*") # finds all objects whose name starts with 'fred' # [u'fred', u'freder...
Using ls() as a filter can sometimes provide produce odd results. If you accidentally forget to pass a filter argument and call ls() with no arguments, you will get a list of every node in the Maya scene: cmds.ls() # [u'time1', u'sequenceManager1', u'hardwareRenderingGlobals', u'renderPartition...
ls() includes a type flag, which lets you find scene nodes of a particular type. For example: cameras = cmds.ls(type='camera') // [u'topShape', u'sideShape', u'perspShape', u'frontShape'] You can search for multiple types in the same call: geometry = cmds.ls(type=('mesh', 'nurbsCurve', 'nu...
Since ls() finds objects by names, it's a handy way to find out if an object is present in the scene. ls() with a list of objects will only return the ones which are in the scene. available_characters = cmds.ls('fred', 'barney', 'wilma', 'dino') # available_characters will contain only the named...
When working with components, such as vertices or uv points, Maya defaults to returning a colon-separated range rather than individual items: print cmds.ls('pCube1.vtx[*]') # get all the vertices in the cube # [u'pCube1.vtx[0:7]'] You can use ls with the flatten option to force Maya to expan...
Many ls() queries are intended to find a single object, but ls always returns a list (or, in older Mayas, a single None). This creates complicated error checking for a simple question. The easiest way to get a single value from an ls under any circumstances is result = (cmds.ls('your query here'...

Page 1 of 1