pymel.core.language.Mel

Inheritance diagram of Mel

class Mel

Acts as a namespace from which MEL procedures can be called as if they were python functions.

Automatically formats python arguments into a command string which is executed via maya.mel.eval. An instance of this class is created for you as pymel.core.mel.

default:
>>> import maya.mel as mel
>>> # create the proc
>>> mel.eval( 'global proc myScript( string $stringArg, float $floatArray[] ){}')
>>> # run the script
>>> mel.eval( 'myScript("firstArg", {1.0, 2.0, 3.0})')
pymel:
>>> from pymel.all import *
>>> # create the proc
>>> mel.eval( 'global proc myScript( string $stringArg, float $floatArray[] ){}')
>>> # run the script
>>> mel.myScript("firstArg", [1.0, 2.0, 3.0])

The above is a very simplistic example. The advantages of pymel.mel over maya.mel.eval are more readily apparent when we want to pass a python object to our mel procedure:

default:
>>> import maya.cmds as cmds
>>> node = "lambert1"
>>> color = cmds.getAttr( node + ".color" )[0]
>>> mel.eval('myScript("%s",{%f,%f,%f})' % (cmds.nodeType(node), color[0], color[1], color[2])   )
pymel:
>>> from pymel.all import *
>>> node = PyNode("lambert1")
>>> mel.myScript( node.type(), node.color.get() )

In this you can see how pymel.core.mel allows you to pass any python object directly to your mel script as if it were a python function, with no need for formatting arguments. The resulting code is much more readable.

Another advantage of this class over maya.mel.eval is its handling of mel errors. If a mel procedure fails to execute, you will get the specific mel error message in the python traceback, and, if they are enabled, line numbers!

For example, in the example below we redeclare the myScript procedure with a line that will result in an error:

>>> commandEcho(lineNumbers=1)  # turn line numbers on
>>> mel.eval( '''
... global proc myScript( string $stringArg, float $floatArray[] ){
...     float $donuts = `ls -type camera`;}
... ''')
>>> mel.myScript( 'foo', [] )
Traceback (most recent call last):
    ...
MelConversionError: Error during execution of MEL script: line 2: ,Cannot convert data of type string[] to type float.,
Calling Procedure: myScript, in Mel procedure entered interactively.
  myScript("foo",{})

Notice that the error raised is a MelConversionError. There are several MEL exceptions that may be raised, depending on the type of error encountered: MelError, MelConversionError, MelArgumentError, MelSyntaxError, and MelUnknownProcedureError.

Here’s an example showing a MelArgumentError, which also demonstrates the additional traceback information that is provided for you, including the file of the calling script.

>>> mel.startsWith('bar') 
Traceback (most recent call last):
  ...
MelArgumentError: Error during execution of MEL script: Line 1.18: ,Wrong number of arguments on call to startsWith.,
Calling Procedure: startsWith, in file "..."
  startsWith("bar")

Lastly, an example of MelUnknownProcedureError

>>> mel.poop()
Traceback (most recent call last):
  ...
MelUnknownProcedureError: Error during execution of MEL script: line 1: ,Cannot find procedure "poop".,

Finally, some limitations: this Mel wrapper class cannot be used in situations in which the mel procedure modifies arguments (such as lists) in place, and you wish to then see the modified result in the calling code. Ie:

>>> origList = []
>>> newList = ["yet", "more", "things"]
>>> mel.appendStringArray(origList, newList, 2)
>>> origList
[]

You will have to fall back to using mel.eval in such situations:

>>> mel.eval('''
... string $origList[] = {};
... string $newList[] = {"yet", "more", "things"};
... appendStringArray($origList, $newList, 2);
... /* force a return value */
... $origList=$origList;
... ''')
[u'yet', u'more']

Note

To remain backward compatible with maya.cmds, MelError, the base exception class that all these exceptions inherit from, in turn inherits from RuntimeError.

static error(msg, showLineNumber=False)
classmethod eval(cmd)

evaluate a string as a mel command and return the result.

Behaves like maya.mel.eval, with several improvements:
  • returns pymel.datatype.Vector and pymel.datatype.Matrix classes
  • when an error is encountered a MelError exception is raised, along with the line number (if enabled) and exact mel error.
>>> mel.eval( 'attributeExists("persp", "translate")' )
0
>>> mel.eval( 'interToUI( "fooBarSpangle" )' )
u'Foo Bar Spangle'
globals = {}
classmethod mprint(*args)

mel print command in case the python print command doesn’t cut it

proc = None
classmethod source(script, language='mel')

use this to source mel or python scripts.

Parameters:
language : {‘mel’, ‘python’}

When set to ‘python’, the source command will look for the python equivalent of this mel file, if it exists, and attempt to import it. This is particularly useful when transitioning from mel to python via pymel.tools.mel2py, with this simple switch you can change back and forth from sourcing mel to importing python.

static tokenize(*args)
static trace(msg, showLineNumber=False)
static warning(msg, showLineNumber=False)