How do I add a script-generated AnimationClip to a Mecanim animation StateMachine?

Hi, I have an AnimationClip made in the script:

AnimationClip clip = new AnimationClip();
clip.SetCurve(path, typeof(Transform), "localPosition.x", ...);

Then I got a Transform with an Animator. How can I add this clip to the existing StateMachine or create a new StateMachine which accepts this animation? The clip plays just fine with the old Animation system.

I have tried the following code, but the animation doesn’t play with the newly created clip above. However, it plays if I load the clip from a file.

 Animator animator = GetComponent<Animator>();
 UnityEditorInternal.AnimatorController c = 
    UnityEditorInternal.AnimatorController.CreateAnimatorControllerAtPathWithClip(
    "Assets/Temp/TestController.controller", clip);

 UnityEditorInternal.AnimatorController.SetAnimatorController(animator, c);
 animator.Play("");

I also tried to add it to the state machine:

 Animator animator = GetComponent<Animator>();
 UnityEditorInternal.AnimatorController c = 
    UnityEditorInternal.AnimatorController.CreateAnimatorControllerAtPathWithClip(
    "Assets/Temp/TestController.controller", clip);

 StateMachine sm = c.GetLayer(0).stateMachine;
 State state = sm.AddState("test");
 state.SetAnimationClip(clip);
 AnimatorController.SetAnimatorController(animator, c);
 animator.Play("test");

But with the same results. Is there something that needs to be done to the newly created AnimationClip before it is accepted by the Animator?

clip.setCurve() is only adding a curve to a clip that is created in memory but is not saved to disk. The clip is not associated with a Muscle Definition, etc. that Mecanim needs.

In the case of the StateMachine, I’ve only been able to SetAnimationClip() from a clip that was loaded via Resources.LoadAssetAtPath().