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.

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.
  7. Transfer the MEL variable’s value to Python by typing the following:
    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.

  8. Output the value of the transferred MEL variable using Python commands:
    print TransferMELvar;

To call Python commands from MEL

  1. Select a MEL tab in the Script Editor.

    Commands typed in the MEL tab are executed in MEL.

  2. Call a Python command by typing the following:
    python "cmds.sphere()";

    A sphere is created at the origin, just as if you had typed the command in the Python tab.

  3. Select a Python Tab in the Script Editor
  4. Declare a variable in the Python tab by typing the following:
    MyPythonVariable=22.7
  5. Select a MEL tab in the Script Editor.
  6. Transfer the Python variable’s value to MEL by typing the following:
    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.

  7. Output the value of the transferred variable in MEL:
    print $TransferVarPy;
    TipTransferring values between Python and Maya can also be accomplished by using invisible user interface elements. You can edit and query the contents of user interface elements with both scripting languages.