A better Blender to Unity 3D importer

I’ve written an importer script for Blender files in Unity 3D that imports the model correctly without forced rotations. Transforms, hierarchies, animations, etc are fully preserved when loading a .blend file into Unity 3D with this importer.

https://github.com/EdyJ/blender-to-unity3d-importer

I consider Blender the “Unity 3D” of the 3D modeling tools. Everything is exquisitely arranged and well-structured. When you’re a bit used to it you can find what you’re looking for intuitively, while everything else can be easily ignored.

The problem

Blender’s coordinate system is right-handed (+Z up) while Unity’s is left-handed (+Y up). The built-in Blender importer in Unity 3D just rotates the model -90º around the X axis effectively turning the original Z into Y. As result, imported GameObjects are given a rotation (-90,0,0). While this is ok for static and inanimated models, it can be a pain for models that need to be rotated in-game, or use orientation-based components such as WheelColliders. Specially cumbersome is rotation with scripting, as one needs to have the X-90 rotation in consideration.

Issues with the default importer are:

  • GameObjects are imposed a X-90 rotation.
  • Forward orientation might point -Z (backwards), should be +Z.
  • As result of the rotation X-90, components added to the GameObjects will use that orientation. In the above picture a WheelCollider added to pivot_l1 points horizontally instead of vertically.

The solution

My script imports the Blender file into Unity 3D as expected:

  • Complete hierarchies are preserved with their correct transforms (position, rotation, scale).
  • No artificial rotations are imposed in the imported model: if a rotation in Blender was [0,0,0] the rotation in Unity will be [0,0,0].
  • Optionally the forward orientation can be reversed for matching the Unity’s convention (+Z).
  • Keyframe-based animations in Blender are correctly converted to animation clips in Unity. The animations will look the same in both Blender and Unity.
No forced rotations in the GameObjects. "Forward" points +Z.

No forced rotations in the GameObjects. “Forward” points +Z.

Bonus

In addition to merely import the file, the importer offers a couple of valuable tools that can be used with any 3D model format, not only Blender.

  • Per-object commands: Include specific keywords in the name of any object in the 3D file and some actions will be performed with it. Actual actions include removing the MeshRenderer and adding a MeshCollider.
  • Mesh instance optimization: Removes identical meshes and replaces duplicates with references to an unique copy. Works with any 3D file format, not only Blender.
  • New menu option in Unity “GameObject > Optimize mesh instances in this scene”: Launches the instance optimization in the loaded scene, replacing the duplicated objects with references to an unique copy. This effectively reduces the size of the builds, as only the referenced meshes are packaged. Non-referenced meshes are excluded from the build even if they come from the same file.

Go to the GigHub page for usage instructions.

Exporting to FBX

Sometimes a .blend file might take a long time to get imported into Unity 3D (several minutes). The solution is to export the model to FBX and import this file into Unity instead of the .blend file. When exporting the file to FBX ensure to leave the default options:

Blender FBX Export options

 

You can disable (shift-click) the Camera and Lamp objects as they are imported as empty objects and have no utility in Unity 3D.

In Unity 3D add [importer.forcefix] to the name of the fbx file for invoking the importer.

Addendum

In my opinion there’s only one annoyance in Blender that should be addressed in order to make Blender 100% perfect for Unity 3D: disabling or bypassing the use of the inverse transform in parenting, so the parenthood is established more like Unity 3D actually does.

By default, when two objects are parented in Blender (Ctrl-P) there are two transforms involved: direct and inverse. Read a good clarification here. Unity 3D imports the direct transform only, so when a child object has both direct and inverse transforms, then the imported GameObject won’t surely look as expected.

Actual workarounds are:

  • Parenting without inverse: use Shift-Ctrl-P for parenting instead of Ctrl-P (documentation here). The problem here is that the actual relative position/rotation/scale of the object with regards to the parent is not preserved. The child’s local transform may need to be readjusted. When rigging a model, parenting without inverse should be used prior to actually editing the child’s transform to its final relative position/rotation/scale.
  • Clear inverse transform: Alt-P offers this option. The problem is the same as above: actual relative transform is not preserved so the child object may need to be re-arranged.

The solution would be a new parenting option in Blender (maybe Shift-Alt-P?) that works like in Unity 3D: the child should receive a local (direct) transform that matches the actual relative position, rotation and scale with regards to the parent at the time of parenting, without involving an inverse transform. Just as Unity 3D does when parenting in the Hierarchy panel.

Anyone with experience in Blender add-ons out there? 🙂