This post is intended as a follow-up to: Don't use Cmds, use Pymel
You'll use this guy a lot, especially if you're using Pymel (why aren't you using Pymel?)
The ls() command.
Pretty sure the ls stands for "list". I never bothered to check, actually, but I assume they abbreviated it because list is already a native python function used to turn variables into a list of variables.
ls() is useful in a number of situations. It can be used to create a python list of specific objects based on the arguments you input.
Check out all the available arguments here:
https://help.autodesk.com/cloudhelp/2016/ENU/Maya-Tech-Docs/PyMel/generated/functions/pymel.core.general/pymel.core.general.ls.html
But I shall go over the commonly useful ones.
Suppose I want to create a python list of all of a certain kind of object in the scene.
More specific? Okay, suppose I want to make all joint objects invisible. All of them. And I don't want to have to dig through the hierarchy to find them all.
I can do this using ls() and the type parameter:
import pymel.core as pm
allJnts = pm.ls(type="joint")
Doing this finds every single joint throughout the scene, composes them into a list and assigns that list to the variable allJnts.
Let's print the variable to see: