How to get attribute value from animation layer?

How to get attribute value from animation layer?

RFlannery1
Collaborator Collaborator
3,067 Views
2 Replies
Message 1 of 3

How to get attribute value from animation layer?

RFlannery1
Collaborator
Collaborator

I am trying to figure out how to get the value of an attribute on a specified animation layer.  If the attribute has an animation curve on the animation layer, then I can query that animation curve.  But I don't know how to get the value if there is no animation curve.  For example, if "pCube1.translateX" is assigned to "AnimLayer1", and the value is 5; but there aren't any keyframes set.  It is just a static value.

 

How do I get that value?

0 Likes
Accepted solutions (1)
3,068 Views
2 Replies
Replies (2)
Message 2 of 3

RFlannery1
Collaborator
Collaborator

I think I figured it out.  I can get the plug on the animBlendNode, and then query that for the value.  Here is how I figure out what the plug is:

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.
        conns = cmds.listConnections(nodeAttr, type='animBlendNodeBase', s=True, d=False)
        blendNode = None
        while conns:
            blendNode = conns[0]
            conns = cmds.listConnections(blendNode, type='animBlendNodeBase', s=True, d=False)
        plug = '{0}.inputA'.format(blendNode)
    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 in Maya 2016, 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
0 Likes
Message 3 of 3

RFlannery1
Collaborator
Collaborator
Accepted solution

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

 

0 Likes