Access animated bone data from script

Looking for form general guidance here. I’m taking my first steps into Unity in what’s probably the worst place to start: writing an exporter. So far I’ve got a nice little static mesh export script (C#, based on the OBJ export) and have started in on trying to export animations for skinned meshes. But while I can easily see where the bindpose matrices are stored and can find what looks like the current animation frame bones (Transforms) pretty quickly, I’m at a bit of a loss on how the animations actually work. I was expecting a traditional system that stored keyframe skeletons and interpolated between them but the only information I can find in the documentation is animation curves, which appear to just transition a single numeric property from value A to value B.

Maybe that’s how the skeletal animations are done too, though that seems like a weird abstraction to me. It also seems like I could probably access the animation data as bones directly if I was able to apply the animation to a mesh and step through it manually, but I don’t see any mechanism for doing that. In any case, can anyone give me some pointers on how skeletal animations are actually implemented in Unity, and if there’s any examples on how to get at that data?

Like in 3ds Max, Maya, and most other common 3D animation software, animation in Unity is stored as a set of animation curves. Each curve controls one variable over time (like you say). A curve could have two keys and have the value go linearly “from A to B” but it can also have any other number of keys and the segments in between keys use bezier interpolation and can thus be curved if desired.

So for example, a Transform has 3 curves controlling the local position (LocalPosition.x, LocalPosition.y, LocalPosition.z). It also has 4 curves controlling the local rotation Quaternion and 3 curves controlling the local scale.

Or rather, it can have, but doesn’t necessarily. For example, character animation usually only has curves for rotation of each bone; not for position and scale since it usually doesn’t change. The exception is the root bone which has the position animated too.

With the curve approach different curves can have keys at different times, so one bone with high frequency motion could have more keys than a different bone with more simple motion. This is efficient, both with regard to file size and computations, and it also makes it more intuitive to edit motion since different parts of the body/object can be animated independently if desired.

A good way to gain understanding of the Animation System in Unity is to read through the guide to the Animation View which let you create and edit animations directly inside Unity and see all the curves.

A good starting point in the scripting reference for getting and manipulating curves is the AnimationUtility class.

There’s also API that will let you apply an animation to a mesh. Have a look at GameObject.SampleAnimation().