Tuesday, 10 November 2015

TUTORIAL: Principles of Blender Rigging and Weighting - Part IV

This is the fourth pat of my series of tutorials introducing the fundamentals of rigging and weighting in Blend for the SL/Opensim avatar. It assumes you're at least somewhat comfortable with the material presented in Part I, Part II and Part III.

Today we'll be taking the big jump from the simple pair of bones we've been playing with in the earlier installments and begin looking at the entire SL Rig to get a far more concrete feel for how this works.

Making this discussion a little complicated, the avatar we use in-world has historically used three armatures, simultaneously, that are all normally bound together in the way they behave. This means there are 3 complete sets of bones in the "real" rig:
  • the "traditional" armature of 26 bones, sometimes called the "main rig," "standard armature," "skeleton," or "mSet"
  • the "collision" armature of 24 bones, sometimes called the "fitted set" or "caps set"
  • the "attachment" armature of 32 bones, occasionally called the "aSset"
The traditional armature is the one most people think of when talking about the avatar's skeleton. It's the one that has historically driven all motion via the animations supplied by default by Linden Labs and then vastly diversified by content creators.

When you stand, walk, sit, or do anything else, it's almost always being done by animating the  traditional armature. Of the 26 bones in the traditional armature, only 21 are normally used for animation.

While the collision armature has been there all along and has been used by the viewer to help with the avatar's shape syspem, it is only in the last few years that people have really become aware of it and not until 2014 that became a supported armature for rigging and mesh upload.

The collision armature is the one used for "fitted mesh" rigging and weighting. It is bound to the traditional armature so when an animation is played, the fitted set of bones goes along for the ride. It is possible to upload animations that control these, even overriding (to some degree) how they move relative to the main rig, although it's rarely done.

The big advantage of the collision set (for this discussion) is that mesh with this weighting is far more responsive to each person's shape slider settings, although not perfectly so. I'll return to this subject in a future part of this series.

The attachment armature has also been around since the beginning and is used as the anchor points for your worn attachments (hair, boots, bling, etc.). Whenever you attach something, if goes on a bone of this armature (even though you're really not aware that's what is happening).

While it is possible to rig and weight somewhat to this armature, and even to animate it, it often produces extremely unpredictable results (including extreme distortion of the mesh) and is not officially supported by the viewer. The attachment points normally just follow the motion of the corresponding part of the main rig.

For today's tutorial we're going to limit ourselves to looking at traditional armature.

You'll be happy to know that I'm not going to ask you to build an armature from scratch. Instead, we'll avail ourselves of a pre-made .blend file that the folks at Machinimatrix graciously make available as a free public download. Go to the Avatar Workbench page of their website and download the most recent version of it (version 271 as of the time I write this, corresponding to the release of Blender 2.71).

I normally try to avoid making product endorsements, but this is a time where it's worth giving Machinimatrix a plug. If you anticipate doing any sort of extensive work with rigged mesh (custom avatars or clothing) or animation, their Avastar product is quite possibly the single best investment you can make as a beginner. This is an add-on for Blender that will save you countless hours of frustration and has a variety of invaluable features to make your workflow easier and faster...well worth the very reasonable price (under USD $30 or you can buy it at a comparable L$ cost). I use it constantly!

Open the workbench blend file you downloaded, above. We'll want to do a few things before we get started looking at it since we'll only be working with the basic Ruth avatar:
  • Save our working copy of the file as "traditional_rig.blend" or something like that so we can always go back and load the original again another time.
  • Delete the camera and lamp on layer 1...we won't be using them and they tend to get into the way
  • Delete all the "MH" meshes on layer 2
  • Delete all the Ron (male) meshes on layer 3
  • On layer 11 (which Machinimatrix calls layer 6 and I suppose they might be right) delete the skirt, hair and eyelash meshes. Keep the left and right eyeball meshes but move them to layer 1.
  • Return to layer 1 and select the 5 mesh objects that are there now (the two eyeballs you just moved, and Ruth's head, upper body and lower body). Join them and call the resulting single mesh "Ruth"
  • Switch into edit mode (with Ruth selected).
  • Select All, then use Mesh > Edges > Select Boundary Loop to select only the boundaries
  • Remove Doubles which will join the 3 distinct parts of the Ruth mesh into a single piece
  • Return to object mode
  • Go to layer 6 (which Machinimatrix calls layer 11) and select the armature (it's called "Female")
  • Switch the armature into Object mode then use the hotkey combination "R Z -90" to rotate it 90 degrees on the z-axis. Ruth will rotate along with it automatically to be facing us when we use Blender's front view (numpad1) hotkey and is now symmetrical on the x-axis (more about this in a moment).
  • Use the menu option Object > Apply > Rotation to set this as the armature's new base translation, then switch it back into Pose mode.
  • With the armature still selected, go to the Property Pane's Data Objects tab where you can confirm that it's already in X-Ray view mode. Enable the "Names" display too, and if you prefer working with octahedral bones instead of stick ones you can switch to that too (I'll leave mine in stick display mode).
Now we're ready...

Side note: I assume there is some good reason why Linden Labs decided to align the avatar to x-axis forward, z-up orientation but it makes it a pain to work with in Blender since the symmetry is then on the y-axis instead of x-axis. In Blender, you always want x-axis symmetry (if the object is symmetrical) since modeling, sculpting and weighting can take advantage of this to make your life much, much easier. Avastar automatically gives you that working environment and their export utility rotates it back to SL orientation...their workbench freebie file doesn't and is oriented as per the SL source. If we wanted to export something from our current set-up we'd have to rotate it back first.

Let's have a look at the traditional armature's structure of 26 bones (5 of which aren't used). You just want the armature layer to be visible for now.

You'll see that all bones start with the letter "m" followed by a reasonably descriptive name. That's why in the previous tutorials I was somewhat pedantic and used a "m" before the names we gave our two bones.

The root bone of the armature is mPelvis. It is the only bone that is able to be moved as well as rotated. Just was we discovered in our 2-bone example, any movement of the root bone takes the rest of the armature along with it.

Three bones connect to the tail of mPelvis: the torso and two hip bones. The hip bones are a special type of connection where the head of the bone isn't actually attached directly to the pelvis. Instead, they're linked. The exact location of the head of their bones relative to the tail of the pelvis bone is always maintained, almost as though there's an invisible rigid bone connecting them.

Each leg is then a chain of connected bones. If you rotate the hip bone, everything further out along the chain will move, too. If you rotate the knee, the hip remains stationary but everything else rotates. All of this is exactly as you'd expect based on the structure of the skeleton.

Heading in the opposite direction from the pelvis is a chain of bones governing the entire upper body. At the joint between the chest and neck bones are two more of the special "linked" bones that work similar to the hip. The heads of the left and right collar bones will always maintain their location relative to the tail of the mChest bone.Each arm then extends down through shoulder, elbow and wrist.

The neck, head, and skull bones complete the armature, with the two eye bones using the same sort of linked attachment to the tail of the mHead bone that we saw with the hip and collar bones.

As you play with various bones on the armature you'll realize that they behave the way you'd expect a (very simple) skeleton to do, provided you could manipulate it one bone at a time. Unlike a human bone structure, there are no rotation limits so it's up to the person creating an animation to remain within a range that appears reasonable (not all do!).

There is no "inverse kinetics" system, either, whereby you can move a part of the body and have the bones between it and the root automatically rotate to make the pose appear natural. You need to manually adjust the rotations of each bone along the chain, always starting at the pelvis and working your way down along the chains.

In animation, an inverse kinetics (IK) system is a reverse calculation whereby you position and rotate an extremity and the software calculates the necessary rotations required between the root and that body part necessary to allow that movement (if it's possible) based on a set of rules about how the joints typically move. Blender supports full IK and Avastar has both a basic IK implementation for the traditional armature as well as a set of bone rotation limits.

Now zero any translations you've made to the armature, make layer 1 visible as well, select Ruth, and switch into weight paint mode.  I suggest you switch the view mode to show zero weights for Active as black (Toolshelf, Option tab, Options section).

I mentioned earlier in this series that I also prefer to enable face selection masking so I can watch the actual structural changes of the mesh when it moves. That can make it a little harder to view weights of the overall mesh so for your initial look at the body let's leave that off.

For the Ruth mesh to respond to this armature requires that each vertex have at least one bone weight associated with it. Any mesh that contains even a single vertex without at least 1 weight assignment will be rejected as invalid at the time of upload. This ensures that Ruth never shows the issue we experienced in Part I of this tutorial with unweighted vertices refusing to move at all.

If you select the various bones groups in the vertex group list (Properties Pane, Data Object tab or ctrl-click directly on the bone) you will see how the mesh has been weighted to each. The colour-coding is exactly what we saw with our 2-bone weighting exercise in Part III of this series. Now, instead of just 2 bones, we're working with a mesh that has weights assigned for 21 bone groups.

Rotate the bones and watch how the mesh reacts to this movement. If you work your way down one of the leg chains you'll discover that the entire foot of the mesh is weighted to the corresponding ankle bone and that neither the mFoot or mToe bones have any weights at all. You can animate those bones in-world, but the avatar ignores them and doesn't move because none of its vertices is weighted to them. We saw this effect in Part II of the series

The same is true of the mSkull since the entire head portion of the mesh is weighted only to the mHead bone. You can animate rotations of the mSkull bone but the avatar doesn't respond to it because none of its vertices have any weights for it. The skull, feet and toes are the 5 "unused" bones I mentioned earlier. They're there, but unless a mesh is given weights for them any rotations they do will be invisible in-world.

Select the mChest bone and zoom in somewhat. This would be a good time to enable face selection masking to get a very clear picture of what is happening. Due to a saved setting in the blend file, when you enable face selection masking you'll also need to go to the 3D View Pane's properties side tab (hotkey N if it isn't visible) and in the "Shading" section uncheck the "Textured Solid" option.

Now rotate the chest somewhat. You'll notice that there's an incredibly sharp transition (or complete lack of transition at the front!) between mChest and mTorse weighting. When that bone moves, it takes a block of vertices of the upper body with it and leaves everything else behind. This explains the extreme texture stretching (and quite ugly and unnatural movement) in that area.

Continue selecting other bones and look at the transition areas. In most cases there is a single "loop" of vertices that are approximately equally weighted to two bones. Everything one loop "up" will be 100% weighted to the bone, and everything one loop "down" will have zero weight. In some places, there is no transition at all. This explains the overall movement and (IMO) unnatural deformation of your avatar in-world.

Without using a custom-weighted mesh body there is no way at all to change this, and using a mesh body has its own set of challenges that most people will find prohibitive. I'll talk about custom mesh body weighting later in this series.

The relevance of these observations to our current discussion is the effect it has on any mesh clothing (or other rigged mesh items) we might wish to wear. They need to be weighted to this same armature and move with it. Surprisingly, our goal will usually be to have the clothing match the movement of the avatar under it rather than to have it move naturally. This minimizes the amount of poke-through you'll need to hide with alpha masks.

It's worth spending quite a while to become as familiar as possible with the avatar body weights and motion. It can help you avoid clothing designs that stray unnecessarily into "problem" areas and suggest areas where a little extra geometry can help it to move better with the avatar. In Part V we will be looking at this in more depth.

To close off today's tutorial, save a backup copy of this file the way it is right now so we can come back to it; then using another copy see if you can use the add/subtract/blur weighting brushes to smooth out the transition between the mChest and mTorso just like you did with the cylinder in Part III. You'll want to enable X Mirror in the brush options and adjust the brush weights before you start.

Remember to look at the mesh under a variety of rotations, ensuring your changes improve it for rotations on all three axes. Typical rotations for the chest bone that you're likely to see for in-world animations are:
  • x-axis from -35 to +45
  • y-axis +/- 35
  • z-axis +/-50
Don't spend too much time trying to improve it...just play with it for a little while to get a sense for it. For now, try to avoid letting any vertex have more than two bone weights (although this will be difficult). The goal is simply to get a bit of a feel for it, and get a little more practice with the basic skills you learned in Parts II and III.