3D World

HOW DO I MOVE FROM MEL TO PYTHON?

Megan Barton, Birmingham

-

Antony Ward replies

Taking the time to experiment with MEL, the scripting language that is the beating heart of Maya, can be extremely rewarding. As good as MEL is, when Autodesk added Python support, it took what was already a good system and supercharg­ed it, making it more essential for its users to move over.

So how do you transfer your MEL code to Python? Well, one of the first things you must do when starting a Python script is to load the Maya commands module. This is a library that holds all the Maya commands, and once loaded it makes them all available to use.

In order to do this you simply import them, like this:

import maya.cmds as cmds

You import them as cmds so you can reference them in the script, although this can be any word you like.

With the module loaded you can now start to write your script, but the way you approach a line of Python is different

to one written in MEL. If you were to do something simple like move a cube 200 units along the X-axis, you would use something like this:

move -r -os -wd 200 0 0 “pcube1”;

This would not work in Python though, so first you need to update the command, so Maya looks for it in the module you imported earlier.

cmds.move

Next, the arguments you use need to be in parenthese­s.

cmds.move( )

Now you are all set to start adding your arguments and flags, and the order you write these is especially important. First

you define the amount the cube will move, using a comma between each number.

cmds.move( 200, 0, 0 )

You can now add the name of the object that you are working on along with the flags, all of which need to be separated with commas. With the flags, you don’t use a minus before each one, you instead need to define each with an equals sign, followed by one if it’s true, or zero if it’s false.

cmds.move( 200, 0, 0, “pcube1”, r=1, os=1, wd=1 )

So, there you’ve quickly converted a MEL command into Python, and you can follow the same steps to convert most other commands quickly and easily.

 ??  ??
 ??  ??

Newspapers in English

Newspapers from Australia