Pymel Blog Rss Feed

Saturday, April 7, 2012

Prior to starting any of these lessons be sure to import the PyMEL library. See here for why and how.

Today I’m going introduce you to the commands that Maya provides to you free of charge. Well, after you pay for a license of the software, you get these commands for free. I will go over user created commands in another tutorial.


Before I get into how to use built in commands. You can find a list of most of Maya’s commands by going to Help and the clicking on Python Command Reference.


built in maya commands tutorial help reference start


You’ll then find a plethora of commands at your disposal. If you feel overwhelmed, don’t worry. Most Maya programmers, including yours truly, have no idea what most of these commands do!  This is because you'll only tend to use a small percentage of the commands specific to where you live in Maya (modeling, dynamics, rigging, etc.).  You can use the text box on the left to filter down the list to something more manageable.


built in maya commands tutorial help reference start


Now let’s look at the anatomy of a built in maya command: We’ll start by looking at the 'sphere' command. The synopsis tells us what the command does as well as a few other tidbits. And then comes something very important: the return value. This value tells us what type of information the command is spitting back at us. This comes into play when you actually want to use a command for anything useful. So pay attention! In this case, the sphere command returns a list of strings; more specifically, an string containing the object name and a string containing the node name.  And when we run the command with PyMEL, we get more than just a list of strings, we get a list of objects.


built in maya commands tutorial help reference start


mySphereGrp = sphere()
print mySphereGrp

You can see that it prints out the contents of a list. [nt.Transform(u'nurbsSphere1'), nt.MakeNurbSphere(u'makeNurbSphere1')]

The first object is a transform node and the second object is an input node. We can move the sphere by doing something like this.

mySphere = mySphereGrp[0]


built in maya commands tutorial help reference start


Going back to the command, take a look at the section called 'Flags'. Flags affect how a command is executed. For example, to create a half sphere with a radius of 10, we’d modify the sphere command to this:

mySphereGrp = sphere(r=5, esw=180)

Here we are using the radius flag and the endSweep flag. Try experimenting to see what kind of spheres you can make by changing the flags. Keep in mind that as long as you enter valid values for flags, it usually won’t change the return value of a command. There are, however, some commands whose return type is dictated by the flags you use.


Since I’m in a good mood, I’ll show you another command. This one’s used a lot. It’s the 'ls' command. 'ls' lists objects in your scene. You use the flags to dictate what objects the command lists. In every valid case, it returns a list of objects. Here are some examples.

objList = ls() #list everything!!!
objList = ls(type='camera') #list all cameras in the scene
objList = ls(type='camera', sn=True) #list all cameras in the scene by their short names
objList = ls(sl=True) #list all selected objects

The last one is used so much that PyMEL offers a nice shortcut for us.

objList = selected() #pretty nice huh!

As a matter of completeness, I should point out that if you know that you only need a particular return value from a command, you can just use the index of the one you want.


mySphere = sphere()[0] #this stores just the transform node

Hopefully with this brief tutorial, you’ve gotten an idea of how to approach using built in maya commands. Most commands follow the same format of specifying flags and storing the return value. So forge ahead and try out other commands!

My comment system is powered by Disqus. And they require you to put in a name and an email. But I'd love to hear what you think. So if you want to comment anonymously, just put any name and test@test.com for your email.

blog comments powered by Disqus