Skip to main content
3 of 4
added 378 characters in body
batFINGER
  • 85.2k
  • 10
  • 114
  • 244

Apply the rotation

Apply the rotational difference between to vectors to the pose bone, rather than setting the pose bones rotation with it.

The rot diff has an axis and angle. Applying the rotation rotates the pose bone, which in this case is angle 54.7 degrees about axis (-1, 1, 0).

import bpy
from mathutils import Vector

up = Vector((0, 0, 1))
dest = Vector((1, 1, 1))

R = up.rotation_difference(dest).to_matrix().to_4x4()

pb = bpy.context.active_pose_bone
pb.matrix = R * pb.matrix
pb.matrix = R @ pb.matrix #  2.8

To set the bone absolutely from the vector, can use Vector.track_to_quat(to, up) For bones track to the 'Y' axis, along the bone. I have arbitrarily chosen 'Z" as up.

>>> v = Vector((1, 1, 1))
>>> q = v.to_track_quat('Y', 'Z')
>>> C.active_pose_bone.rotation_quaternion = q
>>> x = C.active_pose_bone.tail.x
>>> x * x
0.33333332136784577
batFINGER
  • 85.2k
  • 10
  • 114
  • 244