Important differences between MEL and Python
 
 
 

Changes

runTimeCommand -command "sphere -name myName" mySphere;

In a Python tab

import maya.cmds as cmds 
cmds.mySphere() 

As well, you can create Python runtime commands and call them from Python using the following syntax:

import maya.cmds as cmds 
def mySphere(): 	 
     cmds.sphere(name='myName') 
     cmds.runTimeCommand('MyBall', command='mySphere()') 
     cmds.MyBall() 

Miscellaneous differences

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:

if ( $foo == 1 ) $bar = 42; else $bar = 7;

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 bar

Understanding 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.