Python scripting in Maya week 2

What I forgot to tell you in the last post is how to actually execute the code
in the script editor.
To keep the code you’ve written, select the text first, then press ctrl+enter.
This executes the code, but does not clear the editor.
Just a little tip. Let’s get on with Python in Maya!

in a command such as scale, we can see from the MEL that it has a parameter in
the beginning called -r then a three value numeric parameter to follow.
This has nothing to do with the -r, because the -r (-relative) in MEL is just
a boolean value meaning “True” in Python. The parameter signals a relative
scale, not an absolute scale.
Therefor we must write it as so in python:

scale(4,5,4,r=True)

The reason for putting r=True last in the list is because it is a keyword
argument, and in Python you must put it last if the other arguments are not
also keyword arguments.
Because the relative attribute is set to True, this command can be repeated
to add the same size to the object. Try it out.

Let’s make a script that creates a nicely beveled cube that we can use as
dice or something. I always bevel the edges slightly anyway, so why not have
a script in the shelf that does it for me.

In the script editor, type in the following:

polyCube(w=4,h=4,d=4)
polyBevel(offset=0.10,fillNgons=True,segments=3,smoothingAngle=30)

The first line creates a cube with 4 units in xyz, and the second line bevels
the cube with some adjustments so that it gets smooth and nice.

To place this as a tool in one of your shelves, just select the code from the
script editor and middle mouse drag it into the shelf.

You can now create small scripts to create elements you use often in your scenes,
such as lighting setups, planes, cameras, you name it. That’s a nice start.

Try to create and do different things in Maya with the script editor open, then
try to “translate” the MEL output into working Python code.

Until net time…

Advertisements