Friday, 6 November 2015

TUTORIAL: Principles of Blender Rigging and Weighting - Part II

This tutorial is the second part in a series to introduce the subject of rigging and weighting mesh for use in Opensim.  It assumes that you have the simple 2-bone armature and cylinder file that we created in Part I open in Blender.

In today's installment we'll be talking about vertex groups which is the binding method required for using rigged mesh in SL/Opensim whereas in Part I we were "cheating" but doing our initial exploration using bone envelope binding.

Happily, the underlying principle for vertex groups is extremely similar but allows much greater control and precision over weights. With a certain amount of experience (and patience) vertex group weighting methods can correct the many ugly flaws you typically see with bone envelope weights because it allows direct control over the weight given to each and every vertex of your mesh.

Unfortunately, it also means that each and every vertex of your mesh has to be given bone weights. If you had to do that manually, it would be a chore even for a very simple object like our 672-vertex cylinder. Luckily that's not necessary. Blender provides a variety of tools to rapidly create weights for an entire mesh -- ones that give a pretty good starting point for further tweaking. It also has tools that let you manipulate the weights of large groups of vertices all at once, in a fairly controlled and predictable way.

In Part I we talked about the way Blender was figuring out how to bend the cylinder when you rotated the mTorso bone of your armature. It was looking at the bone envelopes of each bone and assigning a "weight" for each bone for each vertex of the mesh, then comparing them to determine whether -- or how much -- it should move.

Vertex group binding does exactly the same thing, except instead of doing this on-the-fly calculation you actually assign each vertex's bone weights in advance. Then when you bend the armature, Blender looks at the weights you've assigned and figures out how the mesh should move.

To be able to do this, we need some way to set and store each vertex's weighting data. That's where vertex groups come in. You could think of a vertex group as being something like an advanced "tag" that you can give to a vertex just like I added tags to this tutorial post to say that it's about "Blender" and "Tutorial" and "Opensim." It's a set or "group" that you create and then add individual things -- like vertices -- as members of that group. A vertex can be a member of multiple groups, just like a blog post can have multiple tags.

With our simple armature and cylinder we need two groups of vertices: a list of which ones should be affected in some way by the mPelvis bone, and a second list of the ones that should be affected by the mTorus bone. Some of our cylinder's vertices only need to belong to one of those groups, while the ones near the joint of the bones need to belong to both.

In addition to keeping track of which groups each vertex belongs to, Blender also stores how strong a member it is -- something you can't do with blog tags. This strength value is exactly the value we talked about in Part I, a number between 0.0 (not a member) and 1.0 (full member). Those are the numbers needed to figure out how to bend the mesh when we bend the armature.

Vertex groups actually have hundreds of other applications in Blender. You can, at any time, create a new vertex group with any name and assign any vertex and any membership weight to it. Those groups can then be used for modifier and many other nifty things. When working with armatures, we need to use very special vertex group names: names that are identical to the bone names.

Whenever you create an armature and start adding bones to it, Blender automatically creates a matching vertex group to correspond to the bone name. When created the armature and gave its bone the name mPelvis, Blender automatically created a vertex group called mPelvis too. Then when we added the mTorso bone, it created another vertex group with that name too.

In Blender terminology we call these "bone groups" as a short way of saying "vertex groups that are specially created to exactly match the bone names of our armature and are bound to the armature via the armature modifier" (which would be quite a mouthful and tedious to type). In an all-too-rare surprise, Opensim/SL uses the same terminology.

However, when we made the cylinder Blender doesn't know whether any of its vertices ought to be members of either of those groups because it didn't know whether we intended to animate it. As far as Blender knew we might have been intending to model it into a tree to put into the background of a scene.

When we added the armature modifier to the cylinder we let Blender know that we intend to animate it, but Blender still doesn't know how we want each of our vertices to respond to the bones so it's waiting for us to tell it which vertices ought to belong to each of those currently-empty bone groups and what weights they should have.

The only reason our cylinder bends right now is because we also told it to respond to bone envelopes. Let's have it stop doing that so we can clearly see what's happening as a result of our vertex groups (and because Opensim doesn't use bone envelopes for animation). Select your cylinder, go to the modifiers tab, and un-check the bone envelope box.

If your armature's mTorso bone was bent like mine is in the picture, you'll see your cylinder snap back into its original shape. That's because it doesn't have any vertices assigned to any of the bone groups yet. A vertex with no bone group weights completely ignores anything the armature does.

We can test that even further by selecting our armature in Pose mode and then playing with the bones. You can move the mPelvis or rotate or scale either bone...the cylinder will just sit there doing nothing. Until we assign bone weights to our vertices, they'll ignore any pose changes.

For what I'd like to show you in this tutorial, it would be useful to have several copies of our cylinder so we can do some comparisons. Select your cylinder in Object mode and duplicate it twice, moving the first copy to layer 2 and the second copy to layer 3. Let's give them each a meaningful name so:
  • rename the cylinder on layer 1 to "pelvis_only"
  • rename the cylinder on layer 2 to "torso_only"
  • rename the cylinder on layer 3 to "body"
  • select the armature ("skeleton") and switch it back into Object mode if it isn't already
  • move the skeleton to layer 6 (or any other empty layer you want)
  • now switch the armature back into Pose mode (to move an entire armature we had to be in Object mode)
  • if you still have the armature in Envelope display mode, switch it into Octahedral (or Stick, if you prefer)
  • if you don't have X-Ray display enabled for the armature, enable it now because we'll need to be able to see those bones easily while working with our mesh
  • if you've applied any translations to either bone, zero them (most easily done by selecting both bones then using the menu Pose > Clear Transform > All which doesn't have a hotkey alternative)
  • now make only layer 6 and layer 1 visible
We're doing this because it's important to understand the underlying principles of how bone group binding affects our mesh so I want to show you the effect of weighting the entire mesh to the mPelvis bone first, then what happens if we weight the entire mesh to the mTorus bone, then finally we'll work with the "body" copy of the cylinder to do work with both, which is what you'll do with 99.99% of the rigged mesh objects you make.

Incidentally, the term "rigged mesh" as it applies to SL/Opensim just means "a mesh that has valid bone group assignments and weighting for all of its vertices." It's a mesh that will animate along with our avatar when worn. "Rig" is just a slang 3D industry term for "armature."

Select your "pelvis_only" cylinder mesh and switch it from Object mode to Weight Paint mode. It will turn a deep blue colour and you'll notice that your toolshelf has a lot of stuff you've never seen before. Your mouse pointer has also changed and is no longer a pointer when it's in the 3D View's a brush (like a paint brush in Gimp/Photoshop or the brush you might have used if you've already done some sculpting or texture painting in Blender).

Let's begin by changing three settings since that will help us see what I'm talking about as we proceed.
  • on the toolshelf's "Options" tab, in the Options section put a check-mark in the X Mirror box. That will save us some time later by having anything we do to weights on the left side of of the cylinder be automatically mirrored on the right side and visa versa (just like x Mirror does in Edit mode)
  • In that same section, change the "Show Zero Weights" selection to "Active" which will make your entire mesh go black instead of deep blue. I'll explain why in a moment.
  • Finally, in the menu bar at the bottom of the 3D Pane select the "Face selection masking" button which will now cause your mesh to show dotted white lines along its edges, looking much like it does when you're in Edit mode.
I find it very useful to be able to see the exact effect weights are having on the mesh structure which is what face selection masking mode shows very clearly. It also allows several other weighting actions that aren't possible unless you're either in this mode or vertex selection masking mode (vertex masking is the button beside face masking). I almost always work in one of these two modes.

If the mesh structure is show as a sort of dull white-grey instead of bright white, it's probably because when you were last left Edit mode you left the mesh with only a single vertex selected instead of all of them. While in weight paint mode you can use the "A" hotkey to toggle between select all and select none. We want to have all face selected. (You can also use hotkeys for bounds select (B) and circle select (C) in weight paint make any other select you need to press TAB to switch to edit mode, make your selection, then TAB again which will revert to weight paint mode.)

The X Mirror option is only really useful if your mesh is symmetrical and centered on the x-axis. You can switch this on and off as needed when working with a mesh that is only partially symmetrical.

Now switch back to the "Tools" tab of the toolshelf. At the top of the panel is the Brush section where you'll see a large picture/icon indicating that the "draw" brush is currently selected (or will be by default). Click the icon to bring up a list of other available brushes, and select the "Add" brush.

The brush's setting appear just below it. Leave the weight set at 1.0. You can change the radius to adjust the size of the brush (the diameter of its circle on your screen). Reduce the strength setting to about 0.1. Also put a check in the "Auto Normalize" box since this is a good habit to get into. You'll almost always want auto-normalize enabled when working with the SL armature.

Blender has a variety of different brushes but for most or your work for Opensim meshes you're likely to use only three of them:  Add, Subtract, and Blur. Each one usually has slightly different settings, although the ones for Add and Subtract are identical except have the opposite effect.

Now Ctrl-select the mPelvis bone (hold down the Ctrl key and click anywhere on the bone's octagonal shape). It should be highlighted in bright blue and your mTorso bone shouldn't. If for any reason that didn't work it's because you forgot to put the armature into Pose mode and will need to do so.

In the Properties Pane (far right side of your screen) switch to the Data Object tab instead of the modifiers tab. Make the "Vertex Groups" part of that pane visible if it isn't already. Currently the list of vertex groups is empty because our mesh doesn't yet have any bone weights.

Now we're ready to play!

Draw with your brush somewhere on your mesh, perhaps starting at the bottom. You'll see a few things happen:
  • the part of the mesh you just drew on will become a deep blue colour again
  • if you brushed on the left side of the mesh the corresponding part of the mesh on the right side will also do the same thing (or visa versa) thanks to our X Mirror setting
  • in the Data Object pane the "mPelvis" group was just automatically added to the list of vertex groups used by this mesh and is highlighted to indicate that the group you're working with is the mPelvis bone group
  • if you keep painting over an area you've already painted on, something that was deep blue will become lighter blue, then green, then yellow, and eventually red if you keep painting over it enough.
Let's talk a little bit about what's going on here.

When you ctrl-click on a bone in your armature, Blender automatically selects it for you in the Vertex Group list in the Data Object pane. If it isn't already listed there, as soon as you begin to paint it will automatically be added. If it does already exist, Blender will select it (by highlighting it) in the list.

Whatever group is currently selected in the Vertex Groups list is the one that you're painting weights for. You always have to have one selected. If not, Blender will try to guess which group to use and add it automatically.

When using the Add (or Subtract) brush, the "Strength" setting of the brush is how much weight to add to a vertex each time you brush over it. In our case we're adding 0.1 each time our brush touches it. That's actually a fairly high value to use and would normally only be set like this for every coarse painting like we're doing now.

The "Weight" setting of the brush is the maximum weight that a vertex is allowed to have as a result of adding our strength value to it. I almost never change this from its default 1.0 value except in very special circumstances.

The colours are Blender's easy-to-see visual representation of the weight a vertex has for the selected bone group. Because of that setting we did earlier to show zero weights for "Active," any part of the mesh that doesn't have any weight at all is shown as black (which is why our entire mesh turned black earlier). Then, as weight is added, the colour ranges from a deep blue (very slight) through green (about 0.5) to yellow-red (a lot) and then finally bright red (maximum).

Keep painting until your cylinder looks somewhat like mine. You can switch to the Subtract brush if you need to remove some weight (make sure you change it's strength to something like 0.1 first) and you can switch to the Blur brush and use a weight of about 0.2 to smooth things about a little if you like. Try to leave a little bit at the top that has no weight at all, and try to make the very bottom of the cylinder have a little bit that's bright red. It doesn't have to be perfect at all...just get it to look a bit similar to mine with a range of weights on it (including some parts that are black).

Now let's check the effects of what we've just done.

A handy thing about weight painting mode is the translate hotkeys are active and apply to the currently selected bone on the armature so you don't have to switch back to selecting your armature to do a little experimentation with the effects of bone moment. All you have to do is remember that R is the hotkey for rotation (and G for moving the root bone if you want to).

Watch what happens if you rotate your mPelvis bone. As you might have expected, any part of the mesh that has zero mPelvis weight gets left behind since it's still completely ignoring the armature. The perhaps-surprising thing is that all other vertices are now fully following the mPelvis bone's rotation, no matter how much weight you've given them. They're all behaving as though they had a weight value of 1.0.

The reason for this is because of the way the effects are calculated. As long as a vertex has even the slightest amount of weight assigned to it, it will base its movement on the normalized sum of all bone groups on the vertex. The effective weight of a bone is:
effective bone weight = 1.0 * (bone weight / sum of all bone weights)
For a vertex that's only a very dark blue,  the weight might be something like 0.1. When that calculation is performed it will use an effective bone weight of 1.0*(0.1/0.1) = 1.0. That means the only time the calculation with result in a value other than 1.0 is when the bone's weight is exactly 0.0 because Blender's response to (0.0/0.0) is to ignore it and give a 0.0 result and not move it.

Now Ctrl-select your mTorso bone but be careful not to paint while you have it selected. Our mesh just went black again because now it's showing us the weights assigned to the mTorso bone group and we haven't assigned any. Try rotating the mTorso bone and, as you'd expect, nothing happens. None of our vertices has any mTorso weight so they ignore it (I didn't show a picture of this but go ahead and try it).

Ctrl-select the mPelvis bone again to highlight it, make sure it has some rotation applied to it, then paint on the vertices that got left behind because they don't have any weight yet. The instant you give them any weight at all they quickly snap into position, just as you'd expect them to. It's actually quite rare to do any weight-painting without rotating the bone you're working with because you normally want to watch the effect of your painting in real time.

File these results away in your mind for later reference, then:
  • switch back into Object mode
  • hide layer 1 that has our "pelvis_only" mesh on it
  • make layer 2 visible (in addition to layer 6)
  • select your "torso_only" mesh
  • switch into weight paint mode for it
  • on the toolshelf's options tab enable X Mirror
  • enable Face Selection Masking at the bottom so you see the highlighted topology
  • ctrl-select the mPelvis bone if it isn't already selected and clear any transformations for it (don't do any painting with it)
  • now ctrl-select the mTorso bone
  • rotate the mTorso bone a bit from its "T-pose" position (about 20-30 degrees is good) -- as expected nothing happens because our mesh doens't have any weights at all yet.
  • staring from the top of your mesh, begin adding some mTorso weight -- it will get added to the vertex group list automatically
  • keep painting down the cylinder but for now leave a row or two of completely unweighted vertices at the bottom
What you'll see is what you'll probably be expecting to see if you think about what we just learned with the mPelvis bone. As soon as a vertex has any mTorso weight at all, it snaps over into position relative to the mTorso bone because it suddenly has an effective weight of 1.0 for that bone even if its actual weight is much less than that. Any vertex that doesn't have any weight yet stays stubbornly in place.

Once you start painting below the half-way mark (where the mTorso bone's head is attached to the mPelvis bone's tail) you might be slightly surprised to see the vertices snapping in the opposite direction. When you think about it you'll see that it makes sense, though...the vertices are trying to keep their positions relative to the mTorso bone so that's where they "belong" based on your current rotation.

While you still have some vertices that are unweighted, zero the rotation of the mTorso bone (ALT-R) then ctr-select the mPelvis bone and rotate it. (Your mesh will go black because it doesn't have any mPelvis bone weights.)

The behaviour of the mesh when you rotate the mPelvis bone looks somewhat different but when you remember the rules about armatures it will make sense: mTorso is a child of mPelvis so when you do the rotation the entire mTorso bone goes along for the ride, which means any vertices that are weighted to it will do that too.

Instead of ctrl-selecting the mTorso bone again, select the mTorso group name from the Vertex Groups list in the Data Object tab. Your weighting colours will return and you can continue to paint until all the vertices have at least a little bit of weight.

This illustrates an important point: that the bone selected on the armature isn't always the one you're painting with. The brush effects are always applied for the currently selected vertex group, not the currently selected bone in the armature. Blender simply makes ctrl-selecting a bone also automatically select its vertex group to save you the need to do so yourself.

There will be times where you want to manipulate one bone while painting weights for another and you can easily do this without having to switch back and forth between the mesh and the armature.

We've covered a lot in this tutorial so let's save our file now.

In Part III of the series we'll work with that third "body" copy of our cylinder and learn a lot about the interaction between bones as well as several very handy methods for weighting things.

No comments:

Post a Comment