Thursday, 5 November 2015

TUTORIAL: Principles of Blender Rigging and Weighting - Part I

This is the first in what will need to be a multi-part tutorial on the subject of rigging and weighting in Blender for Opensim. You'll need at least a moderate familiarity with Blender and by the end of the series we'll be moving into somewhat advanced user territory.

In Part I, I hope to show you the basic principles that underlie the system so you'll have a solid foundation in the mechanics of animation in our environment. I'm using the recently-released Blender 2.76b public release but everything I'll be talking about has been around in one form or another since 2.6x (if not earlier).

The subject of rigging and weighting is more generally the subject of armatures and animation, and that's where we need to start. The purpose is to be able to animate something -- to move it in-world using the animations we have in our inventory.

Opensim only allows the animation of one thing: the avatar and anything attached directly to it (something you wear). It does so by moving an imaginary, invisible, stick figure skeleton inside our avatars. In Blender terms, this skeleton is called an "armature."

Unlike real life bodies that use muscles to move, the movement of our avatars is controlled by a set of instructions carried in a file -- the animation -- that tells the overall skeleton where to position itself and then tells each individual bones of the skeleton how to bend at each of its joints.

Our real life bodies have skin stretched over muscle (and fat :p) but our virtual ones don't. Instead, there's a big empty space inside our bodies and the movement of the avatar is governed strictly by the movement of the nearby bones.

I'll be coming back to this in later parts of this series, but for for this section I want to introduce the extreme basics so let's not think about a whole skeleton for a while and just consider a couple of bones. We'll do it with show and tell just to help you really get a good grasp of it.

Open Blender and start with a completely empty scene in Object mode. From the 3D View pane's menu select Add > Armature > Single Bone and you will see an odd looking shape added to your scene. It has a sphere on either end and a sort of elongated pyramid shape connecting them. Blender calls this shape the "Octahedral" view of a bone. More on that in a moment.

You'll notice that when it was added to the scene it was given the default name "Armature" but just to remind ourselves, let's rename that to "skeleton" since that's the name we're a little bit more familiar with in real life.

Now switch into edit mode and make sure your properties side panel is visible (hotkey: N). There are a few things to take note of right away:
  • In the item side panel's "Item" section you'll see that the object has the name skeleton that we just gave it, but that it also has a little bone symbol under it with the name "Bone."
  • At the top of the panel in the "Transform" section, you'll note that in the our bone has a "Head" and a "Tail" and some there that aren't ones you'll be familiar with from modeling with mesh. Those are armature-specific things
  • You can click on the top sphere of your "Bone" to select just it rather than the whole bone
  • Similarly you can click on the bottom sphere to select only it
  • If you click on the main octahedral part of the bone you select the entire thing, including both end spheres.
This is the basic visual structure of the bone on the screen but in terms of the way Blender thinks about it this bone is really just two points in space that are rigidly connected to each other.

Click on the bottom of the two spheres and try moving it around the screen just like you'd move a vertex of a mesh. You'll notice that the main bone part automatically stays attached to it, lengthening or shortening as necessary to keep the two ends joined by a straight line. You'll notice that the "Head" transform values are updating to indicate where in the scene that bone is located.

Now try rotating the head and you'll find that nothing happens. The head of a bone is unaffected by rotation.

Put the head back at <0,0,0> and now select the sphere at the other end -- the tail. You can move it around too and the same bone length adjustment happens that we saw when we moved the head, except now it's the tail values that are changing in the transforms panel.

Now try rotating it. Unlike rotations of the head, our tail bone rotation is making the entire bone rotate and uses the head as its pivot point. There's actually one more little rule here for rotations which we'll see shortly.

Put the tail back to <0,0,1> and now select the octahedral (big part) section which will highlight the whole bone. If you move this, it moves the entire bone. If you rotate it, it will rotate around the center of the line connecting the head to the tail.

There's lots more to learn here if you were designing an armature from scratch, but we don't need to concern ourselves with that because Opensim/SL doesn't support anything other than the one basic avatar armature.

For now, but the bone back to where it was if you've moved or rotated it, and rename it "mPelvis" in the panel's Item section.

Now select just the tail again (the sphere at the top) and extrude it by 1m in the z-axis (E Z 1.0). Extruding a tail in an armature adds a new bone, with its head at the previous bone's tail position (and attached to it) and it's tail at wherever you extrude it to. It will be given the name "mPelvis.001" by default but let's rename it to "mTorso" instead.

If you'd like to play a little by moving and rotating the new bone's head, tail, or whole bone you can do so. Just return everything back to its original location when you've finished. The one thing you might find quite interesting is that this new connection between the two bones has made it impossible to rotate your original bone's tail because it's also now the head of the second bone.

Again, if you were going to making an armature from scratch we'd spend a lot more time talking about this subject (you could easily fill a book with it!) but for our purposes we don't need to because we're just looking at some basic principles.

Our Blender armature now has two bones in it. The human skeleton contains 206 bones (270 at birth) but you'll be happy to hear that we can't going to try to add all of those.

The Opensim avatar uses a far simpler armature with only 26 bones in it, and only 21 of those are actually used in 99.99% of the animations you'll find. It's a gross (over)simplification of the human anatomy but at the time the system was being designed I suppose Linden Labs felt that was enough to do the job well enough.

They didn't bother to include finger bones in their armature because they use another method (called vertex group morphing) to clench them. Most modern games *do* have that in their armatures but that adds about 30 bones to the armature (3 per finger/thumb). They simplified the entire spine into just 3 pieces: the torso, chest, and neck which is the reason our VR bodies move so....unnaturally.

We'll return to the SL armature later in this series. For today we're just going to focus on the 2 bones we've just created: our "mPelvis" and "mTorso" bones, each of which is 1m long and are in a straight line.

In the Properties pane at the far right, select the "Data Object" tab -- it looks like a little stick figure person. In that pane's "Display" section you'll see that "Octahedral" is currently highlighted, telling Blender that we want to see our bones as octahedrons. That isn't the only way to display them, though.

Try clicking some of the other options to see how your bone shapes change. I frequently work with "Stick" display for them instead of octahedral, and if you ever get into making your own armatures for non-Opensim projects you might find the other display modes useful as well. I'd suggest leaving it in either octahedral (which I'll do) or stick display mode for this tutorial.

You can also check the "Names" box to see the names you've assigned to each bone displayed in text beside them (which can be helpful at times) and I almost always check the "X-Ray" box so let's do that too. You won't see it have any effect for X-Ray right now, but this allows us to see the bones through any other mesh objects in the scene which makes it much, much easier to select them later when we need to.

When you're done, switch back into Object mode and you should have something that looks like this. This is an extremely basic armature -- just a pair of connected bones -- but it's perfect for our purposes in this tutorial.

We're not going to try modeling the corresponding part a human body to use with our armature because we don't need one...instead, we'll just use a basic cylinder as a stand-in.

Add a cylinder to the scene with no end caps, a 2m length, a 0.25m in radius, and offset by 1m on the z-axis so it matches our bone positions. The 32-vertex default is fine. If your armature isn't in X-Ray display mode it will be hidden by this cylinder.Switch into Edit mode and loop-cut it 19 times (ctrl+R 19) to give it some vertical geometry that we'll need. Then switch back to Object mode.

With the cylinder selected, go to the Properties pane's Modifiers tab and add an "Armature" modifier (it's in the "Deforms" class of modifers) then assign your skeleton object as its armature. We are going to temporarily bind it to both Vertex Groups and also to Bone Envelopes so ensure both of those check boxes are selected.

Unfortunately Opensim doesn't support Bone Envelope binding so we will need to do all of our work with vertex groups, but for our initial exploration we can temporarily use it.

Now select your skeleton armature and switch into Pose mode. Unlike Edit mode, you'll immediately notice that in Pose mode you can only select an entire bone (highlighted in blue).

If you do a little experimentation you'll discover that you can only move the root bone of our armature (the one we called the "mPelvis" bone) and, when you do, it moves the entire armature and cylinder mesh.

You can rotate either bone. If you rotate the mPelvis bone the mTorso bone goes along for the ride, as does the entire mesh that's bound to the armature. If you rotate the mTorso bone, only it will rotate, pivoting at its joint with the mPelvis. More importantly, only part of the mesh will follow along with the bone's movement: the parts that are closest to it. Give the mTorso bone some rotation so the cylinder is bent over at the middle by somewhere between 45 and 90 degrees.

At the moment the mesh's response to the bone movement is coming from the binding to the bone envelopes. You can confirm this by un-checking the "Bone Envelopes" binding in the cylinder's armature modifier. Turn that off and the mesh will snap back to being a perfect cylinder. Turn it back on again and it bends over again to approximately match the bend of your armature.

Let's do one more thing and then talk about what's happening. Select your armature, go to the Properties pane's Object Data tab again, and switch the display mode to "Envelope" instead of octahedron. Now select both bones (either "A" hotkey or you can shift + select to select multiple bones). You should now have something that looks like this picture.

The white halo that surrounds each bone is a visual representation of its "envelope" which might be better described as its area of influence. When a mesh is bound to an armature using bone envelopes,
movement of a bone will drag along any vertices that fall within that area of influence. Vertices that don't, won't. In the overlapping area between two bones, both influences "compete" with one another and their relative strengths determines how far the vertex moves.

This competing influence calculation is performed for every vertex to determine how the mesh flexes, stretches and bends as bones move. In Blender terminology, this is the process of comparing "bone weights" where each vertex has a set numbers assigned to it: a weight value for each bone.

Bone envelopes are a (relatively) imprecise and coarse method of calculating weights. It's an "either-or" method where a vertex is given a weight value for each bone based on whether it's inside the bone's white-halo area of influence. If it is, it gets a value of 1.0 for that bone. If it isn't, it gets a 0.0.

With our little example, here, most of the vertices in the lower half of the cylinder have bone weights of 1.0 for mPelvis and 0.0 for mTorso. The vertices in the upper half are the reverse, with a weight ot 0.0 for mPelvis and 1.0 for mTors0. Then there's a narrow band of vertices in the middle that have a value of 1.0 for both.

When Blender makes the calculations for how the mesh moves, it does something called "normalizing" which causes it to adjust the weight values proportionally until the sum of all bone weights on a vertex is 1.0. That means that the effective bone weights in that narrow overlapping region end up being 0.5 for mPelvis and 0.5 for mTorso.

When that mTorso bone moves, any vertex that has a 1.0 mTorso weight value will follow along with it perfectly. Any vertex that has 0.0 mTorso weight won't move at all, and any weight in between will cause the vertex only to move a portion of the distance it would move if it were fully weighted.This explains why the cylinder bends the way it does when you rotate the mTorso bone.

But what about that rotation of the mPelvis bone? Why did *everything* move instead of just the vertices with mPelvis weights? It's because of the armature linkage rules for bones. Roughly, the rules go like this:
  1. Every armature must have a designated root bone. You can think of this as being analogous to the root prim of a linkset when you're building in world.
  2. When the root bone moves in any way (an actual move translation, or a rotation) everything goes along for the ride.
  3. Every other bone in the armature is a "child," either of the root bone or of another child bone. This often results in a chain of children leading away from the root.
  4. Child bones can either be attached or detached. In the standard SL/Opensim armature they are all attached.*
  5. An attached bone can only rotate, which it does by pivoting its head on the tail of the bone it's attached to. (It can also scale but don't get into the habit of doing that because it produces very, very strange results in world.)
  6. Any bone attached to the tail of another bone will follow any motion of that tail.
*Strictly speaking this isn't entirely true but for the time being let's pretend that it is.

So, using these rules, we know that our mPelvis bone is our "root" because it was the very first bone we placed and then, when we extruded the new mTorso bone from its tail, that new bone is a child. When the tail of the mPelvis bone moves, all children (in this case mTorso) move along with it.

This means that the only time that our cylinder will bend is when the angle or rotation between mPelvis and mTorso changes.

Play around with this a little bit for now, then save this file since we'll be starting from here for Part II of the series where we will remove our bone envelope binding and learn how to assign and weight using vertex groups.

Some tips for you for when you've selected an armature and are in Pose mode:
  • you can select more than one bone by shift-selecting
  • the "A" hotkey is a toggle between selecting all bones and selecting none
  • the hotkey ALT+R will zero all selected bones' rotations back to where they started (in effect its "T-pose" which is its shape when you finished creating it in Edit mode)
  • the hotkey ALT+G zeros the root bone back to its starting position (not starting rotation though) but has no effect at all on any other bone (unless it's detached)
  • the hotkey ALT+S will zero the scaling of any selected bone