Friday 26 November 2010

Blender 2.5 to XNA 4 animated model pipeline

See the future linked post for more up to date information:
http://blog.diabolicalgame.co.uk/2011/07/exporting-animated-models-from-blender.html

The following is a bit out of date. If you want to know how to get models from Blender to XNA read the above linked post not this one.

==

It's been a while since I posted because I have been struggling with a problem since I finished the conversion to XNA 4.

As mentioned in my previous post, animated models from Blender would no longer display properly in XNA.  If I had imported the animation in XNA 3.1 they would still work in 4.0 but when I tried to save new animations from the models using XNA 4, the animations were distorted.

I tried fixing the FBX exporter from Blender but the FBX file format is not documented making it difficult.  To cut a long story short I have ended up with a solution that uses separate files for the animations.

The model, bones and bind pose are loaded in to XNA using FBX.  Each animation is exported from Blender as a list of bone transform matrices.  Those can then be imported in to XNA and converted to the AnimationClip format used by the Microsoft Skinning Sample.  Edit:  I now have FBX working see my next post.

The process takes a few steps but works with the models I have been using.  It needs more testing with other models.


The package with several exporters for Blender 2.5 can be downloaded from:
http://code.google.com/p/blender-to-xna/
The most recent instructions are on the Blender Wiki:
http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/File_I-O/Blender-toXNA

The XNA sample and model viewer demonstrating how to import the animations can be downloaded from:
https://code.google.com/p/take-extractor/

Both are on Google Code, if the links do not initially work, wait an hour and try again.  Google appear to have frequent outages but none so far have lasted longer than an hour.
There are still several limitations with the pipeline some are the same limitations as with earlier versions of XNA and Blender:

- All parts (Blender Objects) of a model must have the same centre (Origin, ideally at Zero)
- All parts of the model must have a scale of 1.0
- Every vertex must be weight painted or added to the bone vertex group for animation.
- The model has to be loaded in XNA before the animations can be converted to AnimationClip format.

At the moment the animations cannot be rotated in the content pipeline! [Fixed, see future posts]  My next job is to work out the maths to rotate the animations as they are imported.

==

Please make sure you read my future posts. I have done a lot more work to get it all working properly.

11 comments:

Unknown said...

I am very interested in your work. I tried to export animations with blender 2.55 for import with XNA 4.0 without success.

Miraculously I found your work that I would finally solve my problem. However, after exporting a model and an animation with your python script, I can not view it with your tool 'Take extractor'.
Indeed the model is displayed but the tool says it has no animation:

Warning: Input File Does not Contain Any animation.
This Does not Have Any Model Takes!

Below is a zip file that contains the blender and fxb exported.

http://dl.free.fr/mx3hPnuyt

If you have an idea, I'm interested.

Thank you again for your work.

# Geff

John C Brown said...

Geff

I'm still working on the scripts they are changing daily at the moment. Please make sure you download the latest version. (But wait 10 minutes while I upload it!)

The instructions you are missing are that you have to load the animations separately from the model. This is a limitation of the XNA 4 importer not my sript.

The latest script has three options, model only, animations and full animated model.

If you use the model option it does not contain any animations.

For testing save the model fbx and the animations (or a full animated model) so you have two FBX files. Load the model (Load Animated Model) in TakeExtractor then look further down the menu you will see Load FBX animations, load the FBX file that contains an animation in it. You only get the first animations because of the XNA 4 importer!

If you export several files each with a different animation selected you can load them one at a time in to TakeExtractor.

Let me know how you get on. In a future release I'll type some more instructions and add to the project download.

Thank you for the feedback.

John C Brown said...

Quick instructions for use of the scripts are now on the Blender-to-XNA Wiki:
http://code.google.com/p/blender-to-xna/wiki/Instructions

John C Brown said...

Quick instructions for use of TakeExtractor are now on that project:

http://code.google.com/p/take-extractor/wiki/Instructions

John C Brown said...

Geff

I've tested your model and when I export it I also don't get any animations. The FBX file only contains the heading but no frames! I'll see if I can find out why.

John C Brown said...

Geff

I've only been working on keyframe animations. Your model uses curves. Unfortunately I have no idea how they work.

Is there a way to convert between curves and keyframes?

Unknown said...

Hi, thank you again for your help. As you guessed, this is my animation that is not correct (or can not be exported by your script).
So I decided to recreate this animation and it works.

Thank you very much.

I'll now be able to directly import the animations in my game much simply. Before I used Maya (Demo) which converts crapped FBX blender files to usable FBX files with XNA.

Your tools should be highlighted, because I'm sure we're not alone in having problems with Blender and XNA.
You could for example offer your python script on the blender wiki.

What is certain is that I'll keep an eye on your blog;)
# Geff

John C Brown said...

Geff

Thanks. It took me a while to work out how but I managed to add it to the Blender Wiki a couple of hours ago.

http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/File_I-O/Blender-toXNA

Regards

Harry H said...

I know this is uber late, but im late to the game and im learning. I am actually using Blender 2.58 and trying to export to 3.1 for class ( i know we are still using old xna) But is there is resolution for exporting from 2.58 to 3.1? I dont know!!???

As far as model rotation, I use

Matrix[] bones = animationPlayer.GetSkinTransforms();

than somewhere after that for controls
Matrix rotationY = Matrix.CreateRotationY(modelturn());
for (int i = 0; i < bones.length; i++)
bones[i] *= rotationY;

Sorry if my code is sloppy and slightly off, i am sleepy. essentially each bone is rotated at the same time. it works for me at least.

John C Brown said...

Harry

You can have Blender 2.49b installed at the same time as 2.58. Just open your blend file in the older version when you want to export to XNA 3.1 As far as I know the blend files are backwards compatible at the moment.

My newer posts in July 2011 include the 2.49b to XNA 3.1 instructions.

Rotation using a Matrix in game is not a problem. It's just one additional sum done every frame. Therefore I prefer to rotate the model in the pipeline. Which I now know how to do.

Regards

John C Brown said...

I keep the following post updated with as much information as I know about getting animated models from Blender to XNA.
http://blog.diabolicalgame.co.uk/2011/07/exporting-animated-models-from-blender.html