Getting Keyed Attributes from AnimCurves

Does anyone know of a better way to do this? With the addition of animLayers (which are great) I haven’t found a simple maya command for finding the channel driven by an animation curve node. In the simplest situation, you used to be able to directly trace the curve’s output connection:

import maya.cmds as mc
mc.listConnections('myAnimCurve.output', source=False, plugs=True)[0]

Now with animLayers, there will be any number of nodes daisy-chained in-between the animCurve and the driven attribute, with different attributed names. Here’s how I’m currently handling that, though I’m not totally happy about it:

import maya.cmds as mc
def _getChannelFromAnimCurve(curve):

    attr = ''
    if '.' in curve:
        curve, attr = curve.split('.')

    nodeType = mc.nodeType(curve)
    if nodeType.startswith('animCurveT') or nodeType.startswith('animBlendNode'):
        source = mc.listConnections(curve+'.output', source=False, plugs=True)
        if not source and nodeType=='animBlendNodeAdditiveRotation':
            #if we haven't found a connection from .output, then it may be a node that uses outputX, outputY, etc.
            #get the proper attribute by using the last letter of the input attribute, which should be X, Y, etc.
            source = mc.listConnections(curve+'.output'+attr[-1], source=False, plugs=True)
        if source:
            nodeType = mc.nodeType(source[0])
            if nodeType.startswith('animCurveT') or nodeType.startswith('animBlendNode'):
                return _getChannelFromAnimCurve(source[0])
            return source[0]

Basically recursing through upstream connections and testing if we’re still looking at some sort of animCurve or animBlend node, until we find one that isn’t. It’s not very optimized, and it’s a bit sloppy with how it tests nodeType and constructs attributes, but it seems to work for the most part. I don’t totally trust it, though. For one, in the rare event that your driven channel is actually on a animCurve or animBlend node, it wont return it. If anyone has a more elegant solution, I’d be keen to hear it.

Share Button
This entry was posted in Python. Bookmark the permalink.

3 Responses to Getting Keyed Attributes from AnimCurves

  1. THNKR says:

    You can simplify your script if you skip the animBlendNodes with

  2. Thanks for the response, I forgot about that. I was holding out hope for a maya command that I had missed, but I think I’ll give this an optimization pass and run with it. Thanks!

  3. brian ward says:

    you may want to keep in mind cases where animation curves are driving more than one connection? like in the case of a ‘duplicate special’ with ‘duplicate input connections’ checked. maybe you could have a flag to filter for curves that are unique to that channel or used by other nodes, or even filtering for curves that are only used within a specified hierarchy? or at least collect the resulting nodes as a list instead of an object? gets complex quickly, but maybe worth a thought since it’s a commonly overlooked case in anim tools which can cause unexpected results.

Leave a Reply

Your email address will not be published. Required fields are marked *