Python: getAttr and setAttr with color attributes
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.
No comments yet