unity – How do I translate anim values to script?

I need to copy the values of an anim file to a new animation clip.

However, I don’t understand how these values could be translated to / by script.

enter image description here

I see several key frames, but not all key frames are on the same time spot for all bones.

Could anybody give me a kickstart and show me how to edit the below code to copy the “hip” bone rotation / position to a new animation?

    // create a curve to move the GameObject and assign to the clip
    Keyframe() keys;
    keys = new Keyframe(3);
    keys(0) = new Keyframe(0.0f, 0.0f);
    keys(1) = new Keyframe(1.0f, 1.5f);
    keys(2) = new Keyframe(2.0f, 0.0f);
    curve = new AnimationCurve(keys);
    clip.SetCurve("", typeof(Transform), "localPosition.x", curve);

    // update the clip to a change the red color
    curve = AnimationCurve.Linear(0.0f, 1.0f, 2.0f, 0.0f);
    clip.SetCurve("", typeof(Material), "_Color.r", curve);

Can anybody (as a kick start) tell me how to change this code to reflect what I see in the graph editor for the “hip” bone?

Thank you!


I am now trying to solve it using the following approach:

I select the animation clip in the Editor, and then I click a custom menu item:

private static void DoAnimStuff()
    var nSelectedObject = Selection.activeObject;
    AnimationClip clip =(AnimationClip)nSelectedObject;

The next thing would be to involve the AnimationUtility. I have found the following code.

AnimationCurve curve = AnimationUtility.GetEditorCurve(clip,
            EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalPosition.x"));

Now I have found some code here:

However, now I have no idea what the code does and how I could use it: What is “transform”? And I don’t have an animator.

    string path = AnimationUtility.CalculateTransformPath(transform, animator.transform);
    var binding = EditorCurveBinding.FloatCurve(path, typeof(Transform), "localEulerAngles.y");
    AnimationCurve curve = AnimationUtility.GetEditorCurve(clip, binding);