Tuesday, 1 September 2015

TUTORIAL: Blender 2.74+ Weight Tranfer

In a few of my G+ posts I've made the comment that as of Blender 2.74 there's a new and very powerful weight transfer feature that I am finding does a superb job and is a real time-saver. This was first introduced in 2.70, has subsequently undergone several additions and improvements, and now provides a very powerful, time-saving tool.

This tutorial is intended for Opensim creators who already have a good grasp of rigging and weighting (perhaps under earlier Blender versions) but haven't yet used the new transfer utility. It isn't intended for the original rigging and weighting of your avatar...it's for clothing and other such items where you want the weighting to match your body's weighting to avoid (or completely eliminate) poke-through without having to resort to alpha-masking whatever is underneath.

Before Starting

I've read many people's tutorials where weighting is done each time by proximity mapping from the armature bones. In my opinion this is a colossal waste of time. It's almost always going to require significant post-work and having to do this over and over again for each item you make is counter-productive.

The only time you should need to weight by bone proximity is if you're weighting a completely new and unique mesh avatar from scratch where no existing avatar can act as the source of starting point weights. This tutorial assumes that you already have a base avatar model that will be wearing the clothing item, and that it's rigged and weighted properly.

Instead, it's far more efficient to work from a preexisting model that's already perfectly weighted and use the new weight transfer capabilities of Blender to copy those weights intelligently over to the new object. Since a fully weighted SL avatar rig is readily available from Linden Labs, anyone can do this. It's simply a matter of having a source object with weights you already like, and a target object you want to transfer them to. It works with the standard bones (base SL armature) as well as with the fitted mesh bones.

For women (or men who wear kilts) I highly recommend making a "dress blank" to go with that base avatar as well. The idea, here, is to make full length dress (to the ankles) with long sleeves (to the wrist) and high neck. Keep it quite tight to the body. This can then be used as your weight source for almost all dresses regardless of style or length.

You'll need to weight this one manually to fix the transition area of the pelvis and hip bones, which is a long, annoying, time-consuming process if you want it to look semi-decent. I'd suggest using the method in this tutorial to obtain your starting weights (using your avatar as the weight source).

Make this "blank" much higher poly than you'd normally work with for export in-world (mine is about 40k vert) and when you make it, ensure that it's perfectly symmetrical on the x-axis since it will make it much, much easier to weight the overlap where the left and right thighs overlap one another and to smooth out the blend for the pelvis because you'll be able to use X-Mirror in your weighting options. Having a large number of faces will result in much more consistent (and better) results when using it as your weight source for low poly models.

The most difficult region to weight in your blank will be the transition between knees and hips where you'll need to simultaneously balance the weights of 4 bones. Doing this without being able to use X-Mirror is an exercise in frustration so I wouldn't even bother. For the hips and pelvis, balance your hips first, then work with the pelvis bone weights to smooth out the bend. You'll want to do all of this with auto-normalize enabled on your brushes.

Spend as much time as necessary to make this blank and get its weights as close to perfect as you can. The idea is to spend all this time only once, and then be able to use it as our weight source for all future skirts, dresses, etc with little or no tweaking being necessary afterward.

When the legs are apart you're looking for radiating straight-lines from the pelvis to the ground (or as close to it as you can get -- assuming your dress started off as more or less a plane or cylinder).


The added bonus of a creating a blank like this is it also works very nicely as a convenient shrinkwrap target in addition to being your weight source. Of course if you'll only ever be making shorts or pants, you don't need one of these since you'll be using your avatar mesh as your source.

Save a copy of this blank somewhere that's easy to find later. When making something new I usually just open my blank and immediate "Save As" with whatever my new clothing item's name will be; then work directly in that file. When I've finished everything, I delete my weight blank mesh and avatar to conserve space when archiving.

At this point you'll have two archived weight source objects -- your avatar model and your dress blank -- both in the same file and parented to the same armature -- that you've invested a lot of time and effort into weighting as well as you possibly can. After that, it's easy!

Model A New Clothing Item

Now go ahead and create a new model of whatever clothing or jewellery item you feel inspired to make. You don't have to make it conform perfectly to either of your archived weight sources but the closer the new item is to one of the existing sources, the less post-work you'll need to do. both work nicely as shrinkwrap targets, too.

For this tutorial I'll be using a dress I made that's intended to look similar to the famous Marilyn Monroe white dress. The under-boob tucks of the ribbon,  the extensive pleating, and flared skirt of the style make this something that would normally be a major PITA to weight -- particularly using bone proximity mapping. Here's the model (it will be white...the colours are simply material zone reminders to myself).

The picture on the left also has my dress blank visible, which you'll see pokes through in a few places where I'm conforming more tightly to the body than my blank does, but for the most part lies underneath the model I've made. The picture on the right is showing the final fit on my avatar.

Parent To Your Rig

Now parent the new item to your existing rig.

If you use Avastar (which I highly recommend and still use as part of my workflow) when you bind to armature select "keep" for weights since we don't need Avastar to try to generate weights for us. In pre-2.74 days Avastar's weighting routines were superior to Blender's. Now (in my opinion) Blender's are better and offer far greater flexibility).

At this point we now have a dress that's parented to the rig but unweighted.

Transfer Weights

Now for the magic of Blender 2.74+ weight transfer...
  • In Object mode first select your dress blank (the source that is already weighted) and then shift-select your new unweighted dress to add it to the selection, making it the primary selected object (the dress with be highlighted in orange and your weight source will be more of a red highlighting).
  • Switch to weight paint mode.
  • In the toolshelf's Tools Tab, in the "Weight Tools" section click the "Transfer Weights" button (or menu Weights > Transfer Weights). Immediately after doing this look in the most recent action section of the toolshelf and set the appropriate transfer parameters which will almost always be:
    (in this picture I've hidden the body and armature layers but you don't need to)
    • Data Type: Vertex Groups
    • Create Data checkbox checked
    • Vertex Mappiing: Nearest Face Interpolated (see notes below)
    • Object Transform checkbox checked (to evaluate in global space)
    • Only Neighbor Geometry checkbox NOT checked (Ray Radius then doesn't apply to the transfer and its value is ignored)
    • Source Layer: All Layers (see notes below)
    • Destination Layer: By Name
    • Mix Mode: Replace (see notes below)
    • Mix Factor: 1.00 (see notes below)
For many objects you won't need to do anything further at all beyond a few very minor tweaks.

The above has three rather critical choices involved that are relevant to weighting objects; but it's worth being aware that this same utility can transfer all sorts of other data if you change the data type from Vertex Groups to something else. The The Blender wiki for more information on other data transfer types.

The first critical choice for weighting is the Vertex Mapping where your choice will depend on your source and intended target. For most things you'll want to use "Nearest Face Interpolated" but there occasions where it makes sense to select one of the others (if you hover your mouse over the option in the list Blender will give you a pop-up telling you how the method calculates the weight). On occasion I will use Projected Face Interpolation or, rarely, Nearest Vertex.

The second key choice is whether to transfer all layers (all bones) or only one specific bone. Since our mesh is unweighted we want to transfer them all and our "Destination Layer: By Name" assigns those same vertex group (bone weight) names to our target mesh which is exactly what we want. You might, on occasion, only want to transfer the weight from a single bone (or do a few of them, one by one, while ignoring others).

The third important choice is the Mix Mode and Mix Factor. For our purposes you'll always be using "Replace" with the Factor at maximum because there are no preexisting weights on our model.. If you were importing one bone at a time you would typically mix or add, then later normalize all weights

Clean Up

After transfer, I strongly suggest.using one or two additional tools from the Weight Tools tab:
  • a "Normalize All" with the "Lock Active" NOT checked but only do this if you are NOT using fitted mesh weighting
  • a "Clean" for "All Groups" with limit 0.0 and "Keep Single" checked
The "Normalize All" ensures that the sum of all weights on each vertex is 1.0 which is a requirement for the standard armature. Fitted mesh does not require this (a number of vertices on the SL base model have weights with sums >1.0).

"Clean" with "all groups" just cleans up the vertices to only have vertex groups assigned to them where the weight is non-zero. If you need to do any tweaking later, this will tend to simplify things and avoid potential accidents (used in conjunction with "restrict" painting option enabled)

Test and Tweak

In many cases you can simply export this result because the above method is usually remarkably accurate. Depending on your model you may need to do some minor painting tweaks or blurs, so it's good practice to test your weights by moving your armature through a normal range of motions to ensure that there's nothing odd happening and clean up any minor poke-through areas if possible.

With the default SL avatar and weighting it might not be possible to eliminate all pokethrough on very tightly form-fitted or very low poly clothes. You can then either make a matching alpha layer or adjust your model as needed. The closer your model's vertices are to the location of SL avatar vertices, the less often you'll need to do this (or simply raise the clothing further off the body, or if absolutely necessary increase the poly count in that region).

For most common clothing and jewellery items I can get a pretty respectable set of copied weights that require very little tweaking or post-work adjustment, making all that time spent on the original dress blank very much worth it. If your new item has areas that are quite distant from the source mesh you will typically need to make a few tweaks to smooth out the interpolations.

That was the case with the waist-to-hip region of my "Monroe Dress" which sits quite far out from the body and moves more naturally when the weighting transitions are blended a little more smoothly. I also did a little further tweaking to the pleating in the lower portion of the skirt to have them expand more smoothly with movements of the legs. I also removed all influence from the two knee bones since they had picked up slight weighting at the very bottom (which would be needed if the dress extended much lower) and re-normalized those vertices.

In total my dress' weighting (including post-work tweaks) took less than half an hour. Had I weighted by bone proximity it would have been many hours of work to get even remotely close to an equivalent result.

I use my dress blank as the wight source for anything that needs to have shared influence of the two hip bones (skirts, dresses, very long shirts, etc) and for anything else I'll use my own body as the weight source.

Conclusion

If you haven't yet experimented with the latest weight transfer improvements in Blender (or haven't updated to a recent build) I suggest you give it a whirl. I think you'll find the drastic improvements worth the relatively small learning curve to get up and running with it.

Edit: there appear to have been a few changes with the new Blender 2.76 and the transfer isn't behaving as expected. I'm trying to figure out what's changed and how to get back to the reliable results it was previously given. Once I do so I'll post them here.

Edit2: I have confirmed that were was a bug in versions 2.76-2.76b that broke this function. This has been corrected as of the nightly build Nov. 25, 2015 and is now working properly again on this (and presumably future) nightly builds. It will be included in whatever the next official release is (2.76c or 2.77). Until then you can either get a copy of the nightly build or revert to 2.75 where it also works perfectly.