Rigging Demo

A little demo of the character rig and the springboard. Enjoy!

Blender to Unity Workflow

Ready to dive into some code? Here are a few notes on how to set up Blender and Unity to get the ideal workflow, and what to be aware of when rigging in Blender for later use in Unity.

Make Unity use Blenders new FBX exporter

This can be done by editing the Tools/Unity-BlenderToFBX.py file in the Unity installation folder (in osX right click on the Unity.app, then select show package content to find it). Below is the file edited to use Blenders new binary FBX exporter. The lines added or changed are marked with #changed to use binary fbx exporter. Blender version 7.1 or above is needed for this to work.

blender249 = True

try: import Blender
except:
	blender249 = False
	import bpy	

if blender249:
	try: import export_fbx
	except:
		print('error: export_fbx not found.')
		Blender.Quit()
else:
	try:
		import io_scene_fbx.export_fbx
		import io_scene_fbx.export_fbx_bin #changed to use binary fbx exporter

	except:
		print('error: io_scene_fbx.export_fbx not found.')
		# This might need to be bpy.Quit()

		raise

# Find the Blender output file

import os
outfile = os.getenv("UNITY_BLENDER_EXPORTER_OUTPUT_FILE")

# Do the conversion

print("Starting blender to FBX conversion " + outfile)

if blender249:
	mtx4_x90n = Blender.Mathutils.RotationMatrix(-90, 4, 'x')
	export_fbx.write(outfile,
		EXP_OBS_SELECTED=False,
		EXP_MESH=True,
		EXP_MESH_APPLY_MOD=True,
		EXP_MESH_HQ_NORMALS=True,
		EXP_ARMATURE=True,
		EXP_LAMP=True,
		EXP_CAMERA=True,
		EXP_EMPTY=True,
		EXP_IMAGE_COPY=False,
		ANIM_ENABLE=True,
		ANIM_OPTIMIZE=False,
		ANIM_ACTION_ALL=True,
		GLOBAL_MATRIX=mtx4_x90n)
else:
	# blender 2.58 or newer

	import math
	from mathutils import Matrix
	# -90 degrees

	mtx4_x90n = Matrix.Rotation(-math.pi / 2.0, 4, 'X')
	
	print("moo")
	
	class FakeOp:
		def report(self, tp, msg):
			print("%s: %s" % (tp, msg))
	
	exportObjects = ['ARMATURE', 'EMPTY', 'MESH']
		
	minorVersion = bpy.app.version[1];	
	if minorVersion <= 58:
		# 2.58

		io_scene_fbx.export_fbx.save(FakeOp(), bpy.context, filepath=outfile,
			global_matrix=mtx4_x90n,
			use_selection=False,
			object_types=exportObjects,
			mesh_apply_modifiers=True,
			ANIM_ENABLE=True,
			ANIM_OPTIMIZE=False,
			ANIM_OPTIMIZE_PRECISSION=6,
			ANIM_ACTION_ALL=True,
			batch_mode='OFF',	
			BATCH_OWN_DIR=False)
	else:
		# 2.59 and later

		kwargs = io_scene_fbx.export_fbx_bin.defaults_unity3d() #changed to use binary fbx exporter

		io_scene_fbx.export_fbx.save(FakeOp(), bpy.context, filepath=outfile, **kwargs)
	# HQ normals are not supported in the current exporter


print("Finished blender to FBX conversion " + outfile)

Change default FBX export options in Blender

The defaults for Blenders binary fbx export can be found in the file export_fbx_bin.py located in the folder scripts/addons/io_scene_fbx. Search for defaults_unity3d to find the relevant code. In version 2.71 this is what it looks like:

# defaults for applications, currently only unity but could add others.

def defaults_unity3d():
    return {
        "global_matrix": Matrix.Rotation(-math.pi / 2.0, 4, 'X'),
        "use_selection": False,
        "object_types": {'ARMATURE', 'EMPTY', 'MESH'},
        "use_mesh_modifiers": True,
        #"use_armature_deform_only": True,

        "bake_anim": True,
        #"use_anim_optimize": False,

        #"use_anim_action_all": True,

        "batch_mode": 'OFF',
        # Should really be True, but it can cause problems if a model is already in a scene or prefab

        # with the old transforms.

        "bake_space_transform": False,
    }

To make it work similarly to the old ASCII FBX exporter, but with space transform baking, here is what my modified settings look like.

# defaults for applications, currently only unity but could add others.

def defaults_unity3d():
    return {
        "global_matrix": Matrix.Rotation(-math.pi / 2.0, 4, 'X'),
        "use_selection": False,
        "object_types": {'ARMATURE', 'EMPTY', 'MESH'},
        "use_mesh_modifiers": True,
        "use_armature_deform_only": True,
        "bake_anim": True,
        "use_anim_optimize": False, #let Unity do animation optimization

        "use_anim_action_all": True, #every action is imported as a separate animation clip

        "batch_mode": 'OFF',
        # Should really be True, but it can cause problems if a model is already in a scene or prefab

        # with the old transforms.

        "bake_space_transform": True,
    }

Rigging in Blender for Unity

To make the animations move the right way in Unity rotate root bone 90° around Z axis in Blender in edit mode.

root bone rotation

Now the model isn’t rotated when an animation starts, and the red arrow points the right way in the animation preview.

header unity mecanim


To avoid importing unused bones uncheck the deforming checkbox in Blender for all the bones in the rig that don’t deform the mesh and aren’t needed in realtime. Also make the deforming bones a separate hyrarchie. Avoid deforming bones being children of non deforming bones, otherwise the non deforming parent will be exported to Unity.

To avoid importing custom bone shapes that are used only by the rig they can be saved in a separate blend file outside of the assets folder, and then linked in where needed.

An other way to have things in the .blend file that should not be imported into Unity is to have them in a separate blender scene. Only the active scene is exported.

First Pose

David Pose

Test pose of the first athlete. Almost as cool as his reference.

Model Stats

The model is made of 918 triangles. Posing was done with 33 deforming bones, and a very practical rig including handy features like:

  • IK / FK switch
  • FK to IK snap button
  • Pivot slide along spine
  • Foot roll
  • Arm socket switch
  • Custom keying set

Thanks to Nathan Vegdahl for his superb Blender rigging tutorial series Humane Rigging!

And this rig also exports cleanly to Unity3D, without cluttering the scene with unnecesary transforms or custom bone shapes!