Modelling for XNA with Blender Part I
March 10, 2008
In this series, we will look at using Blender to build models and assets for use in XNA games. In part I, we build a cube, apply a texture to it, and export it to fbx format.
Note: the Blender/XNA series is intended for coders who have every intention of hiring/finding an artist to build their assets, but would like to know enough about the basics to whip up some crude, textured, and animated models to use in their tests. If you would like to learn Blender in more detail, or you are an artist looking for a richer how-to document for Blender, I recommend the Introduction to Character Animation tutorial which can be found on the BlenderWiki. This series of articles covers only the bare essentials, and may be used as a companion to the previously mentioned tutorial.
[adlinks] When modeling for XNA you’ve got a lot of options. Since XNA readily accepts models in the fbx format, pretty much any 3D modeling package you can find is ready, right out of the box, to be used to build models for your XNA games.
With that said, you’ve got four classes of packages to choose from:
- Top dollar software (Maya, 3DS Max)
- “Free” versions of top dollar software (XSI Mod Tool)
- Low budget software (Wings3D, trueSpace)
- Free software (Blender)
Most of us with limited budgets are going to wipe the top dollar offerings right off the plate. For me, not only is the cost prohibitive, but I’m not even an artist — I’m a coder. Why would I spend thousands on software that there isn’t a chance in hell I’m ever going to figure out how to use, just so I can whip up some half-way decent models to use in my unit tests until I can pay a real artist to build me some good ones?
The “free” (note the quotes) offerings like Maya PE and Mod Tool, unfortunately, come with some severe limitations. Maya PE is not going to work because you can’t export, and Mod Tool comes with the caveat that you can’t use it for commercial works. What if your game starts turning out really good, as in, I can charge for this? You’re going to run afoul of the license.
For me, personally, the low budget offerings would be fine, except for one small factor: there is a full-featured, awesome piece of open source software that blows them out of the water, and with a total price of free. No cost, no restrictions on what you can do with your creations. And a positively huge community of helpful users. C’mon, there really isn’t any better alternative. That software, of course, is Blender.
Now, you might have heard some bad things about Blender. Actually, you’ve probably only heard one bad thing among all the good things people have to say about it. And that one thing is, the interface stinks! I’m going to be the odd one out. I’ve used a lot of 3D modeling software in my time,and I haven’t been able to figure out any of it. It simply doesn’t make sense to me. Blender, however, immediately made sense to me. I don’t know why, but something just clicked when I was using it the first time.
Again, I’m a programmer, not an artist, but wow are these programs complicated. When I’m building something for a game, all I want to do is build up my model, texture it, maybe add some bones to it and animate it. Right? So why is it so hard to accomplish such a simple task? The answer is that most modeling software wasn’t designed for you to build animated models for XNA. It was designed to do everything, it just so happens to be capable of building animated models for XNA. Your task is to navigate through all the options you don’t need to get to the stuff you do. Luckily, Blender seems to make finding all that other stuff kind of complicated, which is great for me, because I don’t know what any of it is. Everything I need is right in front of me, which cuts down on my anxiety. That’s a really good thing.
We’re going to go through four stages of modeling using Blender for XNA. We’ll look at:
- How to build a textured cube
- How to build a UV textured model
- How to add bones to a model
- How to animate a model
That’s pretty much all you need to know to use Blender for your XNA project. In fact, you only need 1 & 2 if you just want to build furniture, vehicles, weapons, and so on for your game. We really only need 3 and 4 to build more complicated stuff like players and NPCs.
So let’s get started. Begin by downloading the latest version of Python at http://www.python.org, and then Blender at http://www.blender.org/download/get-blender/ and install them in that order.
Note: I’ll be using Blender 2.45 for this demo. Blender seems to have this weird habit of changing things pretty dramatically between minor releases, so get your learn on while the interface still looks like I’m about to describe!
Now fire up Blender. You’re presented with a default scene, which contains a cube. This is the start state for Blender, and you should see it whenever you start the program. A word of caution here: Blender’s interface was designed for efficiency, and what that means is that you will be doing almost everything using keyboard shortcuts. Good luck remembering them all!
Even though we’re going to just render a cube here, let’s delete the cube and recreate it. Hit A to select everything in the scene, and then DEL. Now hit SPACE and select Add->Mesh->Cube. We’ve got our cube back. You might have noticed a little cross hair in the middle of the window before the cube appeared. That’s the cursor, and it defines where the center of the new mesh is going to go. You could have moved that around if you wanted, but we want it in the center anyways.
So how come it doesn’t look like a cube? Because we’re looking at it square on. If you want to change your viewing perspective, hold down your MIDDLE MOUSE BUTTON and wiggle the mouse around. We’re now changing the perspective, so we can look at the cube from an angle. Looks like a cube now, doesn’t it?
So now we want to texture this thing. Let’s use the same texture we used in the Silent Hill Cube render example. You should see it off to the left. It’s a 128×128 square jpg. It could be a different size, but for a small cube like this, smaller is better. Just make sure whatever you use is a power of 2 in dimensions, i.e. either 32×32, 64×64, 128×128, 256×256, etc.
Where should you save your texture? You should save it to your XNA project’s Content folder. I like to make a subfolder called Textures and stick all my textures in there. You don’t have to, but it’s important that XNA knows where to find your texture when you build your project.
Now back to Blender. How do we wrap this texture around our cube? Well, we need to do what is called UV texturing. But we’re going to do a really, really simple version of it. In part II of this series, we’ll be tearing our model apart and actually mapping coordinates of our texture to our vertices, but since all we have here is a cube, we just want to apply the texture to each face of the cube.
To do this, we’re going to work in two separate windows. We need to split the Blender window into two separate windows. To do this, click the MIDDLE MOUSE BUTTON where the modeling window meets the menu bar at the top. A menu will pop up, so select “Split Area” and move the vertical divider to the middle of the window. LEFT CLICK to set the split. Great, now we’ve got two windows that look the same.
Underneath the right view window, you should see a little drop down that says Edit Mode. This is where you choose what mode this window is in. We actually want to be in Object Mode, so select Object Mode from this drop down.
Now we’re ready to texture. Hit F5 to bring up the shading window. You’ll see it at the bottom of the screen.
If you aren’t in the Materials buttons list, click the Materials icon (its a red ball). Click Add New. You just added a material to your mesh.
Now look on the right, and you’ll see a panel with a tab that says, Texture and one that says Map Input. In the Texture tab, click Add New. You’ve just added a texture to your material. You’ll notice that an entry named Tex appears in the list on the left.
Now select the Map Input tab. We’re going to tell Blender how to map this texture to our cube. Click the UV button. Awesome, that’s done.
Review what you just did: you created a mesh, added a material to it, added a texture to your material, and told Blender to map the texture to your mesh using UV.
Ok, now go back up to your view windows, where your cube is sitting. In the left window, change the mode to UV Face Select. We’re going to tell blender which of the faces to do the UV mapping on. We want to do it on all of them, so make sure every face is selected. The quick way to do this is to hit A.
Now in the right window, change the Window Type (not the mode, the window type) to UV/Image Editor. (See the image at the left for what the window type selector looks like). The window should change to a flat square. This is your texture — well, it will be! We need to tell Blender where our texture is. So select Image->Open from the menu (the menu on this window), and navigate to your texture. Open it, and the square should now display your texture. You should also see that your cube on the left has received its texture!
Again, let’s review. After we got our cube all set up to be textured, we imported a texture and applied it to every face of our model.
Well, we sure seem to be done, don’t we? All we need to do is export our model to fbx format. On the main menu, select File->Export->Autodesk FBX. Save the fbx file to our content directory.
Are we done? Well, not quite. We still have to make sure that our XNA project can handle all of this. Add the FBX file to your content pipeline using the Add->Existing Item option in Visual Studio. Now we need to do one last thing. We need to make sure our FBX file really points to the texture. Guess what? It doesn’t. Nothing is ever easy, right?
Open the fbx file in any text editor (use vi you apes!) Search for the string RelativeFilename. There should be two instances of this line in the file, which look something like this:
-
RelativeFilename: "fbx/mytexture.jpg"
Not good! We don’t even have a fbx folder. We need to change this line ever so slightly:
-
RelativeFilename: "../Textures/mytexture.jpg"
And we need to do that in both places. That’s it! Now render your cube as you would any model in XNA, and you should see it all nice and textured.
Next time, we’ll look at building a more complicated mesh, and applying a more complicated texture to it. For now, here’s a neat-o video of our textured cube:






Modelling for XNA with Blender Part I…
You’ve been kicked (a good thing) - Trackback from GameDevKicks.com…
Awesome tutorial man! that was way easier than some of the stuff i’ve come across.
My only problem is i’m pretty weak on the code side… I can’t seem to get my models to show up in my apps, and the only code example I’ve found so far was from XNA 1.0 .. could you possibly add or send me a sample of code that works so i could study it?
Thanks for the tutorial man!!
Taylor- I’ve got a brief overview of the XNA Model class up here at Stromcode, but if that’s not enough to get you going, I’d recommend looking at the Getting Started info pack at creators.xna.com. The very first tutorial walks you through building your first project, copying a model and texture over to your Content folder, and displaying it in code.
From there, it gets more complicated, but I’ll be covering pretty much everything you need to know here over the next couple weeks.
[…] the basics of UV texturing and exporting in Blender. If you aren’t up to speed, check out part one and part two of my XNA/Blender […]
Great tutorial, but I have a problem when exporting. When I export it using the specially modified fbx exporter and drew it in XNA, it shows sideways. I know when exporting using the .x, you could choose which axis is up, but not with .fbx. Would I have to manually rotate all objects in XNA using code, or is there a simpler version? Also when exporting, did you choose Selected Objects or Scene Objects?
To Roy:
I am no expert but I bet what’s wrong is you rotated your model while creating it. Blender displays it right because it stores the rotation matrix or whatever, but it doesn;t export to .fbx right. What you can do is select your model (If it’s parented to something, like an armature, unparent it with alt-p) and then press cntrl-A. Apply scale and rotation to ObData, and then parent your object back to whatever. Then you should be able to export. It’s handy to cntrl-A all of your models when you have the finished product, It tells blender it’s good to go!
~Orin
Can you please add information about how to do this in blender 2.46. We have no UV Face Select mode.
Thanks for the tutorial.
You can use “Edit Mode” in Blender 2.46+ as they have removed the UV Face Select mode…
Tim
I use the edit mode in blender 2.46 but I cant see the texture applying on the left side window area. so could you tell me another way of doing the same thing..
The tutorial shows a diffuse texture being passed in to XNA by applying a texture in the UV texture editor. But what about other texture maps like spec maps and normal maps? How does one go about applying these different maps?
Hey, excellent tutorial but I’m too using 2.46 and I’ve got the same problem as Dasun, the texture doesnt apply when loading the image. Any clues how to do this now? probably just missing an “apply” step or something but I cant find it.
Oh, I fixed this so a small add:
In edit mode on the right of the screen there is a triangle, hovering over it says “face select” use that one in edit mode to acomplish what is done here.
Ok I’m trying this in 2.48a. And nothing shows in the right window. Spent 20min+ couldn’t find a solution.
If you are using 2.48a you need to click the Up/Down arrow button next to the Image menu button to get the texture to show up. The texture doesn’t seem to “auto-apply” to the mesh however. Something must have changed in between versions of Blender. Any help getting the texture to apply would be appreciated.
[…] setup Saved by netmouse on Wed 19-11-2008 Love Saved by MomokoBishounenLuvxD on Sun 16-11-2008 http://www.stromcode.com/2008/03/10/mod… Saved by virtuoso2199 on Sun 16-11-2008 Beginning DirectX 10 Game Programming By Wendy Jones - […]
I had the same problem of not being able to apply a texture. I fixed it by going into vertex edit mode, then hitting the “New” button beside UV Texure under the “Mesh” tab. I then selected all the faces (in Blender 2.48a you have to hit the little triangle button in the 3d view window in vertex edit mode) and opened the texture image in the UV/Image editor window.
I created some 3D text in Blender, simply by adding text then embossing and beveling it in the Z-direction. I just added a color to it, I didn’t want to do anything fancy with texture or anything. Now I export the file in both *.fbx and *.x.
I have the code exactly as you have it above, with the ballon as the model, and it works. But no matter what I do, I can’t see the text I created in Blender. I change both the camera (further out and closer in, just in case I might be missing something); still nothing. Can someone help me with this? Do I need to scale it to something? or HAVE to apply a texture to get it to work?
Thanks,
Patrick
Hey Patrick, Select your text, do Alt-C to convert object type, select Mesh.
Then go through the mapping steps described above.
To everyone else using the Blender 2.48+: In your objects materials property panel, Go to the Map Input tab, and select UV instead of Orco. This makes the renderer use the correct mapping to render. Then in the Texture tab, add a texture… Go to the Texture panel, set the texture type to Image, then click load or the menu next to it to select/load the texture you want.
With the mouse over the 3d window, toggle Alt-Shift-Z for Lit textured preview mode or just toggle Alt-Z for Textured preview.
Happy blending!
A minor nit: You’ve listed Wings3D as “budget software”. Actually, Wings3D — like Blender — is “a full-featured, awesome piece of open source software that blows low budget software out of the water, and with a total price of free.”
[…] 22 days ago4 votesThree Must Have Game Programming Books>> saved by coscils598×08 32 days ago6 voteshttp://www.stromcode.com/2008/03/10/mod…>> saved by vinceismyname 34 days ago2 votessome published error in Introduction to 3D Game […]
[…] http://www.stromcode.com/2008/03/10/modelling-for-xna-with-blender-part-i/75/ […]
Thumbs down here. This is not an easy tutorial. Nor is it complete. There is no information where to get the “Silent Hill Cube render example” and how to import it into Blender. I couldn’t really start the tutorial without it. This a beginners tutorial so that info should be up front. I am sure I can figure it all out by looking at other tutorials, but again that I shouldn’t have to do that it should be here.
I take that back, I see what you are getting at now.
Cheers
-John
I had problem mapping the texture to the cube with version 2.49b as well and found that I was missing the following:
“Textures” (F6), Set “Texture Type” to Image and select your image in the list on the right.
Hope this helps.
Dave.
To find your favorite team’s NFL jerseys just click here:www.stajump.com
You’ve listed Wings3D as “budget software”. Actually, Wings3D — like Blender — is “a full-featured, awesome piece of open source software that blows low budget software out of the water, and with a total price of free.”