Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Last active October 12, 2021 13:11
Show Gist options
  • Save BigRoy/01ecf1b77c26b1583620 to your computer and use it in GitHub Desktop.
Save BigRoy/01ecf1b77c26b1583620 to your computer and use it in GitHub Desktop.
Parse a Maya MEL command string into the command name, args and kwargs.
def parse_mel_cmd(melStr):
"""Return the command, args and kwargs for a MEL command string"""
# Get python variant and split of pymel import line
import pymel.tools.mel2py as mel2py
pyCmd = mel2py.mel2pyStr(melStr)
pyCmd = pyCmd.splitlines()[1]
cmd, arguments = pyCmd.split("(", 1)
args, kwargs = eval("dummy" + "".join(pyCmd.partition("(")[1:]), {}, dict(dummy=lambda *x, **y: (x, y)))
return {'cmd': cmd,
'args': args,
'kwargs': kwargs}
print parse_mel_cmd('setAttr |balloon_model:balloon_GRP.translate -type "double3" -13.076091 21.600848 -0.975668;')
# {'cmd': 'setAttr',
# 'args': ('|balloon_model:balloon_GRP.translate', -13.076091, 21.600848, -0.975668),
# 'kwargs': {'type': 'double3'}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment