Hi!
I have 2 perpendicular vectors. How I can rotate object using this vectors?
using two perpendicular vectors, you can get a third axis by calculating the cross product of the first and second axis.
up_vector = vec1.normalized()
right_vector = vec2.normalized()
fwd_vector = up_vector.cross(right_vector)
once you have three cartesian axes vectors you can easily create a 3x3 rotation matrix once you know how a blender matrix is composed
and lastly, once youβve got a 3x3 rotation matrix, you can transform your object using matrix multiplication
oldmat.to_3x3() @ new_rot.to_3x3()
2 Likes
Yes, I can get 3 vector, but how rotate object, using this 3 vectors?
Itβs get bad result
bpy.ops.transform.rotate(value=math.acos(vector_speed[0]), orient_axis='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)))
bpy.ops.transform.rotate(value=math.acos(vector_speed[1]), orient_axis='Y', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)))
bpy.ops.transform.rotate(value=math.acos(vector_speed[2]), orient_axis='Z', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)))
I get vector using this code. But how rotate my object?
def make_matrix(v1, v2, v3):
v1 = array_to_vector(v1)
v2 = array_to_vector(v2)
v3 = array_to_vector(v3)
a = v2-v1
b = v3-v1
c = a.cross(b)
if c.magnitude>0:
c = c.normalized()
else:
raise BaseException("A B C are colinear")
b2 = c.cross(a).normalized()
a2 = a.normalized()
m = mathutils.Matrix([a2, b2, c]).transposed()
s = a.magnitude
m = mathutils.Matrix.Translation(v1) * mathutils.Matrix.Scale(s,4) * m.to_4x4()
return m
Itβs work
vector_i = vector_speed
vector_j = normal_for_plane
vector_k = new_normal
vertice0 = [0, 0, 0]
matrix = np.array([[vector_i[0], vector_i[1], vector_i[2], 0],
[vector_j[0], vector_j[1], vector_j[2], 0],
[vector_k[0], vector_k[1], vector_k[2], 0],
[vertice0[0], vertice0[1], vertice0[2], 1]]).T
print(444, matrix)
print(555, mathutils.Matrix(matrix))
print(666, mathutils.Matrix(matrix).decompose())
loc, rot, scale = mathutils.Matrix(matrix).decompose()
print(777, rot, rot.w, rot.x, rot.y, rot.z)
bpy.context.active_object.rotation_mode = 'QUATERNION'
bpy.context.active_object.rotation_quaternion = rot