There is a method rotate
is mathutils.Matrix
class with the signature rotate(other)
.
The documentation says the other can be of type Euler
,Quaternion
or Matrix
.
But no matter what I pass as the argument to rotate
, I always get a ValueError: Matrix.rotate(): must have 3x3 dimensions
I tried passing each of the following to the rotate
method:
Euler((0,0,math.pi/2), 'XYZ')
Euler((0,0,math.pi/2), 'XYZ').to_quaternion()
Euler((0,0,math.pi/2), 'XYZ').to_matrix()
(This is actually a 3x3 matrix)
Also tried converting the Euler
to a 4x4 matrix and then applying it to the given matrix - mw (e.g. Euler((0,0,math.pi/2), 'XYZ').to_matrix().to_4x4() @ mw
) . But this is not giving the correct result.
So how to rotate a 4x4 matrix?
Edit1: The linked question (which is supposed to answer the question) talks about creating a rotation matrix and applying it. The class method Rotation
accepts an axis which needs to be one of 'X', 'Y' and 'Z'. This does not allow arbitrary rotation about any Vector
as it's possible with an Euler
object. So creating a rotation matrix does not solve the problem. Also, it does not answer the question how to use the rotate
method of matrix.
Edit2: Giving below comparison between rotating around local Z axis through pi/2 radians from viewport (pressing R-Z-90) and running the script given in the answer by @Markus von Broady.
Rotating manually:
Rotating via script:
L, R, S = M.decompose(); R.rotate(Euler((0,0,math.pi/2))); M = Matrix.LocRotScale(L, R, S)
... If you want to rotate around the world origin:M = Euler((0,0,math.pi/2)).to_matrix().to_4x4() @ M
$\endgroup$