Getting object attributes in Maya with Python

Modifying an object inside Maya usually involves modifying its attributes.

These attributes are represented in code by the object name followed by the attribute name:

polyCube1.input

Where .input would be the attribute name, for example .translate, .rotate etc

Let’s see an example:

# Import the Maya commands library
from maya import cmds

# Create a cube
cube = cmds.polyCube()

# Store the cube name
cubeShape = cube[0]

# Store the size of the object on the X axis using the scale attribute
xSize = cmds.getAttr(cubeShape+'.scaleX')

print xSize
Output: 1.0

Shorthand Attributes

Attributes in Maya have shorthand versions, to name a few:

translateXtx
translateYty
translateZtz
scaleXsx
scaleYsy
scaleZsz
rotateXrx
rotateYry
rotateZrz

We’ll modify our previous code to use the shorthand:

# Import the Maya commands library
from maya import cmds

# Create a cube
cube = cmds.polyCube()

# Store the cube name
cubeShape = cube[0]

# Store the size of the object on the X axis using the scale attribute (shorthand)
xSize = cmds.getAttr(cubeShape+'.sx')

print xSize