Saturday, 9 April 2022

Blend Shapes Between Non-Matching Geometry: It's Possible!

We aaaaaaaaaall know -don't we? - that in order to apply an object to another object as a blend shape, those two objects have to match.

We know that, right? Of course. Everybody knows that about blend shapes!

Well it's a shame we all know that, because it isn't true.

Watch this:

You might be looking at that and thinking "But that's impossible! The source mesh is just a head, while the target mesh is the whole body!" And who could blame you? Normally when you try to make a blend shape between two objects whose geometries aren't identical, Maya throws it back in your face:

But it is possible to get predictable, usable behaviour out of a blend shape between two non-matching geos. But to control it, you need a little appreciation of what it is blend shapes actually do under the hood.
And to understand that, you first must understand about vertex IDs and vertex order. If you don't, then step right this way: 

https://rigjuice.blogspot.com/2022/04/vertex-ids-vertex-order.html

What blend shapes are actually doing

It's really very simple! Aaaaaaaaall the blend shape deformer does is it looks at the vertices of the source object (or of a stored shape) and for each of those vertices it takes the vector being applied to that vert (relative to said vert's resting position) and applies that same vector to the corresponding vertex in the target object.

Key word there: "corresponding". How does Maya decide which source object vertex corresponds to which target object vertex?
With vertex IDs, baby!

If vertex[930] is moved in the source mesh, that same movement vector is applied to vertex[930] in the target mesh.
And that will happen wherever on the mesh vertex[930] happens to be.
 
Here we have two meshes:
 

They have the same topology and the same number of vertices.
But I've scrambled the vertex order for just one of them. So they have all the same Vertex IDs, just not laid out across the topology in the same way.

What should happen if I apply one to the other as a blend shape? Well Maya has no objections. Why would it? Remember: blend shapes go by vertex ID, and these meshes have the same number of vertices - therefore all the same IDs. So everything is as it should be as far as Maya is concerned.
 
The results however are... not practical:
 
 
But understand: this is not a bug or an error. This is blend shapes behaving as a blend shapes should. The movements of the verts in one mesh are being applied to the verts of the other based on vertex IDs. It's not Maya's fault that the vert IDs weren't in the same positions across both meshes. That's up to the user to manage.

And since the only way one vertex corresponds to another is because they have matching IDs, blend shapes don't actually care about topology!

Here's meshes with completely different topology! But they both have 58 vertices:

 
Again the blend shape results aren't pretty, but since they have the same number of IDs Maya has no problem applying the blend shape:
 

Once you understand that wrangling blend shape behaviour is a matter of being conscious about your vert IDs, there are some cool ways you can take advantage of the blend shape deformer's ID-centrism.

A magic trick

So for two shapes to be blend shape-compatible...
...Topology - doesn't matter.
The blend shape will take as long as the source mesh and the target mesh have the same number of vertices.

Welllllllll... actually...
What if I told you that even the number of vertices doesn't matter?

Oh you heard right, my friend.
Stop me if this sounds familiar:
You have a two meshes that you think are the same, but then you got to apply one to the other as a blend shape target, but it turns out their vertex counts are slightly off so you get an error like this:
 

The only reason Maya cares about this discrepancy is because of this here setting in the blend shape options:
 

It's something of safety net, for people who don't know exactly what they're doing. But turn it off and a whole lot of new possibilities open up.
Now you can apply any mesh to any other mesh as a blend shape even if they have different numbers of vertices and different topology.
 
Let's go back to opening example:
 
 
I'll call these fullBody_mesh and head_mesh
 
This is a common scenario in character rigging. To make the face rig more manageable, duplicates of the character mesh are creates and applied to the original as blend shape targets, then those duplicates are rigged. One for the mouth rig, one for the eyes, the brows, the cheeks, etc.
But it's a bit of a drag to have to duplicate the entire mesh when we just care about head. So what if we duplicate the mesh and then delete everything bellow the neck?

If the Check Topology option were ticked, we could no longer create a blend shape connection between them since their topologies don't match.
But if turn that off, we can do what we like:
 

The results aren't pretty... yet:


With Check Topology turned off, Maya is a little more lax. If it find a match for a vertex ID in the source mesh, it applies the movement to the matching ID in the target mesh. And if not, than it leaves that vertex alone. No drama.
That's why in the above image, only the arms seem to be effected by the madness. I guess the vertices that make up the arms must be at the front of the vertex order. But the head_mesh doesn't have any arms. It's just a head, so of course its vert IDs start and end in the head.
Those same IDs in fullBody_mesh (which happen to be in the arms) are attempting to match with their counter parts in the head_mesh, and the results aren't at all useful to us.

So to make this work, we have to go through some steps to ensure that even in the fullBody_mesh, the vertices composing the head part of the mesh have the same IDs as they do in the head_mesh.

But how to make that happen...

Follow me for a bit.
Get rid of head_mesh. We're gonna make it again, but we'll do it cleanly.

Recall that whenever you mess with the number of vertices in a mesh, you force Maya to automatically redistribute the vertex IDs such that the IDs start at zero and continue uninterrupted.

Splitting a mesh into two separate meshes would certainly do that. So let's chop the head off of fullBody_mesh using the extract function (and don't forget to clean up the construction history after such an operation).
Now we have two meshes. A head and a body. The head has vert IDs[0] to [3968], and the body has vert IDs [0] to [10508]:


While we have a mesh that's just the head, let's duplicate it to get our dedicated head mesh back, and since we got it by duplicating the first head mesh, we know their vertex orders match.
 

Now again I say unto thee: Whenever we edit the number of vertices in a mesh, Maya automatically reorders them. Splitting a mesh into two forced a reorder on both resulting meshes, and now we're going to do that same thing in reverse. We'll take the first head mesh and the body mesh and glue them back together into one mesh.
They both have vert IDs [0] to [3968] (and the body mesh has even more on top of that), but Maya doesn't allow duplicate IDs within a single object, so when we merge these meshes back into one, it will have to trigger another reorder. But we need to it in such a way that the head part of the mesh's vert IDs are undisturbed so they'll still match the duplicate head mesh.

Here is how:
When you merge two meshes, the order of the first mesh in your selection will be maintained. Those verts will remain as [0] to [whatever] and all subsequent meshes in your selection will have their vert IDs reassigned to come after the vert IDs in the first mesh.

So I'll select the head mesh first, then shift select the body mesh, and merge.
The head part of the resulting unified mesh will remain as vert IDs [0] to [3968], in the same positions that they were just previously, while the verts making up the body part of the unified mesh will be verts [3969] to [14429].
 

Do you see what just flipping happened?!
Now we have a head_mesh, and a fullBody_mesh whose head geometry has identical vertex order!
Sure, bellow the neck is a different story, but that's fine. We don't care about the IDs bellow the neck! Remember - with Check Topology switched off any verts in the target mesh that have no equivalents in the source mesh will simply be left alone.

So now when we apply head_mesh to fullBody_mesh as a blend shape target:


And that is how dance!

Take note: Combining the head and body back into one mesh will have left a seem. Feel free to close that seem by merging those vertices. Doing so will lower the number of total vertices in the mesh, which will trigger yet another reorder, but Maya is smart enough not to reorder in such a way that the verts at the front of the vertex order won't be changed. So plugging and seems won't mess this procedure up (nor will re-smoothing the face normals to deal with the hard edge where the seem was)


So to review, be conscious of your vertex IDs when using blend shapes and it becomes possible to get usable blend shape results between meshes whose topologies don't completely match.
And keep in mind how you can heard the IDs by cutting up and gluing a mesh back together.

No comments:

Post a Comment