Animation import: non- rest-relative pose

I’m writing an importer for an animation format that, for each bone, stores keyframe poses relative to the parent bone (I think). As I understand it, this is different from the way Blender does skeletal animation, where the pose bone transformation is relative to the rest position of that bone. Unfortunately, I can’t think of a nice way of going from one format to another. However, like Blender, the format only stores values that have changed each keyframe (like “x position at frame 5=1.2, y rotation at frame 7=92, …”), instead of the whole pose, so I don’t have a full picture for each keyframe.

My temporary solution is to completely clear the rest pose. That means all of the bones have head/tail at 0,0,0 rotation 0, and the mesh is all clustered around that point. The animation then correctly sets the bone positions and fully fleshes out the pose. Unfortunately that will look really bad if an artist wants to make an animation and expects the “reset pose” button to work, or if they want to edit the mesh only to switch into edit mode and see a pile of vertices all scrunched up into a ball on the floor.

I think the “correct” way of doing this would be:

  • For each keyframe point (“x position at frame 5=1.2”), evaluate the rest of the pose at that point in time, to get the whole picture (x,y,z position, x,y,z rotation, x,y,z scale). That pose would be in old, “absolute” space, and hopefully Blender has facilities to help evaluate curves correctly.
  • Compile the pos/rot/scale into a 4x4 transformation matrix.
  • Multiply by the inverse of the rest pose (again, compose from head pos/tail pos/spin if needed). Now it’s in “rest-relative” native Blender space.
  • Decompose that matrix into pos/rot/scale
  • Hope and pray that Blender decomposed it sanely
  • Use the right piece of that transform as the new keyframe value (“x position at frame 5=54.2”)

I think it might work, but it’s a lot of effort and will probably result a lot of frustration debugging. An alternate approach I think would be to skip steps 4, 5, and 6, and just key with a full transformation matrix. Unfortunately, that might interpolate wrong, and there would be way too much extra data that an animator would have to deal with, since all key data would be there for every frame, not to mention general chaos in the f-curve viewer.

Do any Blender Python or general 3D developers have any experience with things like this? I’d appreciate any advice. Thanks!

Hi impiaa :wink:

The way to get the full pose is to set the current frame of the scene and then just going through all the bones of the armature. You can repeat that for every frame (which you would call “sampling” the animation).

Maybe this code will help a bit: https://github.com/Squareys/Blender-OpenGEX/blob/master/src/io_scene_ogex/OpenGexExporter.py#L389

I think it might work, but it’s a lot of effort and will probably result a lot of frustration debugging

Do you have a bpy module compiled? It allows you to test export without starting blender and especially using the python debugger (from within pycharm for example) to step through the code and inspect variables and such.

I hope I understood correctly and this helps somehow :wink:

Regards, Squareys.

I actually got it (mostly) working. The “correct” method I proposed wasn’t too hard to write and worked pretty much on the first try. I didn’t want to set up a scene in Blender just to figure out animated poses so I have it evaluate the animation curve before importing anything. I only implemented linear interpolation, though, so if I want more accuracy, I’ll either need to implement bezier interpolation or have Blender evaluate the animation like you suggest. A bigger issue though is that, post-transformation, the bezier curve animation handles are all incorrect. Now I’m trying to figure out if there’s a way that I can transform the keyframes without evaluating the whole animation, since that would mean I could transform the bezier handles as well and it would be much more efficient.