Top

Modeling for XNA with Blender Part IV

March 16, 2008

blender_p4_title.jpgIn the final part of the Blender/XNA series, we add two animations to our textured model, load it into the SkinnedSample using the new content processor, and switch between animation clips.

[adlinks] If you’ve been following along thus far, you now know how to build models in Blender, apply UV mapped textures to them, and build armatures with which you can pose and animate your models. Now it’s time to finish the whole process off and animate this thing.

I’ve again switched models. The simple guy I used in Part III was fun, but I wanted to use something a little simpler this round. To that end, I’ve built a really simple worm out of a couple cylinders. Our worm has two bones, one in the back and one in the front. We’ll be giving him two actions: a wiggle animation with which he lifts his head and shakes it left and right, and a squirm animation with which he does a sort of wave motion with his entire body. This looks a little bit like a crude walk.

Let’s take a look at what this is all going to look like in XNA:

Ok, before we begin, we should review. Here are the steps you need to get ready to animate in Blender for XNA:

  1. Build up your model in Blender.
  2. Apply any modifiers to finalize your model.
  3. Use the UV/Image Editor to texture your model.
  4. Add an armature and any bones you need to pose your model, making sure to rename your bones logically and responsibly (i.e., get rid of all the .001 stuff)
  5. Weight paint your model’s vertices making sure to paint every vertex. If you have vertices that you don’t want to move ever, give your model a bone that you aren’t going to touch, and weight those vertices to that bone.
  6. Add an armature modifier to your model, but don’t apply it.

If you’ve done all that, you’re all ready to create animations for your model. Our process is fairly simple at this point:

  1. Modify our workspace for animation.
  2. Use pose mode to pose the model, and use the keyframe editor to lock down our poses into frames.
  3. Add new animations as needed and repeat.
  4. Export to fbx, and load in our XNA project.

Let’s begin.

Modifying the work space

You should already know how to split a window and change the window function. We’re going to need two new windows for this.

031608_screen1.jpgFirst, we need an Action Editor window and a Timeline window. You probably want to split your 3D view window vertically to make room for the action editor, and split the buttons window horizontally to make room for the timeline.

In your 3D view window, change the viewport shading mode to Textured (optional), and select your armature. Switch to Pose Mode.

Now we’re all set up to animate.

Building Animations, aka Actions

In the action editor, add a new action by clicking the arrow button next to BAKE and selecting ADD NEW. Give it a name. This will be the name you’ll use to reference this animation in XNA, so pick something descriptive. My two animations are called wiggle and squirm.

Now, we’re just covering the basics, remember, so I’m going to walk you through the easiest way to build an animation without thinking too much. You should get familiar with the keyframe concept if you aren’t already. But it’s pretty simple. When a bone changes, either its location, rotation or scale, you add a keyframe. If it doesn’t move, you don’t need to create a keyframe. But — you may need to create keyframes between two exact LocRotScale settings to say “don’t move here, but move after this”.

Anyways, again, we’re just looking at the basics, so here is a braindead easy way of animating with keyframes.

  1. Move to frame 1.
  2. Pose your model.
  3. Select all bones in your armature and hit I. Select Insert Key->LocRotScale.

031608_screen2.jpgYou now should see a keyframe added for every bone in your armature.

Now, move to another frame. Pose and repeat. Continue doing this until you’ve worked through your entire animation cycle.

One thing to keep in mind: XNA is going to expect that your animations work at 60 FPS. You can manually adjust your animation to occupy more or less frames by moving keyframes around in the action editor. The same basic edit tools apply — Grab, Bounding box select, etc.

When you are done with your animation, you can play it back in the timeline. Just set the last frame to the last frame in your animation, rewind, and hit play.

We’re going to create two animations, so we’ll do this whole process twice. Add a new action, give it a name, and repeat. When you make a new animation, Blender automatically copies all the keyframe data from the last animation, so feel free to delete all of it if you need to.

One thing you might want to be aware of. Do you see a record button in the timeline? That button will automatically do the key framing for you. Just create an initial keyframe in frame 1 for all of your bones, then move to the next frame and pose. Blender automatically updates all the keyframes.

Another thing to keep in mind: most of your animations are going to be loops. One way to accomplish looping is simply to copy the keyframes from frame 1 into your final frame. To do this, unselect all the key frames, hit B to bounding select all the keyframes in frame 1, then hit SHIFT D (duplicate). You’ll automatically enter grab mode, so move your duplicated frames over to your final frame.

That’s all there is to animation. Now we need to export.

Before we do that, though, here’s another example of a Blender built animated model — the dude from part III animated to do a little dance:

Just in case you didn’t think my worm model was cool enough. That animation took me about 15 seconds to set up, which should give you an idea of how easy this all can be once you set everything up properly. Just add talent (which, again, I lack when it comes to modeling and animating).

Exporting

You’ve probably heard that Blender 2.45 has an improved fbx exporter that works great. Well, not for me. I had to use the exporter created by TripleB Games, which you can find here. Download that, copy it to your scripts directory, and restart Blender. Now we’re not going to use the built in exporter anymore, we’re going to use the Modified for XNA exporter instead.

Exporting is simple. If everything was set up properly, just run the exporter, select All Scene Objects and All Actions, then click export. As before, you will need to manually edit your fbx file to point to the texture map you used.

At this point, we should be able to load our model the old way — i.e., adding it to our project, and creating a Model object in XNA. If that much doesn’t work, go back a couple steps and try again.

Importing in XNA

Now for the good stuff. We want to display this in XNA, right? Open the SkinnedSample project. Add your new model to the project using the Add Existing method. Here’s the catch: we need to change the content processor we want to use for this model.

View the properties for your newly imported model. You should see a field named Content Processor at the bottom. Change it from Model - XNA Framework to SkinnedModelProcessor. This new processor will go through your animated model and build a tag for the animation data it includes.

Now, just look at your LoadContent method. Change it to load your model instead of the original dude:

  1. currentModel = Content.Load<Model>("Models\\MyWorm");

Note that currentModel is just the plain old Model class we know and love.

Now change the method to load one of the animations you created for this model:

  1. AnimationClip clip = skinningData.AnimationClips["wiggle"];

Build and run. You should now see your model moving around.

In my worm demo video, I created two AnimationClips, one for each of my two animations. I then handled keyboard input to switch the active animation on a specific keypress. That’s why you can see two different animations cycling in the video.

And that’s all there is to it! That’s everything you need to know to build textured, animated models in Blender for XNA.

Comments

12 Responses to “Modeling for XNA with Blender Part IV”

  1. GameDevKicks.com on March 17th, 2008 7:38 am

    Modeling for XNA with Blender Part IV…

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

  2. Fritz on March 17th, 2008 12:20 pm

    Excellent tutorial - you saved me a job, I’d been intending to put a tutorial together showing how to create models & animations and take them from blender to XNA, now I’m just going to point people here instead.

    Cheers,
    Fritz

  3. stromdotcom on March 17th, 2008 12:49 pm

    @Fritz: thanks, glad you enjoyed it! Your exporter was a lifesaver, by the way. I hassled with the built-in exporter for a couple days before trying yours.

    Incidentally, I was planning on looking over your exporter and comparing it to the one that ships with Blender to see what you changed, but could you briefly mention what you did have to change? I never did figure out what about the original exporter was failing once animations were added.

  4. Fritz on March 19th, 2008 2:49 am

    Hi Strom, I’ve gone into a bit more detail over on the blender artists forums as to the changes that were made in this thread http://blenderartists.org/forum/showthread.php?p=1074089&posted=1#post1074089

    mail me fritz[at]triplebgames.com if there’s any more detail you need or anything else I can help you out with

    Fritz

    http://www.triplebgames.com

  5. Orin on July 28th, 2008 2:33 am

    The only question I have is about blending animations. I do it all the time in blender to make things seem more natural throughout transitions of action. What is your take on that situation? Is it possible with XNA? If so, what do I have to do, as the animator, with blender to get things in proper order?

  6. Nils on September 1st, 2008 12:48 am

    Hi Strom,
    thank you very much for this great tutorial. I had problems with this question about getting models animated in xna for years… But now everything works fine! Thank you!

  7. Manuel Montoya on October 28th, 2008 5:38 pm

    Hi, I am trying to make this work but I can’t do I need an XML file for each Clip?

  8. ildarabbit on November 13th, 2009 5:30 am

    Hi there,

    I am a n00b to both Blender and XNA but have been working my way through your tutorials and finding them very helpful! I have question about workflow: for creating games engine content the workflow is often (1)create mesh (2)skin mesh (3)animate (4)texture but the way you’re doing it you are doing texturing before any animating and i was wondering if there was a particular reason for this?

  9. Dhampir on December 5th, 2009 6:51 pm

    Hi Storm i have some question to you =)
    1. i can’t find the class AnimationClip in xna framework.
    2. what is skinningData in the line to make the clip?

    thanks =)

  10. Dirtysteve on April 19th, 2010 5:49 am

    @Dhampir

    The AnimationClip is in the Skinned Sample project, which must be downloaded from the XNA creator’s club site.

    The libraries contained within the sample can also be taken and used with other projects, so you can ad skinned animation to your own code.

  11. Arturo on May 14th, 2010 4:27 pm

    my question is skinningData
    skinningData is a variable? i think is a variable and skinningData.AnimationClips[”wiggle”]; must be.. a procedure?

    am i right? well, skinningData is what type of variable? or… what?

  12. link building on August 31st, 2010 9:12 pm

    […] Modeling for XNA with Blender Part IV [ …]

Got something to say?





Bottom