Saturday, 30 July 2011

Python Scripting Distraction

Early on this week I received an e-mail from one of the lead developers for Blender asking me to test the latest pre-release Official FBX exporter with XNA.  He had done some work recently using bits of my XNA specific exporter and wanted to know if the official one was now compatible with XNA.

It was not compatible but I had always intended to merge the two in to one unified exporter so I spent the week doing that.

If you are interested all the source code and lots of notes are available by following these two links:

Its done now and the patch has been accepted.  The next release of Blender (2.59) will only need one FBX exporter and XNA will be supported by that official Blender FBX exporter.

It is not quite as user friendly as my current scripts because there are several tick boxes to select to output in the correct format for XNA. 

There is a convenient 'XNA Strict Options' tick box which forces the options to be compatible with XNA.  It is not required but is handy.

If you select the options manually the following are required:
  • Set the scale to 1.0 (which is is by default.)
  • Use the 'Rotate Animation Fix' (essential otherwise animations are a mess!)
  • Turn off, Empty, Camera and Lamp (makes the file size smaller)
  • Turn off smoothing (might not be necessary but I have not tested that yet.)
  • Do not include edges (makes the file smaller and can avoid some import errors)
  • Turn off optimized keyframes (not essential but might remove a duplicate keyframe that was added deliberately!)
  • Do not include default take (can be tricky to merge animations if they are all named 'Default_Take'!)
  • Select the 'Strip Path' mode so the uv textures use the same folder as the FBX file. (easier to manage the files.)
  • Enable Armature included as bone.  (This is the most important option to select.)

Further instructions are on the Blender Wiki:
I'm now going back to my .NET C# code.  So after spending days working in Python and remembering not to end each line with a semi-colon, I now have to remember to put the semi-colon in!

I much prefer C# with the structured code, type explicit variables (I avoid 'var' in .NET) and most importantly Visual Studio's auto complete and syntax suggestions all built in.  Happy coding.

Thursday, 28 July 2011

What Can My Game Already Do

An article by Nick Gravelyn over on his blog inspired me to think about what I have done.  Theirs shows a screen shot of what a team of three achieved on their game engine in 6 months.

I've been working on mine for over two years on my own and I'm pleased to say my game can already do a lot, in fact code wise there can't be much more to add.  I hope!

This list of features is a reminder to me of what I have achieved:
  • Walk round a 3D world
  • - Jump
  • - Spectate
  • First person controls
  • Over the shoulder view of yourself (more fiddly than it sounds)
  • Animated
  • - Blend animations
  • - Merge in arm movement to follow which way the player is looking
  • - Hold attachments that move with whatever they are attached to
  • - Shared animation files (and a way to get them in to the pipeline.)
  • Collide with characters and structures
  • Terrain and game editor
  • - Change heights
  • - Change textures
  • Add and remove:
  • - models
  • - triggers
  • - particle effects
  • - goals
  • - trigger goal success
  • - trigger add a new goal
  • - trigger spawn player
  • - trigger spawn non-players
  • - trigger particle effects
  • - Waypoints for AI pathfinding
  • - Spawnpoints
  • Lighting
  • Shadows (not a trivial task, many many months spent on this)
  • Full menu system
  • - Select which map to play
  • - Customise the player character with hats etc.
  • - Load and save character choices
  • - Change music and effect volumes
  • - In game pause menu, resume or exit
  • - Display goals outstanding and completed
  • Select weapons
  • Shoot weapons
  • Bullet trails
  • Bullet impact decals (instanced)
  • Bullet impact effect, debris and smoke
  • Muzzle flash
  • Most things have sound effects
  • Drop weapons
  • Pickup weapons
  • Pickup ammunition
  • Head Up Display (HUD)
  • - Weapon sights
  • - Sniper sights
  • - Zoom in
  • - Display ammo as used
  • - Compass
  • - Radar showing friend and enemy positions if close
  • Non-Player AI
  • - Pathfinding
  • - Select a target if in range
  • - Follow a player (target)
  • and I'm sure there's more...

All the in game stuff runs on the Xbox with a development version running on the PC as well.

In addition to all that I had to write a Python script for Blender to be able to export models from Blender to XNA and as a short distraction I am currently trying to unify that with the built in FBX exported in Blender so that in future one exporter works with everything!

I'm still a long way from finishing the game though.  That is because most of the above use placeholder graphics and so I am now working to create the finished 3D models to go in the game.  Then I will post a video to really show off :-)

I'd like to thank all those people on various forums that have helped along the way.

Sunday, 10 July 2011

Before Animating With Blender

Before you can create animations with Blender you must setup your model the right way.  There are plenty of tutorials on the Internet that will explain this as part of 'how to animate with Blender' but it is easy to miss these important steps.  This is a common reason why people have trouble getting animations to export properly for use in XNA.

The short summary is:
  • Create your model mesh object
  • Add an armature (skeleton)
  • Tell Blender that you want to use that armature
  • Make that armature the parent for each mesh that you want to animate.
  • Assign bone weights to the mesh by weight painting or adding bone vertex groups.
  • Create animations

The initial steps must all be done BEFORE you start creating the animations otherwise you might have problems exporting those animations.  The animations still work in Blender but it may be impossible to export them and you will have to recreate the animations!


An armature is the skeleton used to pose the model.  I won't go in to detail here about that as this article is about the prerequisites of animations not the animations themselves.

Create the skeleton by adding bones.

Edit: 2 Sept 2015
I came across a very interesting auto-rigger called Rigify.  Worth a look, see the following tutorials:
Note that you need to enable IK (Inverse Kinematics) on both the arms and the legs before the various controls act as you might expect.

Naming Objects

When you name any object especially bones it is worth avoiding the decimal point '.' (period, dot or full stop.)

Blender is happy with a full stop but the FBX exporter will rename it to an underscore '_'.  For example, 'Arm.R' becomes 'Arm_R' in the FBX file.

More Than One Way

This is where it starts to get complicated because there are multiple ways to do the same thing.   I will explain, what I call, the manual process first and then mention the shortcut method at the end.


Blender needs to know which armature is used with which object.  Simply select the object mesh you want to animate and add an armature modifier with the desired armature.

This is done in Object mode.  You do NOT need to apply the modifier.  It is just used like a note to Blender so it knows what links to what.

It is common to name the Armature 'Armature' and that can be a bit confusing because that is also the name of the modifier to add.  I tend to rename my armatures but as most new users leave the name as 'Armature' I have done that for this screenshot.

The modifier panel is on the right and the name of the skeleton is shown in the 'Object:' box.  In this case it is called 'Armature' and the mesh to which it is being added is called 'Cube'.


For animations to export correctly it is also necessary for the armature to be set as the parent for any object mesh that you want to animate. 

Initially the mesh will not have a parent.

Again, in object mode with the object mesh selected.  Go to the Relations panel and change the parent to the name of the armature you want to use.

Same example as above, the object is called 'Cube' and the armature is called 'Armature'.

Weight Paint

When you animate a model with bones you need some way to tell the model which vertices to deform depending on which bones you move.    That is called assigning bone weights.  The techniques for doing that are way beyond this summary post.  There are some links to articles about how to assign weights on my tutorials page:

I find I keep adjusting the bone weights, even after creating the animations, to improve the results.  For complex models I find that manually assigning the vertices to vertex groups is the more reliable method rather than using the pretty paint brush style Weight Paint method.

Shortcut Method

The above methods of 'Add the modifier', 'Parent the armature' and Weight paint' can be done in one go using the Parent function. 

In Object Mode, select the Object you want to animate, shift-select the armature and press Ctrl-P to parent the Armature to the Object.  Select one of the weight paint methods from the list that is displayed and job done.

I find this useful for getting started or for very simple models but invariably I have to adjust the weight painting.

Whatever method you use you can go on to create animations.

Create Animations

With the armature selected you can change to pose mode and create animations.  These must be saved as actions using keyframes.   Press 'I' and from the popup menu select 'LocRotScale' to save each keyframe.  

Use the ActionEditor view to see and edit a list of the keyframes saved. 

The Action Editor is often hidden because it is on a separate menu on a toolbar.  Change the option to Action Editor to see the list and change animations.

With the above settings done first it should be possible to export the animations for use in XNA. 


I have a another post about that:

Multiple animations in one FBX file is possible with Windows Phone SDK v7.1 but there might be a problem:

And the Blender Wiki instructions:


Discussion thread on the XNA forums:


I'd like to thank Ernest Poletaev for pointing out that the mesh had to be parented to the armature and reminding me to include weight painting in the list.

Thanks to Gary Haussmann for pointing out that decimal points will be replaced by the exporter.

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)) *
  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:

You can download just the XNA Skinned Model Processor with corrected rotation from:

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:
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:

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:
This script is NOT shipped with Blender and must be downloaded and installed.

Download an XNA 3.1 compatible exporter from:

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


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:

Tuesday, 5 July 2011

Vertex Information Missing

When loading my new character I have been getting intermittent errors or warnings.  The model works fine in game but the pipeline either throws an exception or puts up a warning about not all vertices having UV mapping information.

I had been working on the assumption that I have a vertex missing from the UV map.  I did some tests in Blender but I could not get any test model to have un-mapped faces or vertices.  However after some hunting through the text of the FBX file I eventually found a PolygonVertexIndex group which was formed of only two points.

Faces need at least three points because Blender uses either quads or triangles.  So two points must be wrong.

The clever ones among you will immediately think that this is an edge.  Well done you'd be right.  Edges cannot be UV mapped on their own and therefore they cause the warning when loading the file!

Finding Orphan Vertices or Edges

Now the tricky bit!  How do I find one lone edge out of over 8000 edges!

With a bit of trial and error I was able to select all (A) the faces in face select mode and hide (H) all the faces.

I swapped to edge select mode in order to find the edge.  I had to select all (A) several times before I could even see the tiny speck of orange flash on and off.

Once found I added it to its own vertex group so I could easily select it again.  I can now delete this edge and export the FBX again.


I've seen reference to a Clean Meshes tool with a Fix Edges option which does the above but I cannot find it for Blender 2.5.  If anyone knows if its available please post a comment.  Thanks


An orphan edge or vertex is one that does not form part of a face.

Monday, 4 July 2011

New Character

When I looked back I've been working on this guy for nearly a year on and off.  The model was a birthday present this time last year.

At last he is in the game.  I still have some tweaking to do to get some of the animations to line up with the in game motion but the bulk of the work is done.  The whole model also appears a bit darker than I would like with the in game lighting so I will probably adjust the texture colouring to match the rest of the models.

The story, still evolving, is that alien technological artifacts are being found in archaelogical sites throughout the galaxy.  Our hero is an adventurer helping to find the sites containing this alien tech.  Hired for missions by whatever galactic corporation most wants his help.  Eventually there will also be a choice of a female counterpart.

Replacing this character means that I no longer have to use placeholder models and from now on all screenshots can be in game artwork.