Monday, April 29, 2013

Exporting animations from Blender to DirectX

It's been a while since my last post... Owh Gooood! The most time and energy consuming thing is behind me. Skeletal animations has been written and I'm really happy about it. In fact, I was swearing at them all the week, the more technologies you use - the more potential bugs you get. What a discovery!

Let me share a few critical problems I got on my way. Maybe it'll save someone's time in the future ;)
It's not a tutorial or step by step instruction, only hints.
  1. Exporting skeletal animation from Blender (v2.65) to a .x file
    • Only active ArmatureAction strip is exported, I was trying with NlaTrack but failed
    • Remember to triangulate the mesh
    • Keyframes on frames <=0 aren't exported
    • Bones have to be in rest pose before running the exporter (but not in Rest Position from Skeleton tab in Object Data panel, I prefer to make a special "rest keyframe" in frame 0)
    • You need to select armature and object first, then launch the .x exporter
    • My exporter options checked:
      • System: Right-Handed (my Assimp loader is configured to automatically change coordinated to Left-Handed)
      • Apply Modifiers
      • Export Textures
      • Export Armatures
      • Animations: Keyframes Only (I guess you know why)
      • Export: Selected Objects

  2. Importing a .x file with Assimp library
    Assimp interface is very neat, so there probably won't be any problems with it. The main I had, was with this function:
    Despite of a fabulous name - it just converts Assimp matrix to XNA matrix. They're both structures with 16 floats in them, and XMFLOAT4X4 has a constructor that enables to build it from a float pointer.
    Tracking this error was a really funny thing, you know where was the problem? The returned XMFLOAT4X4 matrix was transposed... Here's the fix:

  3. Some code issues:
    • Watch out when you want to have a structure like a list of keyframes containing offsets of every bone in current pose. When you're importing, you have a list of channels (corresponding to bones) containing offsets for every frame. It's easy to write some dirty code here.
    • Take a special care for quaternions. Sometimes they're stored as (x, y, z, w) and sometimes as (w, x, y, z)
    • IMHO pointers are much better than lists when managing a bone and animation structure
    • When stuck and don't know what to do, start small. Make a super simple animation with a 3 bones and 6 vertices. Debug all transformations and properties, you'll figure out what happens for sure :)

No comments:

Post a Comment