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 .
maya.mel.eval("sphere -radius 3;")
global float $MyMELVariable=22.7;
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.
float $TransferVarPy = `python "MyPythonVariable"`;