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: