runTimeCommand -command "sphere -name myName" mySphere;
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()
For more information, seeCalling MEL from Python.
maya.cmds.scale(3, 3, 3, r=True, p=('0cm', '0.5cm', '0cm'))
import maya.cmds as cmds def defaultButtonPush(*args): print 'Default was pushed.' cmds.window( width=150 ) cmds.columnLayout( adjustableColumn=True ) cmds.button( label='Default', command=defaultButtonPush ) cmds.button( label='Left', align='left' ) cmds.button( label='Center', align='center' ) cmds.button( label='Right', align='right' ) cmds.showWindow()
select -r surface1.cv["*"][0];
In Python, to specify a range of values, * is used without quotations. For the example above, the equivalent in Python is:
cmds.select( 'surface1.cv[*][0]' , r=True)
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.
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.