I'm trying to use Blender to test some matrix transforms but I'm still trying to learn at the same time. The problem is how to use the Python API to apply a rotation matrix, but to start with I have tried this step.
First I added a Mesh Plane then Scaled it along Y axis so it forms a rectangle.
Running the code causes the plane to rotate but the shape is "restricted" by the scale of the shape before the transform. I expected the whole plane to rotate around the x-axis and keep the shape.
import mathutils from mathutils import Matrix from math import radians import bpy mat_rot = mathutils.Matrix.Rotation(radians(15.0), 4, 'X') curr_obj = bpy.context.active_object obj_mat = curr_obj.data obj_mat.transform(Matrix(mat_rot)) obj_mat.update()
I also notice in the Properties Transform, the Rotation X does not change. Can anyone help me understand what's going on?
lemon, thanks for info on using obj.matrix_world. I have now tried this code now, but even the results are still not as expected. The shape doesn't keep the 6*6 square shape and the Rotation X is only 40.1deg instead of the 45deg. Any ideas?
import mathutils from mathutils import Matrix from math import radians import bpy bpy.ops.mesh.primitive_plane_add(location=(0, 0, 0)) bpy.ops.transform.resize(value=(6, 6, 1)) obj = bpy.context.active_object mat_rot = Matrix.Rotation(radians(45.0), 4, 'X') obj.matrix_world = obj.matrix_world @ mat_rot