First post about Python scripting in Maya

This is the first post in a planned series of posts about Python scripting in
Autodesk Maya. Don’t expect this to turn you into a pro or something, this is
something I do to learn the material better myself.
I don’t know about your experience with Python, but this is not meant to be
a tutorial in the python language. I’ve used Python for some time now, and
I hope you are familiar with the language to get something out of the content.
You can probably do basic stuff without knowing the ins and outs of Python,
but to do something creative you must have some knowledge about the
different datatypes and constructs that defines Python.
One thing that also is important is that you understand the basics of how MEL
works, as most of the same applies to Python for Maya.
Well, let’s get started!

Getting access to Python in Maya

You can use Python either as a single command launcher from the command line,
or as a full scripting environment in the script editor, I’ll mention both in
sequence.
To start using Python in Maya you have to click the command line where it says
“MEL”. This will change into “Python”, and for one-shot commands this is all
you need.

To access Python through the script editor, click the script editor button
also on the command line, next to the output field.
There you’ll see to tabs, MEL and Python. Off course you should choose Python,
but you already knew that!
In the script editor I also choose “Command” -> “Command Completion” and
“Command” -> “Object Path Completion”.
This makes experimenting with Python a lot easier, as you can try out
different functions without knowing the names.

One thing you should be able to do is to transform MEL commands into Python,
so that you can download existing scripts, modify them to your needs and run
them in Python. Most of the scripts out there are still MEL, but py scripts
start to show up here and there.
I’ve always wanted to learn MEL, but it didn’t really make sense to me, so I
find the Python logic easier to grasp. Besides I use it for other tasks, so
I don’t have to learn a whole new language and tools.

Let’s make a polygon cube!
Let’s see, if I watch my script editor output while creating a cube I see that
the MEL needed for this is:

polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1;

Well, it’s actually not that complicated but this is all the default arguments
used by MEL in the output. The same cube will appear by just typing:

polyCube

Try that in the command line by changing back to MEL and press enter. Whee!
But hey! This is not Python… True. Lets try to switch back to Python
and run:

polyCube()

as all function calls ends with parentheses.
This does not work, because we have to import the namespace that the Maya
commands live in. We can do that by:

from maya.cmds import *

This makes a bunch of frequently used commands accessible to Python.
Now we can try again with:

polyCube()

or, for a change, let’s say that we wanted to change the defaults. Look back
to the MEL command used to create the polyCube. -w 1 -h 1 -d 1 etc.
All these define width, height, depth, sizeX, sizeY, sizeZ and so on.
To access these preferences in python, we have to write it as arguments
with a value in the construction call:

polyCube(w=2,h=3,d=4)

This creates a different cube with changed parameters.

Well, this wasn’t much but I’ll post more soon!
Try creating different shapes with different attributes to get a feel for how
it works.

Have a nice one!

Advertisements