Changes
- MEL commands created using the MEL
runTimeCommand command are accessible in Python. For example:
In a MEL tab:
runTimeCommand -command "sphere -name myName" mySphere;
In a Python tab
import maya.cmds as cmds cmds.mySphere()
As well, you can create Python runtime commands and call them from Python using the following syntax:
import maya.cmds as cmds def mySphere(): cmds.sphere(name='myName') cmds.runTimeCommand('MyBall', command='mySphere()') cmds.MyBall()
Miscellaneous differences
- The
eval and
evalDeferred Maya commands are not supported in Python. However, note the following:
- Python has a built-in eval function for evaluating Python expressions.
- Python has maya.mel.eval for evaluating MEL expressions.
- evalDeferred is replaced by maya.utils.executeDeferred() in Python.
For more information, see Using Python.
- MEL has its own warning and error message reporting mechanisms integrated with Maya message reporting. This includes color feedback on the command line. Python comes with system modules that provide warning and error reporting functions; however, these are not integrated into Maya and do not provide color feedback in the command line.
- Units in Python must be specified as strings with quote marks; for example:
maya.cmds.scale(3, 3, 3, r=True, p=('0cm', '0.5cm', '0cm'))
- Command flag arguments in Python scripts take either a string that contains a Python script (as the MEL equivalent does) or a Python callable object such as a function.
For example:
import maya.cmds as cmds def defaultButtonPush(*args): print 'Default was pushed.' cmds.window( width=150 ) cmds.columnLayout( adjustableColumn=True ) cmds.button( label='Default', command=defaultButtonPush ) cmds.button( label='Left', align='left' ) cmds.button( label='Center', align='center' ) cmds.button( label='Right', align='right' ) cmds.showWindow()
- MEL uses a different syntax from Python for specifying a range of values. In MEL, using * to specify a range of values is done using "*", for example:
select -r surface1.cv["*"][0];
In Python, to specify a range of values, * is used without quotations. For the example above, the equivalent in Python is:
cmds.select( 'surface1.cv[*][0]' , r=True)
Returning and echoing results
There are two ways in which MEL and Python differ in the areas of returning and echoing results. One is relevant to proper script execution while the other is a superficial issue. This section discusses both echoing a result and returning a result -- they are sometimes confused.
This section will mostly help those who are familiar with MEL, but new to Python.
Returning Results
When MEL executes a script, it returns the result of the last executed statement, if there is any. Statements that assign values to variables and procedure calls that return results are types of statements which return results. For example, the following block of code will have a result, which MEL will echo to the script editor and command line message area:
if ( $foo == 1 ) $bar = 42; else $bar = 7;
By contrast, statements that assign values in Python do not return results, though Python executes the assignment.
Python's syntax allows you to simply reference a variable in order to return its value. MEL’s syntax does not permit you to simply write the name of a variable as a complete statement.
The MEL code fragment above could be written as the following in Python. The final line (bar) returns the result.
if foo == 1: bar = 42 else bar = 7 bar
Understanding this difference is important if you want to use values computed in one language in the context of the other. For example, if you want to use a Python value in MEL, you can simply do the following:
$myMELvariable = python ("myPythonVariable");
Conversely, if you want to use a MEL variable in Python, you need to do something like:
import maya.mel myPythonVariable = maya.mel.eval ('global $myMELvariable; $temp=$myMELvariable;' )
This works because the assignment statement, which is last in the script passed to the eval command, returns a result.
You can access only globally scoped MEL variables in Python.
Echoing Results
MEL echoes the result, if any, returned by the last statement of a script, regardless of how many lines are in it. Python only echoes results of a single statement.