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'...] etc
A common cause of this is using *args inside ls()
:
cmds.ls(["fred", "barney"]) # OK, returns ['fred', 'barney']
cmds.ls([]) # OK, returns []
cmds.ls(*[]) # not ok: returns all nodes!
In Maya 2015 and earlier, an ls()
query which finds nothing will return None
instead of an empty list. In case of using the result, it can result in an exception:
for item in cmds.ls("don't_exist"):
print item
# Error: TypeError: file <maya console> line 1: 'NoneType' object is not iterable
The cleanest idiom for working around this is always adding an alternative output when None is returned adding or []
after an ls()
operation. That will ensure that the return is an empty list rather than None
:
for item in cmds.ls("don't_exist") or []:
print item
# prints nothing since there's no result -- but no exception