Using XML and Blender to define XNA Game Objects

I’m quite new to the XNA game development scene, but ever since Sony’s Net Yaroze, the home-brew playstation, was launched in 1997, I’ve always been drawn in by the temptation of writing something to run on a console.

Soon after starting the development of a little game using XNA I quickly realised it would be great to somehow define the levels for my game in as much of a labour-free method as possible. The pragmatic man/woman always strives for re-use, so with this notion in mind I didn’t see the point of creating a tool that would allow me to create levels. In any project you should be making desicions that result in a faster development time – tacking a level editor task to my to-do list was just going to push the end point even further away.

I’ve always been a huge fan of Blender, the open-source 3d modelling package. I’m not an artist, foremost (although I used to endulge in that quite a lot), but I can manage a few vertices and straight lines. 🙂 So I wrote a little Python script (which, by the way, is a joy to write) that exports the mesh to an XML format that XNA can very easily read and import directly into a managed C# object ready for use in my game.

The below image shows my rather primative test model in Blender which is the start of the terrain for a version of lunar lander that I’m creating.

Blender showing my example vector model

Blender showing my example vector model

I should stress, this is only really of any use if you’re defining objects made of straight lines and vertices, like for example a version of asteriods or similar.

So here’s the python script:

#!BPY

"""
Name: 'XNA XML Export (.xml)...'
Blender: 244
Group: 'Export'
Tooltip: 'XNA XML exporter'
"""

import Blender
import bpy

def mainExportProc(fileName):
    # obtain a reference to the mesh data
    scene = bpy.data.scenes.active
    obj = scene.objects.active
    mesh = obj.getData(mesh=1)

    # open a file for writing and output the XML header
    out = file(fileName, "w")
    out.write("\n")
    out.write("\n")
    out.write("\t\n")

    out.write ("\t\t")
    # output all of the vertices (their coordinates) to file as vertex elements
	# negate the Y axis as negative is up in XNA (2D)
    for vert in mesh.verts:
        out.write ("%f %f " % (vert.co.x, -vert.co.y))
    out.write ("\n")

    out.write ("\t\t\n")
    # output all the edges with their vertex indices to file as edge elements
    for edge in mesh.edges:
        out.write ("\t\t\t%d %d\n" % (edge.v1.index, edge.v2.index))
    out.write ("\t\t\n")

    out.write("\t\n")
    out.write("")

    # tidy up and close file
    out.close()

# entry point for the exporter
Blender.Window.FileSelector(mainExportProc, "Export")

In order to run this you have to have a version of python compatible with your installed version of Blender. Blender 2.49 seems to get along fine with Python 2.6.2. Copy the above, save it as a file named xnaxml_export.py in your blender scripts folder (which was C:\Documents and Settings\Andrew\Application Data\Blender Foundation\Blender\.blender\scripts on my system intuitively enough).

Please update/share the script as you see fit and feel free to share any improvements in the comments.

I’ll post the code to load this into a usable C# object in my next post.

One thought on “Using XML and Blender to define XNA Game Objects

  1. I too had and still have my net yaroze… Its been nearly 20 years!!
    I was reading some of the old user groups post and saw your name.

Leave a Reply

Your email address will not be published. Required fields are marked *