Finding a Specific translateX AnimCurve Node in Maya API (Even with Intermediate Nodes)

I’m trying to replicate the functionality of cmds.bakeResults() using the Maya API.

My question is: how can I get a specific animation curve node — for example, the translateX anim curve node?

The code I have works only when the translateX attribute is connected directly to its anim curve node.

However, that’s not always the case. In some situations (as shown in the image), there can be another node inbetween the attribute and the anim curve node.

I tried using MItDependencyGraph, but it returns all anim curve nodes, and I’m not sure how to check if a translateX anim curve node is one of the returned nodes.

import maya.api.OpenMaya as om
import maya.api.OpenMayaAnim as oma

selected_object = "pCube1"

def get_dag_object(object_name):
    return om.MSelectionList().add(object_name).getDependNode(0)

def find_plug(object_name, attribute_name):
![Screenshot 2025-08-14 165913|676x500, 100%](upload://pwYM4bNp4J74YD1ToAyvJas8ICR.jpeg)

    obj = get_dag_object(object_name)
    return om.MFnDependencyNode(obj).findPlug(attribute_name, False)

def get_anim_curve(object_name, attribute_name):
    attribute_plug = find_plug(object_name, attribute_name)
    
    source_plug = attribute_plug.source()
    
    if(source_plug.node().hasFn(om.MFn.kAnimCurve)):
        return oma.MFnAnimCurve(source_plug.node())
    
print(get_anim_curve(selected_object, "tx"))