






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Professor: Kesson; Class: Programming Concepts for Visual Effects; Subject: Visual Effects; University: Savannah College of Art and Design; Term: Unknown 1989;
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!
Python
There are a number of differences in how Maya commands are invoked in Python relative to how they are used in MEL, given that the languages are so different. Here are the basics to getting started with Python in Maya:
There are several ways of entering Python in Maya.
To facilitate having both MEL and Python scripting at the same time in Maya, the Script editor has been modified to have separate tabs for each language. Statements entered into the MEL tabbed window are sent to MEL to be processed; similarly, statements entered into the Python tabbed window are processed by Python. Returned results from Python come prefixed with the Python comment character (#). For more information, see Script editor.
You can also enter short Python commands into the command-line. A toggle allows you to enter either MEL or Python commands. You can -mouse drag Python scripts to the Shelf, just like MEL scripts. A dialog box appears asking whether your script is a Python or MEL script.
The Python bindings for all of the native Maya commands are in the maya.cmds module. In order to access these commands, you must enter the following in the Python tab of the Script editor in each session: import maya.cmds
This allows you to use Maya commands. For example: maya.cmds.ls() maya.cmds.sphere( radius=4 ) You can import Maya commands to a different and shorter namespace: import maya.cmds as cmd cmd.sphere() Tip You can import maya.cmds automatically in the userSetup.py file. You can modify this to use your preferred prefix; for example: import maya.cmds as mc mc.sphere() For more information, see Initializing the Maya Environment in and for Python. Note Alternatively, you can import the Maya commands into the top-level namespace using: from maya.cmds import * after which you can use a briefer way to reference Maya commands: ls() sphere( radius=4 ) WARNING : Importing to the top-level namespace overrides definitions from Python built-in and other modules. For example, Python has its own help system and a call to the Python version of help produces output that is specific to Python. A call to maya.cmds.help provides help about Maya commands. Importing maya.cmds into the top-level namespace would result in not being able to access Python help. Flags (named arguments) Flags are handled differently in Python than they are in MEL. MEL was designed with a shell command style syntax. For Maya commands in Python, the command argument syntax has been adapted in a way that is natural for Python. As a result, flags—both long and short forms—are passed to commands as named arguments. The name of the argument is the flag name and the flag’s arguments are passed to the named argument.
The MEL sphere command is: sphere -radius 4; In the Python version, the flag radius is referenced as a named argument and, since there is a single argument, the value is passed as follows: maya.cmds.sphere( radius=4 )
If the flag has multiple arguments, then the arguments need to be packed into a list or a tuple. Here is an example of a command with a flag that has three arguments.
maya.cmds.ambientLight( rgb=( 0.2, 0.3, 0.4 ) )
maya.cmds.ambientLight( rgb=[ 0.2, 0.3, 0.4 ] )
-time 1.0sec -time 15ntsc -time 20 Cut the keys at time 1.0 second, frame 15 (in NTSC format), and time 20 (in the currently-defined global time unit). time=[('1.0sec',), ('15ntsc',), (20,)] -time "10:20" Cut all keys in the range from 10 to 20, inclusive, in the current time unit. time=(10,20) -time "10:" Cut all keys from time 10 (in the current time unit) onwards. time=('10:',) -time ":10" Cut all keys up to (and including) time 10 (in the current time unit). time=(':10',) -time ":" Cut all keys time=(':',) -index 0 Cut the first key of each animation curve. (Indices are 0-based.) index=(0,) -index 2 -index 5 -index 7 Cut the 3rd, 6th, and 8th keys. index=[(2,),(5,),(7,)] -index "1:5" Cut the 2nd, 3rd, 4th, 5th, and 6th keys of each animation curve. index=[("1:5",)]
Certain flag changes to Maya commands are necessary because the arguments for multi-use flags in Python must be passed as a list to the flag. This causes a problem for commands in which different multi-use flags must be mixed-and-matched. Since each multi-use flag’s arguments are provided in a separate list in Python, there is no way to intermix them. The few commands which relied on this have been extended so that a single multi-use flag handles the job of the individual multi-use flags. These commands include: polyAppendVertex: new append flag which can be used in place of the point, vertex, and hole flags. polyAppend: new append flag which can be used in place of the point, edge, and hole
flags. polySplit: new insertpoint flag which can be used in place of the facepoint and edgepoint flags. polyCreateFacet: the existing point flag as been modified so that it may be also used to specify holes. roundConstantRadius: a new side flag replaces the use of sidea and sideb that are supposed to be intermixed. The use of all of these flags is documented in the CommandsPython reference documentation. In all cases, the flags are backwards compatible. All old flags have been left in place. Arguments and objects The curveOnSurface example above also illustrates another Python syntax requirement. Besides flags, Maya commands may also take arguments and objects. Arguments are values of a fixed type that are required by a command. For example, the move command takes three arguments representing the x, y, and z values for the move. Objects are the entities upon which a command operates (for example, an object in a scene or a UI element). There can be a variable number of objects for a command and sometimes they are implicit, based on the current selection list. Objects and arguments are passed to commands as they would be in MEL, but they must be passed in the following order: command arguments object flags/named arguments This is different from MEL where the ordering requires that objects appear at the end of the argument list. However, Python requires that named arguments come after all other arguments. Summary of argument types The following table shows a summary of the flag (named argument) types you can use in the Maya Python module. Flag type Example Comment plain flag MEL: ls -selection Python: maya.cmds.ls( selection=True ) Flags with no arguments get a boolean True/False switch flag with single argument
sphere -radius 10 Python: maya.cmds.sphere( radius=10 ) flag with multiple MEL: use a tuple to
'nurbsSphere1', objectSpace=True ) query where a flag/argument takes a value
skinCluster -q -inf; Python maya.cmds.skinCluster (q=True, inf=True) Query flag requires a boolean True. query where a flag/argument does not take a value
skinCluster -inf joint1 -q -dr; Python: skinCluster (q=True, inf='joint1', dr=True) Standard input (stdin) implementation Python supports reading from STDIN (standard input). In a Python script, this is accomplished by reading from sys.stdin or calling raw_input. When Maya is running in GUI mode (as opposed to batch mode), Maya intercepts these calls from Python and prompts you with a dialog box where you can type your input. I Maya overrides sys.stdin with its own implementation. If you want to use Python’s native stdin object, you can do so by referencing sys.stdin. MEL/Python communication A new MEL command—python—takes a string and passes it to be executed in Python. The python command attempts to convert the result into a MEL type. python( "import maya.cmds" ) python( "maya.cmds.ls" ) Note Only strings with a single Python statement return a result. This is a limitation currently imposed by Python. Python has a more sophisticated type system than MEL, so it is not possible to convert all Python data types into native MEL types. The python command converts those that it knows how to handle. For the rest, it requests a string representation of the object and returns that. The following table shows the currently supported mappings:
Python Return Value MEL Conversion string string unicode string int int float float list containing numbers, including at least one float float[] list containing only integers or longs int[] list containing a non-number string[] anything else string
To call MEL from Python, use the maya.mel.eval() function. It can do a better job of conversion than the MEL python command since Python has more flexible type support. You must import the maya.mel module to use this command; that is: import maya.mel as mm mm.eval("polySphere;") Here is a table of the supported conversions: MEL Return Value Python Conversion string string int int
When you move the slider, you see this error:
If you want to create a callback that works with any number of arguments, use a variable argument list: def genericCallback( *args ): print( "args: " + str ( args ) ) cmds.button( command=genericCallback )