萬幸,官方的約束操作是 python 代碼,不是 c++。並且能直接在 blender 安裝目錄裡面找到。
路徑,你可以參照以下路徑格式找到你自己的blender版本的約束代碼的位置
blender3.2的安裝目錄\3.2\scripts\startup\bl_operators\constraint.py
在第 71 行 CONSTRAINT_OT_disable_keep_transform
代碼內容,非常簡單。把姿態矩陣轉換到世界矩陣,然後禁用約束,然後把世界矩陣再轉換回姿態矩陣,再直接覆寫回原來的姿態矩陣,對象的平移,旋轉,縮放會自動計算出來。
下面官方的原始代碼
# This works most of the time, but when there are multiple constraints active
# there could still be one that overrides the visual transform.
#
# Note that executing this operator and then increasing the constraint
# influence may move the object; this happens when the constraint is
# additive rather than replacing the transform entirely.
# Get the matrix in world space.
is_bone_constraint = context.space_data.context == 'BONE_CONSTRAINT'
ob = context.object
if is_bone_constraint:
bone = context.pose_bone
mat = ob.matrix_world @ bone.matrix
else:
mat = ob.matrix_world
context.constraint.influence = 0.0
# Set the matrix.
if is_bone_constraint:
bone.matrix = ob.matrix_world.inverted() @ mat
else:
ob.matrix_world = mat