I have an object that has been rotated. How can I get its rotation, but in the opposite direction (as a matrix that I can use to multiply some coordinates by)?

bpy.types.Object has an attribute called matrix_world which represents the object's current transformation, including its rotation. I have tried using mathutils.Matrix.invert() and mathutils.Matrix.decompose() to obtain the rotation component of the active object in what I hoped was the opposite direction, and some other methods, none of which worked.

I am currently reading up on linear algebra to discover a solution here (so that I can discover the meaning of matrix inversion, transpose, etc.), but in the mean time any advice is appreciated.

  • $\begingroup$ A matrix has no direction, so what do you mean by "opposing"? The same matrix, but with e.g. Z vector flipped? $\endgroup$ – CodeManX May 25 '14 at 21:44
  • $\begingroup$ If you rotate an object around an axis 45 degrees, I mean rotating around the same axis minus 45 degrees. And I know that you can represent a rotation transformation using a matrix, so what I want is a rotation matrix representing that transformation. $\endgroup$ – Qutorial May 25 '14 at 21:54
  • $\begingroup$ Multiply .matrix_world by the inverted rotation matrix (mat.inverted()), that should do the trick. $\endgroup$ – CodeManX May 25 '14 at 22:21

First some information: (skip if tl;dr)
For matrices in general the inverse should actually be what you are looking for, since the inverse of a matrix exactly undoes the transformation of the matrix. And (at least in 3D) for orthonormal matrices (1) like the rotation matrix the inverse is equal to the transposed matrix. Since transposing a matrix is much faster than inverting it, I would use the transposed matrix in this case.

However matrix_world is the so-called World-Matrix, which does not only contain the object's rotation but also its translation and scale. Furthermore it is a matrix that works in 4D homogenuous coordinates, which are often used in computer graphics because they allow you to perform translations and perspective transformations with matrices. You can usualy not work with this matrix directly.

(1) matrices whose column vectors are normalized and orthogonal to each other

The solution:
You were on the right track with decompose. However decompose gives you the rotation as a Quaternion, not as a rotation matrix. But you can invert quaternions as well and use them for your calculation.
If you really need a matrix you can convert the quaternion to a matrix by calling mathutils.Quaternion.to_matrix(). This will give you a 3D matrix which you can invert or transpose to get what you want.

And this is how you can get all that stuff:

o = bpy.context.scene.objects['Cube']  # use your object name here
# calculate the translation, rotation and scale
t, r, s = o.matrix_world.decompose()
# calculate the inverse quaternion
r_inv = r.inverted()
# calculate the rotation matrix from the quaternion
r_mat = r.to_matrix()
# calculate the inverse rotation matrix
r_mat_inv = r_mat.inverted()
# calculate the transposed rotation matrix which is the same as the inverted
r_mat_transposed = t_mat.transposed()

Now take what you need ;)

| improve this answer | |

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.