import bpy
from mathutils import *
from math import *
def compute_rotation(src_vec, dest_vec):
""" return an orientation quaternion q such that
dest_vec = c * q * src_vec
(where c = dest_vec.magnitude / src_vec.magnitude and is not interesting in terms of orientation)"""
a = src_vec.normalized()
b = dest_vec.normalized()
c = a.cross(b)
theta = asin(c.magnitude)
if (c.magnitude>0):
axis = c.normalized()
st2 = sin(theta/2)
q = Quaternion( [cos(theta/2), st2*axis.x, st2*axis.y, st2*axis.z] )
else:
q = Quaternion( [1,0,0,0] )
return q
obj = bpy.context.active_object
obj.rotation_mode = 'QUATERNION'
obj.rotation_quaternion = compute_rotation(Vector([1,1,1]), Vector([1,0,0]))
|
Blender python API quick-start
Syntax highlighting by Pygments.