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 ;)
.matrix_world
by the inverted rotation matrix (mat.inverted()
), that should do the trick. $\endgroup$