![]()
Important differences between MEL and Python
![]()
Miscellaneous differences
- The eval, evalDeferred, scriptJob, and runTimeCommand Maya commands are not supported in Python. However, note the following:
- Python has a built-in
evalfunction for evaluating Python expressions.- Python has
maya.mel.evalfor evaluating MEL expressions.- evalDeferred is replaced by
maya.utils.executeDeferred()in Python.For more information, seeCalling MEL from Python.
- To access MEL commands created using the MEL runTimeCommand command, use
maya.mel.eval; for more information, see Calling MEL from 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()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:
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
barUnderstanding 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.