Baking geometry of a mesh for every frame in animation...

I’m new in Unity and my task is to extract mesh data of existing skinned and animated objects. So, I want to access vertices of my mesh for every frame of every animation clip of selected object. So, I have a script:

public class TestScript : ScriptableWizard
{
[...]
    void OnWizardCreate ()
    {
        GameObject subject = Selection.activeGameObject;
        Animation animation = subject.GetComponent<Animation>();
        SkinnedMeshRenderer[] smrs = subject.GetComponentsInChildren<SkinnedMeshRenderer>();
        // Process
        foreach (AnimationState  clip in animation) {
            animation.Play(clip.name);
            foreach (SkinnedMeshRenderer smr in smrs) {
                // Process every mesh
                Mesh sharedMesh = smr.sharedMesh;
                Debug.Log("Processing mesh " + sharedMesh.name + " with " + sharedMesh.vertices.Length + " vertices:" );
                clip.time = 0.0f;
                int frame = 0;
                while (clip.time <= clip.length) {
                    // process
                    Debug.Log("--- Frame " + frame + " at " + clip.time);
                    Mesh bmesh = new Mesh();
                    smr.BakeMesh(bmesh);
                    Debug.Log("### Vector " + bmesh.vertices[0].x + "  " + bmesh.vertices[0].y + "  " + bmesh.vertices[0].z + "  ");

                    // Last frame?
                    if (clip.time == clip.length) break;
                    // move to the next frame
                    clip.time = (float)(frame) / 60.0f;
                    frame ++;
                }
                //
            }
        }
    }
[...]
}

It looks like I can’t apply an animation to my skinned mesh and every frame I have the same result. Am I doing something wrong ?

Thank You!

1 Answer

1

To anyone who is still interested. Use:

    var clips = animator.runtimeAnimatorController.animationClips;
    foreach(var clip in clips)
                clip.SampleAnimation(gameObject, time)