I'm trying to calculate the vector difference between a face normal and an object. This is similar to some other questions, such as this one:

Python script get face normal, then set it as another object's orientation (Z-axis) accordingly

I can get the cone to align properly in the example, but a very similar action using a vector other than 'Up' doesn't rotate the way I would expect.

In this example the cube is facing the opposite direction of the wall. I simply want to calculate the angle difference and rotate the cube to match the wall.

enter image description here

To accomplish that I use the following code (skipping to the important bits):

>>> norm
Vector((-1.0, 0.0, 0.0))

>>> cabvec
Vector((1.0, 0.0, 0.0))

>>> norm.rotation_difference(cabvec)
Quaternion((-4.371138828673793e-08, -0.0, -0.7071067690849304, -0.7071067690849304))

Which flips it around, and turns it on its side (which may be happening to the cone also, but the cone shape is forgiving in this aspect).

What I would want in this situation is a result that simply flips the object on the z-axis 180 degrees. Which in the quaternion nomenclature would look like this:

>>> mathutils.Quaternion((0.0, 0.0, 1.0), math.radians(180.0))
Quaternion((-0.0, -0.0, -0.0, -1.0))

So I'm missing something on how to properly set up and calculate these rotations.


So this only appears to not work when the vectors are in opposite directions. If I move the wall slightly then it works fine:

norm -->  <Vector (-0.9970, -0.0776, 0.0000)>
qrot -->  <Quaternion (w=0.0388, x=-0.0000, y=0.0000, z=0.9992)

I'm just learning about these rotation systems, but I was under the impression that issues with things like gimble lock would occur with Euler type rotations. Quaternion didn't have these types of limitations.

Either way I'm pretty sure the process itself is good because it works in all the other scenarios. So I don't know if I'm dealing with a math issue (can't perform this type of operation) or Blender issue (maybe need to present the info in a different way).

  • $\begingroup$ Quaternions can interpolate in the shortest path. If your angle is 180°, all the rotations are the same. Two vectors pointing in opposite direction could be rotated around any axis perpendicular to them by the same amount (180°). You will have to set some constraints, like an UP Axis. $\endgroup$
    – Leander
    Oct 9 '19 at 13:38
  • $\begingroup$ Understood... my design problem is that these objects need to spin up to 180 depending on the orientation of the walls. Using Up as the constraint can align the object forward, but doesn't give me the correct spin or orientation. The only way I could make the angle difference work is using the object forward as the constraint. So if that's the only boundary condition I can work around it by checking first and explicitly setting the rotation in that one scenario. $\endgroup$
    – Sam Vimes
    Oct 9 '19 at 15:56

Your Answer

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

Browse other questions tagged or ask your own question.