py2melCmd(pyObj, commandName=None, register=True, includeFlags=[], excludeFlags=[], nonUniqueName='warn', invalidName='warn')

Create a MEL command from a python function or class.

A MEL command has two types of arguments: command arguments and flag arguments. In the case of passing a function, the function’s non-keyword arguments become the command arguments and the function’s keyword arguments become the flag arguments. for example:

def makeName( first, last, middle=''):
    if middle:
        return first + ' ' + middle + ' ' + last
    return first + ' ' + last

import pymel as pm
from import py2melCmd
cmd = py2melCmd( makeName, 'makeNameCmd' )
pm.makeNameCmd( 'Homer', 'Simpson')
# Result: Homer Simpson #
pm.makeNameCmd( 'Homer', 'Simpson', middle='J.')
# Result: Homer J. Simpson #

Of course, the real advantage of this tool is that now your python function is available from within MEL as a command:

makeNameCmd "Homer" "Simpson";
// Result: Homer Simpson //
makeNameCmd "Homer" "Simpson" -middle "J.";
// Result: Homer J. Simpson //

To remove the command, call the deregister method of the class returned by py2melCmd:


This function attempts to automatically create short names (3 character max) based on the long names of the methods or arguments of the pass python object. It does this by looping through long names in alphabetical order and trying the following techniques until a unique short name is found:

  1. by docstring (methods only): check the method docstring looking for something of the form shortname: xyz::
    class Foo():
    def bar():

    ‘shortname: b’ # do some things return

  2. by convention: if the name uses under_scores or camelCase, use the first letter of each “word” to generate a short name up to 3 letters long

  3. first letter

  4. first two letters

  5. first three letters

  6. first two letters plus a unique digit


if you edit the python object that is passed to this function it may result in short names changing! for example, if you have a class like the following:

class Foo():
    def bar():
        pass will be assigned the short flag name ‘b’. but if you later add the method Foo.baa, it will be assigned the short flag name ‘b’ and ‘bar’ will be given ‘ba’. The only way to be completely sure which short name is assigned is to use the docstring method described above.

  • commandName – name given to the generated MEL command
  • register – whether or not to automatically register the generated command. If False, you will have to manually call the register method of the returned WrapperCommand instance
  • includeFlags – list of flags to include. other flags will be ignored
  • exludeFlags – list of flags to exclude. other flags will be included
  • nonUniqueName – ‘force’, ‘warn’, ‘skip’, or ‘error’
  • invalidName – ‘force’, ‘warn’, ‘skip’, or ‘error’

Previous topic

Next topic


Core Modules

Other Modules

This Page