Saturday, 7 November 2015

TUTORIAL - Principles of Blender Rigging and Weighting - Part III

This is the third part of a tutorial series on Blender rigging and weighting and assumes you've already read Part I and Part II.

In Part II we learned about vertex group binding, bone groups, and then looked at the effects on a mesh that is weighted to only a single bone. Today we're going to work with a mesh that is weighted to two bones.

While it might not be immediately obvious from the simple 2-bone armature we're using, once you understand the fundamental principles that govern these two very simple cases you'll be in a position to understand the behaviour of the entire SL armature.

Open the .blend file you saved at the end of Part II, then make only layer 3 and layer 6 visible. Layer 3 should have the as-yet untouched "body" cylinder mesh on it, and layer 6 has your 2-bone armature. 
  • If you've saved the file with any translations to your armature's bones, zero them back to the "T-pose" straight upright position.
  • Make sure your armature is in Pose mode
  • Select your "body" mesh and switch to Weight Paint mode 
  • On the Toolshelf's Options tab, in the Options section enable X Mirror
  • On the Toolshelf's Tools tab, make sure "Auto Normalize" is enabled in the Brush section
Unlike the previous two times we began to add weights to a mesh, this time we need to start with Selection Masking disabled so if for any reason you have either Face Selection Masking or Vertex Selection Masking enabled, disable them.

The reason we want masking disabled is that we need to select both bones of our armature for the first step of today's tutorial. When masking is off you can use the "A" hotkey toggle to select/deselect all bones. When either masking mode is enabled, that hotkey's function switches to select/deselect all faces or all vertices. Use "A" to select all bones.

In Part II we manually painted our weights onto the mesh which can be a very lengthy process and tends to require a lot of smoothing and tweaking. It's not a big deal when working only a bone or two, but when working with an extensive armature it would be a nightmare to manually add and weight dozens of bones. Happily, Blender has a number of tools to automate that process.

In our situation, there are two options to automatically generate weights. Both base their weights on the each vertex's proximity to the armature's bones. One of them looks at the actual bone location and shape. The other looks at the bone envelope. In almost all cases you're likely to encounter, the former method produces far better results so that's what we'll use.

From the 3D View pane's menu at the bottom, select the "Weight > Assign Automatic From Bones" option. Presto! You just saved yourself a lengthy manual painting exercise.

If you look over at your mesh's Data Object tab in the Properties pane you'll see that both the mPelvis and mTorso bone groups were automatically added to the Vertex Group list. One of the two will be selected for you, but you can select back and forth to see that each of your mesh's vertices were each given weights for either one, or the other, or both.

The vertices in the lower portion of the mesh will have mPelvis weights of 1.0 and mTorso weights of 0.0. The upper portion will have mPelvis values of 0.0 and mTorso weights at 1.0. An overlapping region in the middle will have weights for both, ranging fairly smoothly between 0 and 1, where the sum of the mPelvis value and mTorso value will always equal 1.0. That's because this process is automatically normalized for us.

Crtl-select the mPelvis bone and rotate it. You'll see the entire mesh moving along with your rotation which is exactly what you'd expect it to do (based on our experiences from the previous parts of this tutorial) because the mTorso bone is attached to mPelvis.

Now crtl-select the mTorso bone and rotate it. Only the upper portion of your cylinder will bend (again, as expected).

What you'll probably notice almost immediately is how much smoother that bend appears to be compared to what you saw in Part I of this series when we used bone envelope binding. If we'd used the "Weights > Assign From Bone Envelopes" option to generate our weights instead of the bones themselves, we would have exactly duplicated the effective weights we saw with bone envelope binding.

As you increase the amount of bend in the joint you'll see that the mesh collapses into a creased seam and overlaps. Go in world and watch your body move as you play animations...every single joint is doing exactly the same thing. The sharpness of the crease at the bend depends on how smooth that transition is for the weights of the two bones. Similarly, when you bend something backwards the vertices are spread apart, which of course means that the texture on those faces will stretch as well. The smoother that transition is, the less distortion you'll see on the texture. There's no way to eliminate distortion...all you can do is try to minimize it.

For a cylinder like ours that is perfectly symmetrical and centered exactly around the bone, the weights generated by assigning them automatically from the bones themselves will be very close to perfect -- or as perfect as you can get with a mesh and bone armature system.

If you compare the behaviour of the vertices on this inside part of a bend to the way your skin moves when you bend your elbow or knee, you'll see that the mesh bend has a tendency to indent somewhat whereas your skin doesn't. Similarly, the outside part of the bend tends to collapse inward somewhat toward the pivot point of the joint whereas your body doesn't.

That's (very loosely speaking) because your skin behaves quite differently when bending and stretching than any mesh can. It's also much, much higher "geometry resolution" than our cylinder is (millions of cells instead of a small handful of vertices), and there's muscle, fat, tendon and overall bone mass under the skin that limits how the skin can move.

When weighting a joint's transition between two bones, it's possible to tweak these initially-generated weights to make it appear a little more natural. Let's try doing that.

Zero your armature bones's translations if it currently has any, then ctrl-select the mTorso bone and use the hotkey combination RX60 top bend it forward by 60 degrees. If you look at it from the right-hand side (ctrl-numpad3) it should look like my picture above. When you did that, the mTorso bone group was also automatically selected in the Data Object pane so those are the weights we'll be working with initially.

To make it easier to see the effect of tweaking we're about to do, enable Selection Face Masking so the mesh's structure is visible. For the work we're about to do, I also find the combination of Octahedral bone dispaly and X Ray view can make it quite hard to see what I'm doing, so I will normally select my armature in the Outliner Pane and either switch it to stick display or disable X Ray (usually I leave X-Ray on and just use stick bones). Then select your mesh in the Outliner pane to return to it.

Select the "Add" brush and ensure that "Auto Normalize" is enabled. This ensures that when we add weight to the mTorso bone it will automatically subtract a corresponding amount of weight from the mPelvis bone to keep the sum of all weights always equal 1.0.

For weight tweaking, we want a brush strength that is very small, otherwise the changes from even a single click will be too drastic. I'd suggest a strength value of 0.005 to start with for what we're about to do. If you later switch to the Subtract brush, you'll need to change its strength as well (although there is an option to have strength settings automatically apply to all brushes instead of each brush individually).

Zoom in fairly close to the joint and adjust your view so you're looking at it from about a 45-degree angle that is also slightly elevated or slightly lower, then start adding a bit of mTorso weight to the vertices that already have a large amount of mTorso weight (the part above the joint). You'll notice that this pulls the mesh back out, reducing that indent.

You can do this to any part of the mesh that's above that neutral mid-point of the joint to help the upper torso keep its cylindrical shape with less of a depression leading into the joint.

For the portion of the mesh below the mid-point of the joint, either switch to the Subtract brush or switch to the mPelvis bone and continue to use the Add brush. Adjust the weights there to help reduce the indent for that area.

For the exact mid point of the joint there is absolutely nothing you can do...all you can adjust is how rapidly it creases (how sharp it appears) and, to some degree, where it creases. Sharper creases are often more natural looking for many human body joints, although you'll never be able to get it to perfectly resemble human body motion throughout a joint's entire range of motion.

Assuming you still have the X Mirror option enabled (we set that in Part II on the toolshelf's Options tab), any change you make on the left side will be mirrored for the corresponding vertex on the right side, and visa versa.

The back of the mesh where the bend of the joint it stretching the mesh can also be tweaked, although you still can't change the affect of the "pull in" at the mid-point of the transition.

You'll notice that any time you do a brush stroke, if you keep your mouse button held down it won't have any further effect on the vertices you've already tweaked earlier in the stroke. You have to release the mouse button, then press it again to further tweak those vertices.

My personal preference is for my brushes to behave in a more air-brush-like way but I suspect that's a matter of personal preference and habit. If you'd like to try that, enable the "Spray" box in the Options section of the Toolshelf's Options tab. If you do, you'll probably want to reduce your brush strength a little until you're used to it.

As you get closer to a more natural looking bend (for a body joint) you'll likely want to switch to the Blur brush and so some smoothing out of the weights.

You will probably want to extend the area that both bones have weight which will help to smooth out the transition between no-weight and some weight, particularly on the inner part of the bend.

After a little while you can probably get a somewhat more natural looking bend as shown here. When you're a beginner it will take longer and probably look less smooth. In time you'll develop a feel for it and be able to get better results and do so more rapidly.

I encourage you to play around with this very simple mesh and armature, using both bones and even trying some very extreme "wrong" things just to help you start to get a feel for how adjusting the weights affects the way a mesh bends with motion of the armature.

So far we've only been working with a very basic two-bone armature and extremely simple mesh cylinder object. Even though it bears little resemblance to anything we encounter in-world, it does let us isolate the conceptual foundation of bone interaction and weights. In Part IV we'll be looking at a far more concrete and meaningful example: the avatar.

Some General Weighting Advice for Beginners

When tweaking, start with coarser (higher) strengths if you need to make large changes, then gradually reduce the strength as you get closer and closer to the desired end effect.

I rarely have the Add or Subtract brush strength set any higher than 0.010 even for major adjustments, and tend to do most of my tweaking work using values in the 0.0005 to 0.002 range.

For the Blur brush, I normally use strength values in the 0.002 to 0.010 range or for very coarse changes perhaps as high as 0.025 or even 0.050.

As you're working, remember that you can adjust your brush size using the Radius setting. When working with large areas you might like a much larger brush whereas for fine, detailed work you might want a very small brush. For the most part I work with a brush radius in the 50-65 pixel range and do a lot of zooming in and out, but that might be a matter of personal preference.

When working on an area with two bone weights, remember that adding to one bone's weight is identical to subtracting from the other's. I tend to swap back and forth between bones by ctrl-selecting them and work with just the Add and blur brushes. You may find it more intuitive to swap back and forth between the Add and Subtract brushes, instead, keeping the same bone group selected.

As you're working on a joint, you'll probably want to periodically rotate it through a range of angles to see how it responds. What looks fairly natural at one bend angle might not look as good at another.

When doing so, keep in mind the natural range of motion a joint would typically move through. You can tweak weights for elbows and knees quite differently than some other joints because they only normally bend in one direction. That doesn't mean that the animations you play will always respect that range of motion (some that I've seen make body parts do things no human could manage) but most are fairly decent.

Weight-painting is as much an art as a science. It's very helpful to know a certain amount about human anatomy and to observe your own body in motion. When you first start to do weighting you'll probably find it very difficult to achieve what you want. Practice makes perfect, though, so as you continue to work with weights you'll begin to get a good "feel" for it and be better able to get better results and get them more rapidly.

No matter how good you are, it's never going to be perfect when you're trying to mimic the behaviour of a human body. Some areas are almost impossible to duplicate just due to the nature of mesh structures and armatures. Most often you shoot for "best possible in the time I'm willing to put into it" and give yourself some sort of time limit that fits with your personal patience threshhold.