Exporting an animation with Blender

The place for artists, modellers, level designers et al to discuss their approaches for creating content for OGRE.
Post Reply
St0rr
Gnoblar
Posts: 1
Joined: Fri Nov 29, 2013 4:25 pm
x 5

Exporting an animation with Blender

Post by St0rr »

Hi there,

since I had some troubles exporting an animated Mesh with Blender I made a tutorial for it. Maybe someone can get use out of it :)

Introduction

In this tutorial we will use Blender to create a simple Minecraft like character, rig and animate it and export it to the Ogre data type.

Prerequisites

For this tutorial you need Blender in at least version 2.68 and the Blender2Ogre exporter in at least
version 0.5.9. (Can be found here http://code.google.com/p/blender2ogre/). I assume that you have a little
Blender knowledge. To see the animation in action we will use Ogre3d in version 1.8 and to make things simpler we will use the wiki framework with the code of the intermediate tutorial 1 (can be found here http://www.ogre3d.org/tikiwiki/tiki-ind ... =Tutorials)


Getting Started

To install the Blender2Ogre exporter, simply copy the python file to path_to_blender/2.68/scripts/addons. Now we need to activate the exporter in Blender by going to the user preferences tab. Go to File->User Preferences, there open the addons tab and activate in the list "Import-Export Ogre Exporter". Save the config and the Exporter is good to go.
Now we start to create our character.

Modeling a basic character

In the outliner expand the cube entry and give the next cube entry (the one with a triangle beside it) a name. Sonething like Larry. Larry is a good name. This will be the name with which you will load the mesh with Ogre.
Press numpad 1 to set the view to front view
  • Go to edit mod (press tab)
    Select the cube and press
    s, z, 3 enter (scale the cube 3 units along the z axis)
    s, x, 1.5 enter (scale the cube 1.5 units along the x axis)
    Create a new cube, this will be an arm (press shift-a)
    Select the cube and press
    s, x, 0.5 enter
    s, z, 1.7 enter
    Move it to the height of the shoulder
    Press shift-d while the arm is selected and move it to other side (it's easier when you press x, so you can only translate it along the x-axis)
    Create a new cube this will be a leg (again press shift-a)
    Select the cube and press
    s, x, 0.6 enter
    s, z, 2 enter
    Move the mesh to the leg position
    Again press shift-d, then the x button to move it to the other leg position
    Create a new cube, this will be the head
    The cube already has the correct size, so just move it to the head position
Go back to the object mode, by pressing tab. Translate the mesh along the z-axis so that it will stand on the ground. In order to get no error when we export the mesh, press strg-a and click on location. This will prevent the "armature offset to mesh" error.
Your character should look like this
model_finish.png
model_finish.png (3.39 KiB) Viewed 13948 times

Before we rig the character we should rotate it so it will look down the x-axis. This is only important because then we don't need to do adjustments in the wiki framework code.
Select the mesh press r, z, 90 enter.
Now we need to apply the rotation.
Select the mesh and press strg-a -> rotation
This step is crucial because else the mesh and its animation will look differently in Ogre3D. So before you export your mesh always check that the rotation values in the transform window (press n) are set to 0.
In the next step we will rig the character.

Rigging the character
  • Be sure that you are in the object mode.
    Press shift-a go to armature and press on "single bone". Select the bone and go in the properties window (that is the one under the outliner) to the object data tab (that is the one with the cube on it). Now click on "X-Ray" in the Display section. This option will render the bone even if it is inside the mesh.
    Place the bone in the mesh like it would be the spine of the character. Go again to the edit mode. Scale the bone up a bit so we can see and select it easier.
    Press shift-a and we have another bone created. Place it in the head.
    Press again shift-a and place the bone in one of the arms. Make sure that the bone cone is pointed downwards. * Keep the bone selected and press shift-d to duplicate and press x to restrain th translation to the x-axis. Move the bone in the other arm.
    Repeat theses steps for the legs and we have rigged the character.
Now we need to give the bones names. This is done best by using the outliner. Expand the two armature options and give the bones the appropiate names (spine, head, arm.L, arm.R, leg.L, leg.R).
  • Select one of the arm bones and then shift select the spine bone.
    Press strg-p and choose "keep offset".
    Do the same for the other arm bone. This will move both arms when the spine bone is moved or rotated.
    Go to object mode and select the mesh and then shift select the armature. Press strg-p and choose "with automatic weights". Now the armature is the parent of the mesh, so when you translate the armature the mesh will move aswell. You can try it in the pose mode which you can find in the drop down menu where you can also change from object mode to edit mode.

Move or rotate all bones and you will see that for some reason the spine didn't get any weights so it won't move (if it does skip the next steps). To change this we need to go to the weight paint editor.
  • First go back to object mode, select the mesh and now you can change to the weight paint mode.
    First select the spine bone, by selecting it in the vertex group (the triangle) tab in the object properties window. * On the left side window choose the blend type "add".
    In order to only paint the vertices which are related to the spine mesh we activate the option "Face selection masking for painting".
    Now select the spine mesh and by left clicking paint the mesh completely red. Do the same for the backside.
The spine bone will now manipulate the mesh. Check it in the pose mode!
Weight paint mode and how the spine mesh should look like
Weight paint mode and how the spine mesh should look like
weight_paint1.png (54.22 KiB) Viewed 13948 times
Animation

In this section we will create two animations. One "Idle" and one "Walk" animation. First we setup our windows. We need the timeline window which is activated by default. The others are the dope sheet editor and the nla editor. At first we only need the timeline and the dope sheet. Split the timeline window horizontally and open in one of the new windows the dope sheet editor. Inside of the dope sheet open in the drop down menu the "Action Editor" and create a new action by pressing on the plus button. We start with the idle animation and that is how we call this action "Idle". Later we will use this name to play the animation with Ogre.
  • In the timeline window activate the "automatic keyframe insertion for objects and bones" (the red dot). For the idle animation we will create a simple head shake.
    Our animation will only last 48 frames so we don't need the default 250 frames. Change that in the timeline window.
    Go to frame 12 and rotate the head to one side.
    Go to frame 24 press alt-r to reset the rotation and the head should look straight ahead again.
    In the dope sheet window select only these keyframes press shift-d to duplicate and move them to frame 0.
    Go to frame 36 and rotate to the other side.
    Go to frame 48 and press alt-r again.
That is our idle animation.

To use this animation in Ogre we need to create a nla-strip. To do this we change the timeline window to the nla editor window. Expand armature and click on the snowflake to create a nla-strip. This is usually used to blend between animations but the Blender2Ogre exporter uses the nla-strip to get the names of the animations. If we wouldn't create a nla-strip the animation would be called "my_animation" and we couldn't create more than one animation.

Change the nla-strip window back to the timeline window and click on the red button to automatically create keyframes. Now we start to create the walking animation.
  • First create a new action in the dope sheet editor and call that action "Walk".
    This animation will also last for 48 frames.
    Start at frame 12 and rotate the arms and the legs in a appropiate position.
    Go to frame 24 and reset all the rotation by pressing alt-r for each bone.
    Like we did it with the head select only the last keyframes and press shift-d. Move the duplicated keyframes to frame 0.
    Go to frame 36 and rotate the arms and legs the other way around.
    Go to frame 48 and reset all rotations.


Now we need to create a nla-strip. Do it the same way as you did with the idle animation.

Export

Export the animation with the following options:
export.png
export.png (41.13 KiB) Viewed 13948 times
If you get the error that the mesh and the armature have an offset, select the mesh and press strg-a and click on location. This will set the location of the mesh to zero. Sometimes you have to do the same for the rotation and for scaling.

To use this animation with the intermediate tutorial 1 simply copy the .mesh and .skeleton files to path_to_project/dist/media/models.
In the TutorialApplication.cpp just load instead of the robot.mesh, the mesh you just copied in the working directory. And you should scale the mesh. Here is the code which belongs in the createScene() method.

Code: Select all

// Create the entity
    mEntity = mSceneMgr->createEntity("Larry", "Larry.mesh"); // change the second string to the name you gave your mesh
    mEntity->setCastShadows(true);

    // Create the scene node
    mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("LarryNode", Ogre::Vector3(0.0f, 0.0f, 25.0f));
    mNode->attachObject(mEntity);
    mNode->setScale(10.0f,10.0f,10.0f); // scale the mesh
If you now start the OgreApp you will notice that animation states won't reset. To solve this we just adding the following code in the frameRenderingQueued

Code: Select all

if (nextLocation()) {
	// Set walking 
	if("Idle" == mAnimationState->getAnimationName()) { //this will reset the idle animation state
		 mAnimationState->setWeight(0);                          //by setting its weight to 0
	}
	mAnimationState = mEntity->getAnimationState("Walk");
	mAnimationState->setWeight(1);
	mAnimationState->setLoop(true);
	mAnimationState->setEnabled(true);				
}//if

Code: Select all

if (!nextLocation()){
	changedState = true;
	if("Walk" == mAnimationState->getAnimationName()) {
		mAnimationState->setWeight(0);
	}
	// Set Idle animation
	mAnimationState = mEntity->getAnimationState("Idle");
	mAnimationState->setWeight(1);
	mAnimationState->setLoop(true);
	mAnimationState->setEnabled(true);
}
The final result should look like this:
http://youtu.be/0TQYGIuRz0U


If there is anything wrong with these steps or maybe there is an easier way to do things, let me know :)
User avatar
spacegaier
OGRE Team Member
OGRE Team Member
Posts: 4304
Joined: Mon Feb 04, 2008 2:02 pm
Location: Germany
x 135
Contact:

Re: Exporting an animation with Blender

Post by spacegaier »

I would advise you to add that to our Ogre wiki. Your effort might otherwise get a bit lost here in the thread jungle.
Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
LDAsh
Gnoblar
Posts: 20
Joined: Thu Aug 25, 2011 8:29 pm
x 3

Re: Exporting an animation with Blender

Post by LDAsh »

Thanks St0rr, this is a really great and concise bunch of information to follow for people new to the process.
Also - best 1st post in a forum, ever. 8)
RigoCL
Greenskin
Posts: 114
Joined: Mon Oct 14, 2013 1:41 am
Location: Chile
x 3

Re: Exporting an animation with Blender

Post by RigoCL »

Great job, I will take a look at it to check if I know all I should.

Thanks.
Integrated: Ogre3D + dotScene (Blender loader) + MyGUI (UI) + RakNet (Client/Server) + Leap Motion (The future is here!) + StereoManager (3D Anaglyph red-cyan)
WIP: StereoManager (Real 3D) + CCS (Camera Control System) + Sound, experimenting with Android.
narcberry
Gnoblar
Posts: 1
Joined: Thu Mar 20, 2014 5:46 pm

Re: Exporting an animation with Blender

Post by narcberry »

I have my first animation working now! Thank you.
I assume that you have a little Blender knowledge.
I'm very new to Blender and Ogre, here are the additions, changes and comments I have for the people that have less than a little Blender knowledge:
  • * The new action you created is what you call "Idle" (I'm sure this was obvious to everyone else, but your wording confused me a little)
  • * When animating the new action, it needs to be done in the Pose editor. If you animate from object mode (an easy mistake if you're just testing this process out on a single bone, like me), you will get an animation in Blender but nothing in Ogre3D, those translations and rotations are simply ignored
  • * The animation timing is 60 frames per second
  • * You still need to manually increment the animation time each frame with mAnimationState->addTime(). It takes seconds.
I spent an embarrassing amount of time troubleshooting the third one. This is a really simple process, if it isn't working you are making a simple mistake like me.

Thanks for the help, St0rr
zuuka12
Gnoblar
Posts: 5
Joined: Fri Aug 16, 2013 6:39 pm
x 1

Re: Exporting an animation with Blender

Post by zuuka12 »

Awesome work, thank you!
i have struggled with this myself.
Explained really nice.

Thanks!

Blender and Ogre 3D, both best in Open Source and Crossplatform!! :)
RigoCL
Greenskin
Posts: 114
Joined: Mon Oct 14, 2013 1:41 am
Location: Chile
x 3

Re: Exporting an animation with Blender

Post by RigoCL »

Great tutorial.

I think it would be ok to add here something I learned the hard way, and that is related to how the mesh will be displayed in Ogre. By default, all meshes are exported with flat faces, meaning that all the edges will be visible, so if you want smooth nice looking meshes you have to tell so to Blender, the right way is to select the mesh you want to look smooth, then switch to Edit Mode, then select the menu option "Mesh", then "Faces" and finally "Shade Smooth". Hope this helps.
Integrated: Ogre3D + dotScene (Blender loader) + MyGUI (UI) + RakNet (Client/Server) + Leap Motion (The future is here!) + StereoManager (3D Anaglyph red-cyan)
WIP: StereoManager (Real 3D) + CCS (Camera Control System) + Sound, experimenting with Android.
zuuka12
Gnoblar
Posts: 5
Joined: Fri Aug 16, 2013 6:39 pm
x 1

Re: Exporting an animation with Blender

Post by zuuka12 »

Really?
Thought this would be Blender internal only the "shade smooth" option.
What about Vertex edge split, doesn't this do the job?

regards.
RigoCL
Greenskin
Posts: 114
Joined: Mon Oct 14, 2013 1:41 am
Location: Chile
x 3

Re: Exporting an animation with Blender

Post by RigoCL »

zuuka12 wrote:Really?
Thought this would be Blender internal only the "shade smooth" option.
What about Vertex edge split, doesn't this do the job?

regards.
Thought the same, but as mentioned, I learnt the hard way that I had to set it manually.

BTW, don't know what "Vertex edge split" is.
Integrated: Ogre3D + dotScene (Blender loader) + MyGUI (UI) + RakNet (Client/Server) + Leap Motion (The future is here!) + StereoManager (3D Anaglyph red-cyan)
WIP: StereoManager (Real 3D) + CCS (Camera Control System) + Sound, experimenting with Android.
Post Reply