Wednesday, May 15, 2013

Broken FBX animations with XNA importer (FIX)

We successfully replaced XNA in our engine and we are excited by performance speedup and count of bugs we finally fixed with this step. SharpDX is really great library.

The last piece of code where we still use XNA is our content processor pipeline. There is no satisfying replacement for FBX import, although some appears to be already on the way (https://github.com/assimp/assimp)

As it is a good tradition in XNA, there is a couple of bugs. And as it is a good tradition in Microsoft, nobody cares. This time we bumped into annoying bug with importing animations from FBX file. This is done through XNA FBXImporter class, which is built over FBX SDK from 2009. Hard to say if bug is caused by Autodesk's SDK, or Microsoft importer implementation, but importer always fails to import animation from first node in hiearchy. Really shame that Microsoft haven't published XNA source code, rebuild of FBX importer with latest FBX SDK and fix of this issue would be a few minutes task.

My idea is that Autodesk made a little mess with his "imaginary" FBX root node in its SDK, which is created on the fly and has nothing similar with FBX file content, only keeps root nodes from the file as children. FBX importer decided to not import "first" node, because that is anyway just fake, and always skips it. But unfortunatelly, there is a bug somewhere and importer skips valid node.

We had a hard times to even find out what's going on, and also finding a solution to this problem. Luckily, there is a workaround which works, at least for us, and you have to pray to be working also for you. Or you will be more lucky and in the time you are reading this article, there will be complete implementation of Assimp .NET importer for FBX.

So what to do with your animation to be correctly imported with XNA FBX importer? You always have to add fake root node to this animation. It can be achieved by Dummy object in 3DS Max.


Put Dummy into scene with your animation. Next step is to set Dummy transformation exactly same, as it is at first bone/node at first key. You can do this by using Select by name and Transform Type-In dialogs (they appears after right clicking on Select and Move or Rotate and Move tool buttons.


Note that dummy can be inserted as "Freezed", so you have to unfreeze it in the first time.

After the Dummy has correct transformation, you have to set it up as a parent of your animation. Do this in schematic view.


Correct hierarchy should appear like this:


With dummy parent, FBX importer skips our "dummy" and correctly process all underlying animations. Voila!