Top

Modeling for XNA with Blender Part III

March 13, 2008

bpxnapart4.jpgTo this point, we’ve covered some basic modeling and texturing concepts. You should now know how to build a model and UV texture it. In part III, we’ll add a skeleton to a mesh in preparation for animation.

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]Since we saw the extent of my artistic talent, I’ve decided to take an actually good model and modify it. I’ve used the blue alien guy model from the Introduction to Character Animation tutorial and modified it somewhat. That tutorial goes into some material, texture and lighting concepts that are a little different from what we’re doing here, because that tutorial isn’t specifically for game development, and definitely not for XNA game development. For our purposes, we would rather do the UV unwrap and texture routine on a character model. And so I’ve done just that. I’ve taken Ryan’s untextured model, removed the eyes (so I’m left with just a single mesh), unwrapped it, and layed a ridiculously simple texture on top of it. You can see where we’re starting from in the video below:

Once again my pitiful artistry has taken a pretty cool model and turned it into something rather sad, but I think it will work great for what we’re going to do here. First of all, Ryan has already done the hard work of making sure the model has vertices in the right places, and because I chose such a simple texture, it should be pretty easy to see what’s going on.

In Part III, we’re only going to add a skeleton to our mesh, and play around with it a little bit. We’re not going to build any animations until Part IV.

In short, here’s what we’re going to do:

  1. Add an armature to our model
  2. Add bones to the armature in a sensible way that will allow us to be able to do the kinds of animations we want to do
  3. Link up vertices in the model with bones in the model, so that moving a bone causes the model to move (we’ll use weight painting for this)
  4. Talk about inverse kinematics and forward kinematics, and see how each of them affects our model

One thing that I think is interesting to keep in mind: although bones in the 3D modeling sense are nearly analogous to bones in the biological sense, the relationship isn’t exactly 1-1. For starters, we don’t move around because our bones start moving around, we move around because our muscles start moving around. The bones are just there to structure everything. That’s ok though, because you can think of your mouse as a muscle system, moving your model’s bones around and relying on the joints to keep everything in place. The most important thing to keep in mind is that sometimes you’re going to want more bones than you think.

I looked at a model of a large woman earlier today that had the typical skeleton you’d expect, plus a giant bone right in the middle of her chest, apparently so you could animate her breasts bouncing if you wanted to. It sounds pretty ghastly to have a bone there, but that’s what you gotta do if you want to animate a part of your model that doesn’t usually have a bone. Some less disturbing examples would be vehicles, which might have a bone for each of the wheels, or a character wearing a robe — you might add bones for the robe, so both the character and his clothing can have their own motion.

Basically, just don’t get too locked into thinking that your model of a human should have all the same bones as a real human. A lot of those bones are going to be redundant, and you may end up needing bones that don’t exist in nature. Millions of years of natural selection have resulted in us having the bones and muscles required for the movements we need to do. Any more or any less wouldn’t make sense. Similarly, your model is going to need only the bones required for the kind of motions you are going to animate it into. If you just want your model to stand there and wave, it’s not going to make a whole lot of sense to add a bunch of toe bones.

Getting Ready

First of all, we are going to want our skeleton to be symmetrical. You could build the bones without any sort of mirroring, but since you probably built your character model using a mirror modifier, you will likely want the skeleton to be symmetrical as well. So first of all, let’s position the 3D cursor at the center of our mesh. To do this, enter edit mode, select a vertex somewhere on the center of your model (this should be somewhere on the seam of where your model was mirrored), and hit SHIFT S. Select Cursor->Selection.

Now switch to object mode. Add an armature with Space->Add->Armature. You should see a little bone starting from where you positioned the cursor. Bones have a root and a tip. We’re going to want to build a bunch of parent-child relationships between our bones, so its important to know which is which. The fat end is the root, and the skinny end is the tip. If we extrude a bone from the tip of another bone, it will become the child of the first bone. If we extrude a bone from the root, they’ll be connected, but not exactly in the same way. We’ll see what that means later.

Now before doing anything else, look down in the buttons window. In the edit buttons, you should see a box for the armature we just added. Give it a name (Armature is a fine name), and then click the X-Axis Mirror and X-Ray buttons. The first button will make sure we create mirror bones whenever we extrude a bone along the x-axis, and the second button will make sure that bones show through the surface of the mesh.

Now you can get as detailed or as simple as you want in how you build your skeleton out from here. I’m going with simple, because I just want my guy to do some basic stuff. I don’t need him to bend at the side or anything crazy like that, so one big bone is going to be just fine for the spine. But if you wanted your model to bend realistically at the waist, you’d need a more complicated spine.

0313screen1.jpgOk, so go into side view, select the entire bone (by right-clicking somewhere in the middle of it), and G to grab it. Move it down so its root is in the pelvis area. You might notice that the bone isn’t inside the model, so in side view, move it to roughly the center. Now grab just the tip, hit g, and drag the tip up to about shoulder level. Don’t go up to the neck, because we’re going to extend the arm bones from this tip.

Now we’re going to extrude ourselves a neck. Select just the tip, hit E, and constrain to the z axis. Drag up to make a little neck bone. Repeat the process again for the head bone. You can make the head bone jut out of the top of the head a little bit, which will make it easier to grab it.

Now, a couple interesting things here. First, every time you extruded a new bone, it was automatically made a child of the original bone. So we’ve basically built up a little chain here. Second, each of these bones was given an automatic name, and it’s a good idea to rename these bones as we go along to something that makes sense to us. So far, I’ve got bones named spine, neck and head. You’ll agree that’s a bit more descriptive than Bone, Bone.001 and Bone.002. I don’t know if XNA and/or the fbx format have the same problem as DirectX and/or the x format, but I seem to remember these bone names being very important. As in, the . in the auto-generated name screwed things up pretty badly. I’d just advise avoiding any punctuation in object names period.

Now we can extrude a couple arm bones, and hand bones. Here we’re going to take advantage of that x-mirror thing from before. Select the root of the neck bone (which is the tip of the spine). Switch to front view. We don’t just want to extrude now, we want to symmetrically extrude, so instead of hitting E, hit SHIFT E. Move to the right and you’ll see, magically, we also get a left bone. Place a bone for the shoulder, upper arm, forearm, and hand, and name them all appropriately. Mine are named shoulder_l, upperarm_l, forearm_l, hand_l, upperarm_r, etc… However you name them, just use the same naming convention. So don’t do something like Left_Upper_Arm and upperarm_r. They should match up if you want blender to know what you’re trying to do.

0313screen2.jpgRemember you’re free to use G to move things around to get it just the way you want it. Just don’t do any rotation or scaling unless you know what you’re doing.

At this point, if you switch to Pose Mode you can start playing around with your bones. Have fun, just remember to undo everything you did. As you move things around, you’ll notice that moving a parent bone causes the children to move as well. That’s probably what you expected. Keep in mind that this kind of relationship is called Forward Kinematics. It basically means that when you move one bone, the motion is carried forward to all of the child bones.

You can also play with Inverse Kinematics. To do this, go to the Armature panel in the button window and click the Auto IK button. Now move around a bone. Things are very different this time but, oddly, more natural. Inverse kinematics basically means that when you move one of the child bones, the motions is pushed inversely back to the parents.

A way to think of this is to reach your hand out. Imagine someone grabs it and starts shaking it. In forward kinematics, only your hand moves. Your arm bones, being parents of the hand, don’t receive any of the motion from your hand. They just sit there. But what actually happens if someone grabs your hand and starts shaking it? Your arm starts moving in a rolling, wave-like motion. The parent bones receive some of the momentum of your hand and move as well.

For this reason, IK is probably going to be easier to deal with when posing. You can use FK to get more specific with your poses (when you are just focused on the specific position of a single bone).

For simplicity sake, we’re not going to deal with the lower body here. We’re just going to be animating the top of this guy. In part IV we’ll add a couple animations to him — a wave, and a salute. For the lower body, the process is more or less the same, with a couple little gotchas when you need to keep the feet lined up during walking/jumping/crouching, etc. But you can figure it out.

So we’ve got a skeleton, but moving it doesn’t affect our mesh. How do we link the two?

We need to create an armature modifier. If you’ve used the mirror modifier, or the subsurf modifier, you know how to use modifiers. If not, it’s simple. Switch back to object mode and select the mesh (not the armature). In the edit buttons window, click the Add Modifier button, and select Armature. In the OB: box, type the name you gave your armature earlier. Unclick the Envelopes button.

Now if you select the armature and switch to Pose Mode, you’ll notice that nothing seems to have changed. That’s because we haven’t told Blender which vertices go with which bone. We’ll do that now.

Weight Painting

Select your mesh and switch to Weight Paint mode. Your mesh will turn dark blue. Weight painting is a very simple concept. You select a bone, decide how much influence you want that bone to have on a vertex, and then “paint” the influence onto your mesh.

Look at the buttons window. You’ll see a Paint box down there now. Set Weight to 1.0. Set Opacity to 1.0. Turn off All Faces. Turn off Vertex Dist. Leave Soft on, and Turn X-Mirror on. We basically just said we want to paint heavy influence wherever we set down the brush, and we want symmetrical bones to be painted automatically in the same way for us.

0313screen3.jpgSelect the left hand bone. Paint the hand, all of it, but only the hand. You might need to do a fair amount of zooming, panning and rotating to get everything. But be as thorough as you can. If the bones are getting in your way, go down to the Armature panel (in object mode) and switch the bone draw mode from Octahedron to Stick. That will turn your bones into thin sticks that are less likely to get in the way.

Now select the right hand bone. Whoah! It’s already painted for you! That’s what all the symmetrical stuff was about (and the bone naming stuff). So just go through your model and paint the vertices that should go with your bones. One thing you might want to do is to grab your bones and move them around. Make sure the right verts go with them, and none get left behind. If you drag the hand, for example, and the hand goes with your bone except for one vertex, you can just paint over it to make it snap into place. Moving bones around is a good way to make sure you got everything, as well as to get at verts that are hard to reach. Just move all the painted ones out of the way, and keep painting.

What if you want to erase? Simple. Either hit CTRL Z or go to the Paint window and click the Sub button. This will subtract the 1.0 opacity wherever you paint. Check out those buttons while you’re there so you know you have them available to you.

You don’t need to use weights of 1 and opacities of 1. You can mix and match weights and do all sorts of crazy stuff with weight painting, but for our purposes here its enough to basically say “yes, this bone affects this vertex” or “no, this bone does not affect this vertex.” As long as everything moves around normally when we grab a bone, we’re good to go.

0313screen4.jpgI actually find this process to be the most intuitive of the entire modeling-texturing-rigging-animating process. It just makes sense to me. I move the bones around, and paint the vertices whenever something doesn’t look right. Eventually, it all just looks right, and I know I’m good.

One note: When weight painting with an eye towards exporting fbx files for XNA, remember that every vertex needs a weight associated with it. When I first added this model to my project and tried to build, I got an error: BoneWeightCollection does not contain any weighting values. Apparently this means that my mesh has bones, but some vertices aren’t weighted to any of the bones. For whatever reason, this causes XNA to freak. It makes sense, but it’s still kind of annoying. So just make sure you paint every vertex to some bone. In our case, since we’re only rigging the top half, I just painted everything in the bottom half to the spine.Ok, now finish up painting. You know you’re done when you can go into pose mode and move your model around without any weird tearing or other strange behavior.

Since we’re just learning the bare minimum here, let’s call this sucker done and get ready to do some animation.

If you want to, you can pose your model in object mode, and export to fbx. If you load your model in XNA, you should see it all posed. Here’s what mine looks like:

In case you were wondering what a skinned model is… well, that up there is a skinned model. I think its a pretty odd term, myself. Boned model makes more sense to me. But a skinned model is simply a model with bones, which can be manipulated (animated, posed, ragdolled) by manipulating the bones rather than the individual vertices.

We’re almost done! Get ready for Part IV!

Comments

13 Responses to “Modeling for XNA with Blender Part III”

  1. GameDevKicks.com on March 14th, 2008 6:03 am

    Modeling for XNA with Blender Part III…

    You’ve been kicked (a good thing) - Trackback from GameDevKicks.com…

  2. Byron Nelson on April 9th, 2008 11:43 am

    Nice tutorial. These things confuse me profoundly. If you take the simple tank animation off the XNA site, it uses separate meshes & separate bones. It looks like you have one mesh, multiple bones. Can the type of model you create here be code-animated like the tank example where you can apply transforms to particular bones for particular meshes?

    I ask because I’m in need of building some simple guns on turrets that I can individually manipulate as in the simple animation/tank example on the XNA site.

    Enjoyed reading your site,
    Byron

  3. stromdotcom on April 9th, 2008 12:22 pm

    Byron-

    Yes, you can. Take a look at the Model methods. A few of them will return the bone transforms in the model, copied to an array. You would need to manipulate these transform matrixes yourself in order to do what you want to do.

    In either case — if the animation is present in the fbx file or if you are going to do it yourself — you are simply manipulating bones. So everything up to the animation step would apply.

  4. Scilly Guy on April 9th, 2008 10:03 pm

    I tried to paint the vertices to link them with particular bones but when I try to select a bone it just selects the entire armature and returns to object mode. I am sure I haven’t missed anything. When I paint the vertices it assigns them to groups and these groups are linked to bones so it is fine from a modelling point of view but when I try importing it to XNA I get “Error normalizing vertex bone weights. BoneWeightCollection does not contain any weighting values.”

    Also you do not mention about extruding from the other end of a bone ie the leg end in this example, I assume you just assign the new bone the old one as a parent and do not “connect” it.

  5. stromdotcom on April 9th, 2008 10:49 pm

    1. Blender can be kind of bizarre when it comes to modes and selections and so forth. The only advice I can give you is to just experiment.

    For example, I have found myself in positions where suddenly I am in object mode, and no matter what I do I can’t switch out of object mode. It usually comes down to something being selected that shouldn’t be selected, or some other little trivial thing.

    In your case, it is likely that you are selecting the bone in the wrong view. If I remember correctly, I keep one window in pose mode, and one in weight paint mode. I select the bone in the pose mode window, then paint in the weight paint window. Also, it matters what is selected before you switch modes!

    The key, really, is to remember that Blender’s interface is not in any way related to the interface of any other program on Earth. It takes some getting used to.

    2. That error means you missed at least one vertex while painting. It can be brutal trying to figure out which one. Also, make sure you named your bones without any of the .001 stuff.

    3. You can extrude from the root, it just doesn’t give you the parent/child relationship. You can also (in the buttons window) link or unlink bones. The button is strange, but I believe it says con (for connect). You want to unselect that button, thus unconnecting the bones.

  6. Scilly Guy on April 10th, 2008 1:37 am

    If I have more than one viewport in 3D view then they all have to be in the same mode, how strange that you can but I can’t, something odd is going on. My blender does seem to be odd though, think it doesn’t like my graphics card or something, I get a pink haze that builds up, its really annoying.

    I have tried selecting the bone I want and then switching to object mode and selecting the mesh and switching to vertex paint mode but I either end up with just the mesh selected or the mesh and the entire Armature. Like I mentioned I have the bones linked using vertex groups, and I have now weighted everything to 1 for the Armature (using painting) and it’ll now run in XNA but all I get is a grey screen. Its strange, if I export without the Armature I can load it and view it fine,

    All my bones are definitely named as I think they are else it wouldn’t load, I’ve had that error before. I have also tried zooming out but to no avail.

    Do you ever find that you try to select a face/edge/vertex and it selects a completely different one?

  7. bob on July 13th, 2008 9:44 am

    Hello thanks for the tut.

    My problem is that when I mirror over the X-axis the mirrored portion moves in the opposite direction. For example if i want to make an animation of a bird flapping its wings, if i move a bone on the right to move the wing up, the left side will move down. Is there a way to use a mirror so both sides move the same?

  8. galdon on July 23rd, 2008 10:47 am

    It does not seem to work. The painting that is, i cannot select the bone while in paint mode. and selecting the bone in either pose mode or edit mode seems to do nothing to effect the paint mode. Using blenders automatic bone assignment generates the weight error. painting everything then auto-assigning does not help.

    I tried opening the second window and putting one into pose and one into paint, however they are quite firmly glued together, so when i switch one window’s view, the other window changes to match.

    I can make the bones work perfectly in blender’s own system, but when it is exported and run through the test to go to XML it always comes back claiming one error after another.

  9. Orin on July 28th, 2008 2:06 am

    To scillyguy:
    It sounds like you’re trying to select a specific bone while in object mode. If when you select the armature, it does not appear blue, then you are in object mode, and you must get into pose mode before you can weight paint the way you want to. To do this, select the armature (right click) and switch to pose mode (There are several ways to do this, the simplest way is to press cntrl+tab) The armature should become blue and you should be able to select individual bones. Then just follow the tutorial above. Good Luck!

    To Bob:
    It sounds like your armature wasn’t built correctly. If you view the axis of the bones, I believe one side of your armature has the Z axis pointing upward, and the otherside has the Z axis pointing downward. Because of that, when you mirror a pose, it doesn’t produce the desired effect. I will not go into too much detail, look up a few other tutorials on creating armatures, and practice practice practice! Either way you go, it seems like you’ll have to make a new armature for your creature. Sorry :(
    Good Luck!

    To galdon:
    It sounds like you’ve made a mistake I make a lot. The most convienient way to weight paint is to make sure the armature is in pose mode, then while still in pose mode, manage to select your mesh (This is hard because blender assumes you are trying to click on a bone, so you have to find an angle where you can click on a part of your model that is not close to a bone). Then from there you may enter weight paint mode. Since your armature is still in pose mode, you should be able to select individual bones.
    Good Luck!

  10. Mike on August 19th, 2008 4:30 pm

    I have made a similar model but didn’t use weight paint. Instead I assigned vertices to vertex groups for every bone. Works about the same way BUT then I got the “BoneWeightCollection does not contain any weighting values” error.

    Do you have to use weight paint in order to export to FBX or did I miss a vertex somewhere? If a vertex was missed shouldn’t that just show when you move around a bit?

    Anyway, great tutorial and thanks for finally explaining wth skinned model means! I’ve been thinking it had something to do with UV textures :)

  11. Mike on August 26th, 2008 5:23 am

    Ok solved it myself. When looking into the weight stuff I realized that assigning vertices to bone groups gives them a weight of 1.0, so that’s that. And when going through all bonegroups selecting all verts and then moving around a bit I saw that I had missed one vertex. So all is good.

    I did however get problems when using 4 textures. One for pants, one for arms, one for torso and one for head. All the time at least one texture refused to work. Reassigning only changed which one didn’t work. When changing to 3 textures everything started working. Anyone know what this is all about? Is 3 the maximum number of textures?

  12. Nick on December 24th, 2008 2:15 am

    I think there may be a reason they didn’t go with the term “boned model”… think about it (hint: Tyra Banks). Then again, “skinned model” actually seems more graphic. Guess it brings up the old debate of violence vs sexuality and which is more graphic.

  13. Daz on October 10th, 2009 12:18 pm

    Thanks for putting these tutorials up. They inspired me to do my own 3d modelling as I’m continually hampered by never having any suitable 3d models for my demos but i have to say Blender is the most complex piece of software I’ve ever used but I’m slowly getting there.

Got something to say?





Bottom