Export .x files with Blender

For some time it has been possible to export and import Direct3D .x meshes with Blender, thanks to the export script included. However, I sometimes needed more special behaviour and while I could have modified existing scripts, I decided to to try and write a new one. It had been some time since I last tried Blender's Python API, so I could probably learn a thing or two in the process. After some work and testing I have made the exporter available, check the link at the bottom of this page

The new exporter

First I am going to explain how to use it so you don't have to figure it out yourself. The export script should be placed in the Blender\.blender\scripts directory where all other scripts are located. Blender will then automatically add it to File->Export section. Next I will explain something about the interface. After clicking the script a new window opens (it is sometimes useful to split the screen to multiple windows and set one of them as a script window so that you can see the scene all the time). Now you can see some buttons which are also divided in categories, so let me explain them in more detail.

This section will determine what objects will be exported. Only one of these options can be selected.

All - This will export all "static" meshes in the current scene. All other objects are ignored.
Selected - This will export all selected meshes.
Animated - This will export a mesh and it's armature animations. Only the mesh and it's armature should be selected.

Exported data
This section controls what data is exported with meshes. You can easily discard data which you don't want to export. For example normals are sometimes calculated by the application which loads the data, so you can save space if you don't export certain data. Some data (materials, colors) saves index data to vertices, so even an innocent looking single material can increase the file size dramatically. After the first material size increase is minimal, however.

Normals - Exports normals.
Materials - Exports materials.
Colors - Exports vertex colors.
Texcoords - Exports vertex texture coordinates.

Misc options
Some additional options.

Flip y and z - Transforms data from right-handed (Blender, OpenGL) to left-handed (Direct3D) coordinate system.
Apply world - Apply world transformation to objects. Most of the time this should be left on.
Speed - Controls the speed of the animation.

Export... - Opens a file dialog asking for a file. After you have selected or typed in a new name click the Export-button to start the processing.
Exit - Exits the script.

General info

There are some differences between this script and Blender's one. This is usually slightly faster and produces smaller files. Blender's default exporter duplicates vertices, which means that a mesh with 21 000 vertices will take nearly seven seconds to export and produces a file with 91 000 vertices (nearly 8mb's). This script only takes 2-3 seconds, only saves real vertices (21 000) and the file itself is about 3 mb's in size.

However, the lack of duplication may cause slightly different results in armature-animated meshes which are using skin-weights. It is only caused if different bones heavily modify same vertices or a child bone modifies it's parent bones vertices. The effect is easily demonstrated by the "cylinder is gets modified by two bones"-tutorial. Fortunately this is also pretty easy to fix once you know about it. Use the Edit Mode (Weight Paint mode is not good enough) and go through all vertex groups (Select and Deselect) to see which vertices the group really affects. You really have to make sure that vertex groups don't overlap too much and that they are properly weight-painted.

Also, texturing very simple objects can be harder because the exporter does not duplicate vertices. For example a cube with 8 vertices can not be rendered in Direct3D with all faces properly textured. 8 pairs of texture coordinates just can't do it. You can use the default exporter to export these object or you can manually add vertices. A cube could be made from 6 planes and then joined together, for example.

If you have rotated or scaled objects you must apply these transformations before exporting. Select meshes, press space and select Transform->Clear/Apply->Apply Scale/Rotation. Also when exporting an animated object you must make sure that the mesh and the armature have same transformation and center point. Do this by pressing the space and then select Transform->Properties and set LocX/Y/Z etc. to desired values.

The script has seen some testing, but there probably are bugs and other issues still lurking around. If you have any questions, feel free to e-mail me or contact me at the Blender Artists forum. If you have problems with exporting it is useful to send a link to the .blend file (if possible) so that I can test it too.

Heikki Salo

Critical steps

So, before exporting, remeber these:

Some of these things could be done automatically when exporting, but as a guideline export scripts should not modify the scene or any objects in it.


Latest version 1.0 (2006/12/20) for Blender 2.42 or newer.

SourceForge.net Logo