This post is a bit of a 101 guide to transforming and handling nodes in Maya using Python. There are just so many different ways to control objects and their pivots, that even for my own benefit, I thought it would be handy to publish a cheat-sheet. ‘node’ is used to denote the name of the object that you are manipulating.
The things that you probably want to do the most to a node: translating, rotating and scaling a node. The rather less used transformation, shear is also available. The flags absolute and relative are used to specify whether the transformation is cumulative. The transformations can also be set using the ‘setAttr’ command, but that is rather fiddly.
cmds.xform(node, relative=True, translation=[1,1,1]) cmds.xform(node, absolute=True, rotation=[45,0,0]) cmds.xform(node, absolute=True, scale=[0.2,2,0.2]) cmds.xform(node, absolute=True, shear=[0,0,0.2])
This command puts the object back at the origin with zero rotations and scale values of 1.
Here, we set the transformation values to [0,0,0] for translation and rotation, with scale set to [1,1,1]. Unlike reset transformations, the object stays where it is, so effectively you are setting the new default transformation state for the object.
cmds.makeIdentity(node, apply=True, translate=True, rotate=True, scale=True)
Query Node Location
Get the location of the node, which is not necessarily the location of the pivot!
cmds.xform(node, query=True, worldSpace=True, translation=True)
Move the pivot of the node without affecting the node itself. Note that the object translation values do not change. Note that the concept of rotating the pivot doesn’t exist, as the pivot is considered simply a point in space.
cmds.xform(node, worldSpace=True, pivots=[0,0,0])
We can also move the pivot to the object transform location.
Or to the object center.
Query Pivot Location
This one got me for a while, as it’s not just a case of adding a query flag to the pivot translation command. You also have to specify ‘rotatePivot’. Confusingly, ‘scalePivot’ is a different and independent pivot associated with a node. When you specify ‘pivots’ in the xform command, you are moving both at the same time, and indeed, they can be moved independently by using the appropriate flag. This query command returns a list of the form [x, y, z].
cmds.xform(node, query=True, worldSpace=True, rotatePivot=True)
There are situations where you want to set an object’s translation value from its pivot location. For example, a boat model could have its pivot set to represent its central float point. You would want its zero translation point to be at the pivot, rather than the object center or the lowest point of its hull. I couldn’t find a direct command to do this, although maybe anyone reading this can correct me. A simple script was required to achieve this.
def reset_pivot(node): pivot = cmds.xform(node, query=True, worldSpace=True, rotatePivot=True) cmds.xform(node, relative=True, translation=[-pivot,-pivot,-pivot]) cmds.makeIdentity(node, apply=True, translate=True) cmds.xform(node, translation=[pivot,pivot,pivot])
That’s about it for now, before we delve into the rather murky waters of transformation matrices and relative rotations. Now is not the time to bring up the subject of gimbal locking.