Here is a small change from the above answer. There was a problem if the attribute was a rotate value. Also, my friend suggested that I could use the "listHistory" command instead of traversing the animBlend nodes manually:
def getAttrPlugForLayer(nodeName, attribute, animLayer):
""" Find the animBlendNode plug corresponding to the given node, attribute,
and animation layer.
"""
nodeAttr = '{0}.{1}'.format(nodeName, attribute)
if not isObjectInAnimLayer(nodeAttr, animLayer):
return None
plug = None
if animLayer == cmds.animLayer(q=True, root=True):
# For the base animation layer, traverse the chain of animBlendNodes all
# the way to the end. The plug will be "inputA" on that last node.
blendNode = cmds.listConnections(nodeAttr, type='animBlendNodeBase', s=True, d=False)[0]
history = cmds.listHistory(blendNode)
lastAnimBlendNode = cmds.ls(history, type='animBlendNodeBase')[-1]
if cmds.objectType(lastAnimBlendNode, isa='animBlendNodeAdditiveRotation'):
letterXYZ = nodeAttr[-1]
plug = '{0}.inputA{1}'.format(lastAnimBlendNode, letterXYZ.upper())
else:
plug = '{0}.inputA'.format(lastAnimBlendNode)
else:
# For every layer other than the base animation layer, we can just use
# the "animLayer" command. Unfortunately the "layeredPlug" flag is
# broken in Python, so we have to use MEL.
cmd = 'animLayer -q -layeredPlug "{0}" "{1}"'.format(nodeAttr, animLayer)
plug = mel.eval(cmd)
return plug
def isObjectInAnimLayer(obj, animLayer):
""" Determine if the given object is in the given animation layer.
Parameters:
* obj - Can be either a node name, like "pCube1", or a node/attribute combo,
like "pCube1.translateX".
* animLayer - The name of an animation layer.
"""
objAnimLayers = cmds.animLayer([obj], q=True, affectedLayers=True) or []
if animLayer in objAnimLayers:
return True
return False