This post is a part of a three post series, where I implement popular rigging functionalities with just using maya’s native matrix nodes.

- Part 1: Node based matrix constraint
**Part 2: Matrix twist calculator**- Part 3: Matrix rivet

Calculating twist is a popular rigging necessity, as often we would rather smoothly interpolate it along a joint chain, instead of just applying it at the end of it. The classical example is limbs, where we need some twist in the forearm/shin area to support the rotation of the wrist or foot. Some popular implementations utilize ik handles or aim constraints, but I find them as a bit of an **overkill** for the task. Therefore, today we will have a look at creating a matrix twist calculator, that is both clean and quick to evaluate.

Other than matrix nodes I will be using a couple of quaternion ones, but I promise it will be quite simple, as even I myself am not really used to working with them.

**tl;dr:** We will get the matrix offset between two objects – **relative matrix**, then extract the quaternion of that matrix and get only the X and W components, which when converted to an euler angle, will result in the twist between the two matrices along the desired axis.

### Desired behaviour

*Please excuse the skinning, I have just done a geodesic voxel bind*

As you can see what we are doing is calculating the twist amount (*often called roll as well from the yaw, pitch and roll notation*) between two objects. That is, the rotation difference on the axis aiming down the joint chain.

### Limitations

An undesirable effect you can notice is the flip when the angle reaches 180 degrees. Now, as far as I am aware, there is no reasonable solution to this problem, that does not involve some sort of caching of the previous rotation. I believe, that is what the *No flip* `interpType`

on constraints does. There was one, using an orient constraint between a no roll joint and the rolling joint and then multiplying the resulting angle by 2, which worked in simple cases, but I found it a bit unintuitive and not always predictable. Additionally, most animators are familiar with the issue, and are reasonable about it. In the rare cases, where this issue will be a pain in your production you can always add control over the twisting matrices, so the animators can tweak them.

Something else to keep in mind is to always use the first axis of the rotate order to calculate the twist in, since the other ones might flip at 90 degrees instead of 180. That is why, I will be looking at calculating the X twist, as the default rotate order is *XYZ*.

With that out of the way, let us have a look at the setup.

### Matrix twist calculator

I will be looking at the simple case of extracting the twist between two cubes oriented in the same way. Now, you might think that is too simple of an example, but in fact this is exactly what I do in my rigs. I create two locators, which are oriented with the X axis being aligned with the axis I am interested in. Then I parent them to the two objects I want to find the twist between, respectively. This, means that finding the twist on that axis of the locators, will give me the twist between the two objects.

*Granted, I do not use actual locators or cubes, but just create matrices to represent them, so I keep my outliner cleaner. But, that is not important at the moment.*

#### The relative matrix

Now, since we are going to be comparing two matrices to get the twist angle between them, we need to start by getting one of them in the *relative* space of the other one. If you have had a look at my Node based matrix constraint post or you were already familiar with matrices, you would know that we can do that with a simple multiplication of the *child matrix* by the inverse of the *parent matrix*. That will give us the matrix of the child object relative to that of the parent one.

The reason, we need that is because that relative matrix is now holding all the differences in the transformations between the two objects, and we are interested in exactly that, the difference on the aim axis.

Here is how that would look in the graph.

#### The quaternion

So, if we have the relative matrix, we can proceed to extracting the rotation out of it. The thing with rotations in 3D space is that they seem a bit messy, mainly because we usually think of them in terms of Euler angles, as that is what maya gives us in the `.rotation`

attributes of transforms. There is a thing called a quaternion, though, which also represents a rotation in 3D space, and dare I say it, is much nicer to work with. Nicer, mainly because we do not care about rotate order, when working with quaternions, since they represent just a single rotation. What this gives us is a reliable representation of an angle along just one axis.

In practical terms, this means, that taking the *X* and *W* components of the quaternion, and zeroing out the *Y* and *Z* ones, will give us the desired rotation only in the *X* axis.

In maya terms, we will make use of the `decomposeMatrix`

to get the quaternion out of a matrix and then use the `quatToEuler`

node to convert that quaternion to an euler rotation, which will hold the twist between the matrices.

Here is the full graph, where the `.outputRotateX`

of the `quatToEuler`

node is the actual twist value.

### Conclusion

And that is it! As you can see, it is a stupidly simple procedure, but has proved to be giving stable results, which in fact are 100% the same as using an ik handle or an aim constraint, but with little to no overhead, since matrix and quaternion nodes are very computationally efficient.

Stay tuned for part 3 from this matrix series, where I will look at creating a rivet by using just matrix nodes.

Hi, thanks for this series, great creative use of the nodes.This one is super neat, can’t wait to try it.

Brilliantly light weight! Why didn’t I do this before! Thanks a lot for your time!

It was very helpful.

By the way there are questions.

Like the author, I was able to extract only the twist component using the matrix and the nodes. But, still flip it. How do you solve this flip?

If you tell me, I’m confident that you will be able to further understand the depth of maya in the future.

Thank you.

Do you mean the flip when you get to 180 degrees? As I mentioned in the post, we will need to have some sort of caching in order to know which way we are going, in order to solve the flip. Constraints do that with the “No flip” option, but I have no idea how would we go about it for a setup like this without writing custom nodes. That being said, a custom node should be able to do it in a fairly straightforward manner.

Hi Vasil, thanks for posting this.

I’m trying out this method as a means to extract twist from the hip and I’m running into some issues. This works great on wrists, ankles, and even shoulders, but its giving me issues on the hips. The problem seems to be the joint’s orient values. If I set those to zero then it works as expected but if I orient the joint as it needs to be (something like (0,0,-90) for the hip) then the quat twist doesn’t work as expected. It extracts the twist from Rx fine but it also associates rotation on the Z axis as twist also.

Know of a way around this?

Thanks,

Ben

Hey Ben, sorry for the late reply!

If you are using joints to calculate the twist from, then, yes, it will be polluted from the

`jointOrient`

. My advice would always be in this case, to create two locators under the joints and orient them so their X axis match the axis you want to extract twist from.