Sunday, 29 May 2022

Scripting: ls() command

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:

Scripting: Don't Use Cmds - Use Pymel

This post is intended as a follow-up to: Getting objects inside a script

If you've been using Python in Maya for long you're probably already used to typing this at the beginning of every script:

import maya.cmds as mc

Maybe you assign it to a different name, like cmds or commands or something. In any case, Cmds is great - it's just outdated is all. It has a newer, sleeker sibling called Pymel.

Autodesk's own website describes Pymel as being what Cmds was always meant to be. As the name implies, it is a more successful fusion of Python and MEL.
The great thing about switching to Pymel is that you barely need to learn anything new. All you need to do is import pymel.core at the top of each script instead of maya.cmds.

No more of this:

import maya.cmds as mc

Instead, this!:

import pymel.core as pm

All the functions that Cmds gives you access to, Pymel has as well.

Scripting: Getting Objects From the Scene

Oooooh, so you've decided to start scripting, have you nowwwwww?

First thing to understand is how Maya handles the names of nodes.
I've an entire post about it here:

Long story short - Maya doesn't allow objects under the same parent to have the same name. If you try, it will slap a number on the end of one of them to force objects to have different names.

In order to do stuff with/to an object in a Maya scene, you need to be able to refer to that object in the script. A rudimentary way of doing that is by calling the name of that object as a string.

If I have an object called "pCube1", and I know it's called that, I can do stuff to it (like select it) using "pCube1" as a string

import maya.cmds as mc

mc.select("pCube1")

This should normally work. But what if there is more than one object with that name? Maya does allow that after all, as long as the name-sharing objects are not in the same place in the hierarchy, like so: