Skip to content

Python: getAttr and setAttr with color attributes

by nikos on April 25th, 2011

Maya’s built-in Python support leaves plenty of things to be desired when it comes to dealing with arrays of attributes. Lets say we want to set the color of the built-in lambert shader.

# The hard way
import maya.cmds as cmds
color = cmds.getAttr( "lambert1.color" )
# result: [(0.5, 0.5, 0.5)]
cmds.setAttr( "lambert1.color", color[0][0], color[0][1], color[0][2], type="double3" )

As we can see the built-in Python module returns a tuple wrapped in a list. This is very strange and not very handy. Fortunately the bright minds behind PyMEL for Maya wrote the attribute functions the way they were meant to be run.

# The easy way
from pymel.core import *
color = getAttr( "lambert1.color" )
# result: (0.5, 0.5, 0.5)
setAttr( "lambert1.color", color )

Much better and we don’t even have to specify the data type when setting the attribute.

From → Python

No comments yet

Leave a Reply

You must be logged in to post a comment.