.ls() – Listing Maya attributes in Python

To get a list of items in Maya, this relies on the Maya commands library:

from maya import cmds

We can run this to get a list of all the objects in the scene:

from maya import cmds
print cmds.ls()

This will return everything including a bunch of hidden objects that we don’t care about and truthfully, don’t need to know about

What we can do is tell the list function more specifically what we want, we can do this by passing arguments to the .ls() function, lets select all ‘selected’ objects:

from maya import cmds
print cmds.ls(selection=True)

We can now store what we have selected in a variable, by changing the print command to a variable name:

from maya import cmds
print cmds.ls(selection=True)
selection =cmds.ls(selection=True)