DEV - Quaternions Angle transfer from anywhere
Added 2021-07-12 08:25:46 +0000 UTCThe previous post worked only when the reference point where on the Y axis.
By the time you moved (both objects that you wanted to get the angle) the angle got "crazy"
I found a workaround (not the best solution) to make it work anywhere (except when y=0).
Blend file attached and below is the code.
#This code was created to copy the angle between 2 points
#And transfer it to a Bone
import bpy,math
eq2 = bpy.data.objects['Empty2'].location
eq1 = bpy.data.objects['Empty3'].location
#Quaternion Rotation Transfer
pt_ini_x =eq1.x
pt_ini_z =eq1.z
eq1.x = eq1.x - pt_ini_x
eq1.z = eq1.z - pt_ini_z
eq2.x = eq2.x - pt_ini_x
eq2.z = eq2.z - pt_ini_z
arm = bpy.context.scene.objects['Armature_Quaternion']
bone = arm.pose.bones['Bone']
if eq1.y >=0:
rot_quaternion = eq1.rotation_difference(eq2-eq1)
else:
rot_quaternion = eq1.rotation_difference(eq1-eq2)
eq1.x = eq1.x + pt_ini_x
eq1.z = eq1.z + pt_ini_z
eq2.x = eq2.x + pt_ini_x
eq2.z = eq2.z + pt_ini_z
bone.rotation_quaternion = rot_quaternion
print("W: %.2f, X: %.2f, Y: %.2f, Z: %.2f" % tuple(math.degrees(a) for a in rot_quaternion))