2010

Table Of Contents
Communicating between Python and MEL
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 MEL/Python communication of the Python guide.
To call MEL commands from Python
1 Select a Python tab in the Script Editor
2 Import the MEL module
import maya.mel
The maya.mel module is a module for evaluating MEL expressions in
Python.
3 Call a MEL command by typing the following:
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.
4 Select a MEL Tab in the Script Editor
5 Declare a variable in the MEL tab by typing the following:
global float $MyMELVariable=22.7;
Only global MEL procedures, variables and scripts are accessible from
within Python.
6 Select a Python tab.
Communicating between Python and MEL | 713