Mel command argument passing in Python

From over on my mel wiki

Python has some shortcuts you can implement when passing arguments to mel commands (or other Python functions).
Take for example a sphere (named pSphere1) that you want to scale. Here is the basic mel to do so:

float $scaleVals[] = {1.0, 2.0, .5};
// optionA:
setAttr pSphere1.scale -type double3 $scaleVals[0] $scaleVals[1] $scaleVals[2];
// optionB also works:
setAttr pSphere1.scale $scaleVals[0] $scaleVals[1] $scaleVals[2];

Likewise in Python, you can do something very similar:

import maya.cmds as mc

scaleVals = [1, 2, .5]
# optionA:
mc.setAttr("pSphere1.scale", scaleVals[0], scaleVals[1], scaleVals[2], type="double3")
# optionB also works:
mc.setAttr("pSphere1.scale", scaleVals[0], scaleVals[1], scaleVals[2])

However, in Python you can pass all the args in as a list\tuple variable by prefixing the variable name with an asterisk ‘*’. The function (mel command) will unpack the variable as individual positional arguments. It’s important that the variable have the same number of items as the command expects argument values, or a Python RuntimeError exception will be raised:

# option C, much cleaner!:
mc.setAttr("pSphere1.scale", *scaleVals)

You can see more about how this argument system works over on my Python Wiki.

Just ran into HYPE
Open for business
Comment are closed.