1
$\begingroup$

So this is a classic question, I guess, but still I couldn't find an answer to my specific variation: I have an object where I select a surface with a given normal vector. I then want to rotate the object so that this normal is parallel to some target_vector. I am basing it on this code:

rot = normal.rotation_difference(target_vector)
obj.rotation_quaternion += rot # Wrong. But this line is the essense of my question

I can to it when converting to Euler angles and then simply adding the rotation vectors, but I get into troubles with gimbal lock. So how do I rotate with a given quaternion? Also Will this method be robust for future target vectors and normals? I'm thinking of if any coordinates being local or global and taking the current rotation into account etc.

$\endgroup$
0
$\begingroup$

I figured it out. The code is simply:

rot = normal.rotation_difference(target_vector)
obj.rotation_quaternion = rot

BUT I had transformed my normal vector the the global coordinate system like this:

world_matrix = obj.matrix_world 
normal = obj_face.normal           
normal = world_matrix.to_3x3() @ normal  

It worked if I skipped the last line with the matrix multiplication. I'm haven't wrapped my head around why this did the trick, as I just tried it out of intuition (and desperation). So I guess I have something to think about over the weekend :-)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.