Thursday, 12 November 2015

TUTORIAL: Principles of Blender Rigging and Weighting - Part V

This is the fifth part of a series of tutorials on the subject of rigging and weighting in Blender for subsequent use in Opensim. Parts I - IV are listed in my blog's page of links in the "moderate" level tutorial section.

I've changed my mind about the subject for today's installment. Originally I'd intended to create a quick, simple tube top, then rig and weight it to the traditional armature which would give us a good look at some of the most basic, common steps you'll be using when making clothing. There's a somewhat large problem with doing this: it won't be of much practical use to many readers since, for most, the collision armature will be preferable.

Today's subject, then, is to explain the difference between the two armatures and to give you some guidelines about why you might choose one over the other. Then in Part VI we'll cover  the methods I'd been intending to discuss in this one. Unfortunately that means this installment, like Part IV, will be a somewhat theory-intensive one with very little "hands-on" instruction. Sorry.

The single biggest issue when creating a mesh and weighting it is "fitting" it and having it move nicely with the body (the same is true of a lot of real life clothing too, really). When you make something and export it, you want to have it fit and move properly with your avatar. You don't want body parts poking through it all the time.

The only way to have any chance of making properly-fitting clothing is to have a Blender model of the avatar that exactly matches the shape of your in-world avatar. That way you can model the clothing to fit it in Blender and weight it to match the body's movement when the armature moves. If we all wore avatars that were identical in appearance -- all used identical shapes -- this wouldn't be an issue. We don't, so it is.

Further complicating matters in-world, rigged mesh differs from un-rigged mesh in a very important respect: when you wear un-rigged mesh it behaves exactly like wearing a prim, allowing you to position it on the attachment point, scale it, and rotate it. Rigged mesh cannot be moved or scaled or matter which attachment point you attach it to, it will automatically snap to its "correct" rigged position on the body and can't be changed at can't move it, can't scale it...nothing.

The only thing that affects the shape of a rigged mesh in-world is the armature weights is has. If a bone is changed in some way, and the mesh is weighted to it, the mesh deforms based on that weighting. We're used to seeing this effect in animations, but it also applies to changing the bones via your avatar's appearance. You've already seen this effect in Parts II and III of this series if you tried changing a bone's scale after the mesh was weighted to it.

As you're undoubtedly aware, your avatar appearance in-world is governed by a series of approximately 70 shape slider settings (the exact number is slightly different depending on whether you use a male or female shape). What you're less likely to be aware of is how those actually work. I don't want to get overly technical or detailed about it, but in a nutshell this is achieved by tying those sliders to two internal mechanisms:
  • changes to the traditional armature's structure, and
  • changes to the avatar mesh's structure
Some sliders -- height being a good example -- cause the length of bones in the traditional armature to change. A few sliders adjust bone position or rotation a little. Only 17 of the appearance sliders do this and some of those affect the mHead or one the two eye bones so for most clothing items it's even fewer. A mesh that is rigged and weighted to any of these bones will deform in direct correlation to the bone's new characteristics.

The other ~50 appearance sliders don't affect the traditional armature at all. Instead, they use something called "shape keys" to directly alter the structure of the mesh by moving groups of vertices in predetermined ways. In the world of 3D modeling, this is commonly called "morphing."

The way this works under the hood is very much the way an armature works. In addition to the armature bone weights, your avatar has additional vertex groups and weights assigned to parts of it with specific movements that are tied to the shape sliders. Change a slider and the vertex group assigned to that slider moves in whatever way it has been set to move (the Blender website has some good tutorials on shape keys and morphing if you want an in-depth explanation of how this works).

Unfortunately, it's not possible (ie not supported by viewers or the server back end) to upload meshes that have these additional shape keys which means it's impossible to have a mesh respond to any of them. Any changes to one of these sliders is completely ignored by rigged mesh.

What this means for mesh clothing rugged to the traditional armature is that it will stretch along with the armature's bone changes but nothing else. If you change your boob or butt size or your body fat or anything else like that, the clothing isn't aware of it and doesn't get adjusted at all.

That's a serious problem when trying to make clothes that fit well and look nice, one that is further exacerbated when the bones move when you're playing animations. Historically this meant that all clothing had to be made to custom-fit the exact avatar that would wear it. In a market-driven world like SL, that's a huge problem since a vendor can't make a custom mesh to sell to each individual possible user shape who might want to wear it.

The market's solution to this was threefold:
  • The easy solution was to supply alpha masks to wear to make the parts of your avatar that were under the clothing be invisible...that way when your body pokes through the mesh you can't see it. Unfortunately that also means that your mesh's textures have to be completely opaque (no point in having a lovely sheer lace camisole if the body under it is invisible).
  • The next solution was a collective effort by the majority of clothing manufacturers to come up with a set of "standard" avatar sizes. They polled the public to get a sense of common shapes being used, then set a series of "standard sizing shapes" ranging from extra-small to extra-extra-large to fit them.

    The creators would then make a mesh version of each item to fit each of those predetermined shapes and you'd buy the one that was the best fit for you. It's still lots of work for the creator but at least allowed them to make better-fitting clothes. Most real-life clothing is sold based on this exact same approach where you buy "S" or "M" or "L" or "XL" etc.

    Some RL things (like dresses, bras, pants, etc) have more precise variations but that means having to make a lot of versions of each things...something that rapidly becomes too time-consuming to be profitable for an SL merchant. Instead, they made a limited number of size/shape variations and continued to supply alpha masks as needed and/or expected the purchaser to make some minor adjustments to their own shape to make it fit their sizing standard.
  • The third solution was pioneered based on some interesting experimentation where it was discovered that the SL avatar also has a separate collision armature and that it would be relatively easy for TPV to be modified to allow uploading weights for these bones, too. With that possible, it meant that mesh could be weighted to that armature and respond to it.

    The reason this generated a lot of excitement was that, unlike the traditional armature's bones, the collision bones were being affected to a fair degree by far more of the shape sliders and, in particular, the "important" ones like the fat, boob, "package" and butt ones that were prominently adjusted by users. The correlation in shape change wasn't identical, but it was pretty close -- close enough that you could make a piece of clothing to fit Ruth, rig and weight it to the collision armature, and then when worn in-world would change its shape to very closely match almost all of the common shape slider settings.

    At some point in 2013, the people working on this approached Linden Labs with a proposal to make this an officially supported capability, and contributed the majority of the code necessary to make it happen. Linden Lab did the rest, and introduced "fitted mesh" as an official mechanism in 2014.
Sorry about the lengthy description/history lesson but now you will have some idea of the issues involved, which will be quite important as you make decisions about how to create and weight mesh clothing. There is no single "right" way to do it. It depends a lot on how you intend to use it.

In Part IV we downloaded the Machinimatrix "workbench" avatar model that is rigged and weighted to the traditional armature. Today, let's get the collision armature "fitted mesh" version.

Go to the SL Wiki Page for fitted mesh and near the top you'll see a text box with a link to their most recent fitted mesh skeleton download. Download this zipped folder and upzip it. You'll see a number of files there, in assorted formats. The one we want is the .blend file for the female mesh (or the male mesh if you prefer).

When you open it in Blender, you'll see that Ruth is a single mesh object (without eyes or eyelashes or other things this time). You'll also see a considerably different and more complex armature that has both the mBones of the traditional armature and a large number of additional bones whose names are entire in upper case. Those are the "fitted mesh" bones and they're all linked to the traditional bones -- the need to be, in order to respond to animations.

You'll find that everything is oriented to the SL standard of facing in the positive x-axis direction which is a nuisance when modeling, so select both the armature and mesh and rotate them by 90 degrees on the z-axis to make Ruth symmetrical on the x-axis and facing in the negative-y-axis direction. Then apply that transformation (Object > Apply > Rotation). You'll probably want to delete the camera and lamp on layer 2 as well.

Save this file as "fitted.blend" (or whatever name you like) and remember that prior to exporting you will need to rotate everything back and apply it first. Work with a copy of this file if you're going to play around with it and experiment.

Select Ruth and have a look at the vertex groups list in the Object Data tab. It's a very, very long list of bones from both armatures. Any bone that has a name like "mBoneName" is one that you're already familiar with from the traditional armature. Any that are in upper case like "BONE_NAME" is one of the collision bones.

Switch Ruth into weight paint mode and start selecting the various bones to see how they're weighted. You'll see that none of the traditional bones has any weights set in the mesh. Instead, all weights are for the collision bones. You'll also discover that
  • you can grab and move any collision bone whereas only the mPelvis bone of the traditional armature can translate that way
  • any rotation of a traditional armature bone will behave exactly as you'd expect it to from Part IV, but it also causes the corresponding collision bone to translate along with it
  • you can rotate a collision bone but it does its translation in an utterly "detached" and independent way. If you rotate (or scale or move) the L_UPPER_LEG bone, nothing at all happens to the lower leg or foot...all it does is move the nearby weighted portions of Ruth's mesh
  • the same is true of scaling
  • there are more collision bones than there are corresponding traditional bones so more of them share the same linkage point on the traditional armature
What you're playing with is the exact collision armature that's hard-coded into your avatar in the viewer. Animations are all handled by the 21 traditional bones but they have to be part of the collision armature as well to ensure that the collision bones respond to those animations, too, even though a fitted mesh has no weights for those bones.

If you'd like to know exactly how the fitted mesh bones correspond to your shape sliders the SL Wiki has a complete list of each slider and which bones it affects. It's very important to remember that traditional bones are affected on an exact 1:1 basis with slider changes whereas collision bones have only a very close -- but not perfect -- correlation.

If you purchase the Machinimatrix Avastar add-on their product includes both armatures and Ruth weight sets. Their avatar model also has the complete set of SL appearance sliders that allow you to easily achieve an in-Blender Ruth model that exactly matches your in-world one (your viewer can export an XML of your shape settings that you can quickly import and apply automatically to Avastar's model). It is also pre-configured to x-axis symmetry and automatically handles the necessary rotations on export (if you use their exporter).

Play around with this armature for a while until you begin to get a feel for how the bone movement affects other bones in the structure. It's more complex than the simpler, traditional armature but there is a logic to it once you've grasped it.

Now I can give you some general guidelines to consider when you're preparing to create a new mesh clothing item:
  1. If you have purchased Avastar, use it. It's great for either method of weighting since it handles both equally as well and now even has an ability to convert a mesh quite well from one to the other. It also has some very handy time-saving utilities. (Honestly, they're not paying or asking me to promote their's really just that useful!)  The decision of which armature to use should be based on the points below (they apply for everyone).
  2. If you use your own custom-made (and potentially custom rig-adjusted) mesh avatar you'll already have an exact copy of it available to you in Blender. Your modeling, rigging and weighting should use whichever armature it uses, making the weights match your avatar's corresponding weights as closely as possible. With sufficient care you can do this with no need for the avatar to wear alpha masks.
  3. If you have (via whatever means) an exact copy of your avatar and its traditional armature in Blender, you can model the mesh to fit it precisely and weight it to the traditional armature. This will give you the most accurate preview possible for all motion and weight effects. Unfortunately it also means that the clothing item will only fit and move correctly for you (or someone with a near-identical in-world shape). The weights of the clothing will ideally match as closely as possible to the weighting of the underlying avatar's weights. This method also makes it possible to avoid needing to use alpha masks.
  4. If you don't have an exact Blender copy of your avatar, or if you want to make a clothing item that will fit almost anyone, you'll need to do your modeling to fit the collision armature version of either the female or male model, then rig and weight it using only the collision bones, making those weights correspond as accurately as possible to the body mesh's weights.

    When working with fitted mesh, it's critical to work with Ruth or Ron, not your own shape! When you import and wear that, your own shape sliders will deform the mesh to match your shape...but do not expect it to be a perfect match because the collision bones don't correspond perfectly to the slider adjustments. Always model leaving a little extra room and/or be prepared to make alpha masks for some portions of the avatar.
  5.  In my opinion, it's considerably harder to accurately weight and preview weights for the collision armature. Even then, no matter how precisely you do it in Blender it will NOT behave exactly the same way in world because of the lack of exact correlation between shape slider and collision bone. It's very good, but not perfect.

    If you need perfect, you'll need to custom-make it for the avatar that's going to wear it and you'll need to rig and weight it to the traditional armature unless you have a custom mesh avatar that's rigged and weighted to the collision armature.
For most people's purposes, the above means that you'll probably want to work with fitted mesh (unless you have an OCD-like desire for your clothing to be as close to perfection as possible*). You'll need to make your clothing items by fitting them to the Ruth (or Ron) of the files we downloaded in this installment of the tutorial. Then you'll need to rig and weight the item based on the collision armature.

That's what we'll do in Part VI of this series.

* I do...