From bf936d59d0f453a3f05403b74bbd08969f74cd24 Mon Sep 17 00:00:00 2001
From: Alexander Gavrilov <angavrilov@gmail.com>
Date: Wed, 20 Sep 2023 21:29:57 +0300
Subject: [PATCH] Rigify: fix unreliable interpolation of limb tweak roll.

The old math wasn't handling inverted quaternions. In comparison,
Matrix.lerp is actually using internally exactly the same math code
that is used by constraint influence.
---
 rigify/rigs/limbs/limb_rigs.py | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/rigify/rigs/limbs/limb_rigs.py b/rigify/rigs/limbs/limb_rigs.py
index c0748055e..1cf1d8462 100644
--- a/rigify/rigs/limbs/limb_rigs.py
+++ b/rigify/rigs/limbs/limb_rigs.py
@@ -817,14 +817,12 @@ class BaseLimbRig(BaseRig):
             prev_tweak, next_tweak, fac = self.get_tweak_blend(i, entry)
 
             # Apply the final roll resulting from mixing tweaks to rest pose
-            prev_rot = self.get_bone(prev_tweak).matrix.to_quaternion()
-            next_rot = self.get_bone(next_tweak).matrix.to_quaternion()
-            rot = (prev_rot * (1-fac) + next_rot * fac).normalized()
+            prev_mat = self.get_bone(prev_tweak).matrix
+            next_mat = self.get_bone(next_tweak).matrix
+            rot_mat = prev_mat.lerp(next_mat, fac)
 
             bone = self.get_bone(tweak)
-            bone_rot = bone.matrix.to_quaternion()
-
-            bone.roll += (bone_rot.inverted() @ rot).to_swing_twist('Y')[1]
+            bone.roll += (bone.matrix.inverted() @ rot_mat).to_quaternion().to_swing_twist('Y')[1]
 
     @stage.rig_bones
     def rig_tweak_mch_chain(self):
-- 
GitLab