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 pymel.tools.py2mel 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:
cmd.deregister()
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:
- 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
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
first letter
first two letters
first three letters
first two letters plus a unique digit
Warning
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
Foo.bar 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.
Parameters: |
|
---|