Wednesday 6 July 2011

Exporting Animated Models From Blender To XNA

If you want to develop a 3D game you will probably spend as much time creating the content as writing code. I am not an artist but I have found that Blender is a fully featured 3D editor that with a few weeks practice I was able to create models that I could use with XNA. The pipeline to get animated models from Blender to XNA needs to be followed closely but once understood is as easy as any other tool.

XNA and Blender are still being developed, improved and updated. Changes to both platforms have affected the methods required to have a successful pipeline. Follow the specific instructions for the XNA and Blender versions you are using.

Instructions for creating models that can be exported to XNA

Getting models to look right when they are imported in to XNA can cause a lot of confusion.  Like many things it is not difficult when you know how. 

There are several techniques that look fine from within Blender but which either have to be avoided or adhered to if you want to use that model in XNA.
This is a summary of the most common things that catch people out:
  • All the model objects (meshes) and the armature must be centred at the same location, ideally zero (X = 0.0, Y = 0.0, Z = 0.0 in the Object properties.) Set the locations to zero in Object mode and make all changes in EDIT mode.
  • All the model objects must have a scale of 1.0 (one.) Set all the scales to 1.0 in Object mode then do all changes in EDIT mode.
  • The model objects must not use rotation. Set all the rotations to 0.0 in Object mode then do all changes in EDIT mode.
  • Every vertex must be weight painted or added manually to a bone vertex group. Any loan vertex will cause an error when importing in to XNA. To check you have bone weights for all vertices pull the model about in POSE mode. Any un-weighted points will be left behind when posing the armature.
  • The XNA model class only supports UV wrapped textures. Blender's shading only work in Blender not in XNA. 
  • The FBX importer only support keyframe animations from Blender Actions and will not work with Blender's curves.
  • In XNA set the 'Content Processor' for the FBX model to 'SkinnedModelProcessor' or whatever your processor name is - this is the most common oversight.

To explain in more detail:

(Update Dec.2012 for Blender 2.6) The quick way to do this without changing the appearance of the model is to use the Apply menu in Object mode.  Select the object (mesh) and use CTRL+A to bring up a small menu and then L to move the location of the origin to zero, R to fix the rotation to where it is and S to fix the scale.

As a rule of thumb, once you have set all the Blender objects properties to a location and rotation of zero and a scale of one all the modelling will be done in Blender's EDIT mode.



Blender is easiest to use if models are created with up in the Z direction. XNA's default is that models use Y as the up direction! From trial and error I have found it is easier to rotate the model when imported in to XNA rather than trying to work in Blender the wrong way up! Animations do not rotate very well in Blender, usually resulting in a mess!   Using the rotation options in Blender's FBX exporter always results in a mess!



I have an XNA project with a content pipeline animation processor that includes a method for rotating 3D models while they are loaded including rotating their animations. Its only a few lines of code inserted at the correct point. 


public static void RotateAll(NodeContent node, 
                             float degX, 
                             float degY, 
                             float degZ)
{
  Matrix rotate = Matrix.Identity *
    Matrix.CreateRotationX(MathHelper.ToRadians(degX)) *
    Matrix.CreateRotationY(MathHelper.ToRadians(degY)) *
    Matrix.CreateRotationZ(MathHelper.ToRadians(degZ));
  MeshHelper.TransformScene(node, rotate);
}
  

Use a rotation of X = 90, Y = 0, Z = 180 to rotate from Blender to XNA during the pipeline processing. Download that project source code using a Subversion client to see how its done: http://code.google.com/p/3d-model-prep/

You can download just the XNA Skinned Model Processor with corrected rotation from:
http://code.google.com/p/3d-model-prep/downloads/list


There is one last thing to watch out for.  Poorly finished models with orphan vertices or edges are likely to cause errors or warnings during the import in to XNA.  This is a common result of preparing a model to be game ready especially when reducing the numbers of faces a model uses to improve performance.  I have a separate post on finding missing edges: http://blog.diabolicalgame.co.uk/2011/07/vertex-information-missing.html
This is not so important with the latest Blender FBX exporter because I have made that ignore lone vertices.


Setting up the animations


There are some prerequisites for creating animations and I have put those in another post:
http://blog.diabolicalgame.co.uk/2011/07/before-animating-with-blender.html



Exporting FBX Files

In versions of Blender prior to 2.59 the standard FBX export script that ships with Blender is NOT suitable for XNA. If you want to use XNA 4 please upgrade to at least 2.6x of Blender.  There are some older instructions at the end if you are still using XNA 3.

From Blender 2.59 onwards the official FBX exporter supports XNA.

Blender 2.59 and 2.6+ to XNA 4.0

To work with XNA you must be using a version of Blender greater than 2.59.  At the time of updating this page 2.60a is the latest stable version of Blender. 

The official Autodesk FBX exporter included with that version supports XNA.  The export script has general documentation on the Blender Wiki:

The above image shows the typical setting for use with XNA.  It is necessary to change the default settings when working with animations.  The default works for non-animated models but the rotation breaks animations.  The default options also include more information in the file than XNA can use.

As a quick way to export XNA compatible files there is a tick box that sets all the required options:


It is mainly the 'XNA Rotate Animation Hack' and the Path mode: 'Strip Path' that are needed.  This stops any rotation of the model and requires that all texture files are stored in the same folder as the FBX file.

The 'XNA Strict Options' is just a quick way to set the essential XNA options.  One tick and all the settings work.  You can still adjust some of the settings but it prevents you accidentally changing a necessary option.

Individual Animations
The Autodesk FBX importer shipped with XNA 4.0 introduced the limitation that only one animation can be loaded from an FBX file. The updated version with the Windows Phone 7.1 SDK tried to fix this but included a bug so the number of frames in all animations was the same as the first take!  

The Blender script not only has an option to output in a compatible format for XNA but also has an option to output 'All Actions' or if that is not selected, to output just the currently selected animation. 



Just having one animation in each FBX file is the solution that works best with the current (November 2011) version of XNA.





In POSE mode use the 'Action Editor' to select the animation to export.


Then export and make sure the option for 'All Actions' is off.



==



Blender 2.49b to XNA 3.1 or older [Archive]

At the time of writing the last version of the old series of Blender is 2.49b.  In order to use older versions of XNA you will probably need to use the older series of Blender but make sure it is version 2.49b. Earlier versions have issues. Specifically 2.49a has a fault preventing scripts from running! (Nov.2010)
Essential script
As mentioned above the standard FBX exporter will not work with XNA it is essential to use the following script or a variant of it:
http://wiki.blender.org/index.php/Extensions:2.4/Py/Scripts/Export/FBX-XNA
This script is NOT shipped with Blender and must be downloaded and installed.

Download an XNA 3.1 compatible exporter from:
http://www.discoverthat.co.uk/games/blenderscripts/export_fbx_for_xna_v122a.py
or
http://www.triplebgames.com/export_fbx__for_xna.py

Copy the script in to the Blender script folder.  In Windows Blender 2.49b scripts are stored in:
%USERPROFILE%\AppData\[Roaming]\Blender Foundation\Blender\.blender\scripts

Installation of Blender 2.4 Python scripts
The scripting language used by Blender is Python. In the older versions of Blender up to and including 2.49b you'll need to download and install the full version of the Python scripting language 'Python' to use these scripts. You must install the matching version for the version of Blender you have. It tells you in the console Window when you start Blender. For example, Blender 2.48 required version 2.5 of Python (the sub version is not critical so version 2.5.2 and version 2.5.4 both work but version 2.6 would not.) Get from Python.org.

==



Blender 2.56 to 2.58 [Archive]

These early 2.5x versions shipped with a separate compatible exporter.  I strongly recommend using the newer 2.59 version but the instructions for the older version are still on the Blender Wiki:

16 comments:

Ernest Poletaev said...

Unforunately i cannot get export animation from blender, i checked any steps that You provide, but exported model no have any animations. Can You point where i'm wrong? I use Blender 2.58a with bundled xna export plugin and this model for test.

John C Brown said...

I've answered with a new post:
http://blog.diabolicalgame.co.uk/2011/07/before-animating-with-blender.html

Christer Nyberg said...

The refresh of Game Studio 4 contained an improvment in the FBX importer that removed the limitation of one animation per file. I've run into problems with all my animation takes having the same, incorrect, duration. I'm going to experiment further, but have you had a look at the refresh release?

John C Brown said...

That sounds like a problem! I don't use the phone tools and as far as I know the refresh is still beta and only available with the phone tools. Its probably a good idea to report the problem on the forums and on connect so it can be fixed before the final release.
Regards.

Ian said...

Hi, it's great that this is now bundled with blender. I tried with one of the standard yofrankie characters though and had issues. I asked about them in the thread below ... just posting here incase you have an idea and don't read the forum below.

http://blenderartists.org/forum/showthread.php?232206-Using-.fbx-exporter-for-XNA&p=1956065#post1956065

Anonymous said...

This post is the best guide ever for your entry into Blender/XNA. Thank you, I'm really really really grateful to you right now ^.^

Anonymous said...

I love your what you're doing for the xna/blender community. Your work is helpful to more people than you think. Good luck with your project. I'll credit you in my xblig if you don't mind. :)

John C Brown said...

Thank you. Very happy for you to credit me.

Unknown said...

Hi John, I was wondering why the Blender FBX exporter apparently samples all keyframes and inserts them into every frame between the first and last keyframe of each action. Is this because Blender is trying to preserve the interpolation properties in the exported file? Come to think of it, I don't think the XNA 4.0 SkinningSample uses any kind of interpolation so the export behavior is desirable in that case...

John C Brown said...

I have never questioned why Blender samples all frames not just the keyframes because as you say that is what is needed by the Skinning Sample.

It is the same behaviour expected by other programmes and games that use the same exporter.

I was just pleased when I got it to work with XNA :-)

Justin S. said...

Trying this with the new graphics content pipeline in Visual Studio 2012, and I have found that I need to flip the Z axis (scale the Z-axis by -1) otherwise the model is inside out along that axis.

I have done this in two ways:
1) do this in the world matrix for the object
2) edit the export_fbx.py to include
mtx4_z90 = mtx4_z90 * Matrix.Scale(-1, 4, (0.0, 0.0, 1.0))
after the initialization of mtx4_z90.

Is this something you have experienced and do you think this is the correct solution?

John C Brown said...

I am not using Visual Studio 2012 so I was not aware the FBX importer had changed yet again!

I assume that this pipeline is not XNA but something else Microsoft have added?

If that transform works in the Blender exporter I would use that method.

Have you tried using the standard FBX exporter options without the XNA specific settings I had to add for XNA 4. Microsoft or Autodesk might have made the Importer more compatible.

I totally failed to work out what the correct transform was to get rotation to work with animations in the exporter so I am impressed if your transform does not mess up the animations.

Well done.

Regards

Anonymous said...

Great tutorial, congratulations!
But I'm having a problem, when I try to rotate a bone in xna it seems rotate about the tip of his root bone, I don't know what I'm doing wrong, because I've used a sample for xna and the sample model works perfectly, then I think my problem is when doing my model in blender. It seems a very simple problem, but I found nothing searching this, only changing the code but the code is working.
If you can answer i thank you.

John C Brown said...

Without knowing more it is difficult to tell where you need to change your code.

I found manipulating individual bones in code difficult so I use animation poses and blend between frames to control bone positions.

I suggest posting a question on the XNA forums. It is easier to include more information. Perhaps also add a link to the question as a comment here.

Regards

MilaMoO said...

I got multiple animations to work with XNA 4 Content Pipeline (SkinnedModelPipeline) and Monogame Win8 xaml. The Model has multiple animations from blender and can be played by calling Player.PlayClip("Clipname", )

The main issue that I had outside of following this tutorial was that I was not calling the PlayClip method. Looks like the SkinnedModel does not render unless animation is playing. The export was done from blender 2.68a with the built in FBX export.

3D facial animation software said...

Awesome!I love it. I've tried to use blender for about 5 times in about 5 years, haha, everytime I just leave it because I don't have time to understand that super weird UI and mouse way of working, this could really, really move a lot of people to the blender boat. thanks a lot!~ Diane E. Benson