MEL and Python in Maya each have built in commands to communicate with each other. MEL and Python communicate by calling commands in the other language and evaluating the results of the last executed command.
Python communicates with MEL using the eval() command. Unlike the other Python commands in this lesson, the eval() function does not belong to the Maya commands module (maya.cmds). The eval() function belongs to the maya.mel module. The eval() function can call MEL scripts or execute MEL commands by sending the commands as a string. Multiple MEL commands can be called in the string by separating the commands with semi-colons. The Python eval() function returns the results of the last executed MEL script or command within the eval brackets.
MEL communicates with Python using the python command. The python command accepts a string as its only argument. The string is sent to Python to be evaluated, and the result is returned to MEL. As Python has a more descriptive type system, some results from Python commands returned to MEL have their data type modified. For more information on type conversion, see .
To call MEL commands from Python
The maya.mel module is a module for evaluating MEL expressions in Python.
maya.mel.eval("sphere -radius 3;")
A sphere of radius three is created at the origin, just as if you had used the Python command.
global float $MyMELVariable=22.7;
Only global MEL procedures, variables and scripts are accessible from within Python.
TransferMELvar = maya.mel.eval("$temp=$MyMELVariable")
When transferring variables between MEL and Python, the functions return the value of the statement. MEL syntax does not allow you to return the value of a variable by using the variable as a command string. In MEL, when a variable is assigned a value, the value is returned to the Script Editor. Within the eval() statement, you assign the value of the global MEL variable to a temporary variable.
To call Python commands from MEL
Commands typed in the MEL tab are executed in MEL.
A sphere is created at the origin, just as if you had typed the command in the Python tab.
float $TransferVarPy = `python "MyPythonVariable"`;
Evaluation back quotes are used in MEL to use the return value of a command in assignment, when the return value is normally returned to the Script Editor history window.