Understanding matrix operations in Blender

I need to refine my understanding of matrix operations in Blender, specifically verion 2.8

This following code does a number of object manipulations

import bpy
import mathutils
import math


bpy.ops.mesh.primitive_monkey_add(size=2, enter_editmode=False, location=(0, 0, 0))

# create a location matrix
mat_loc = mathutils.Matrix.Translation((5.0, 0.0, 0.0))

# create a rotation matrix
mat_rot = mathutils.Matrix.Rotation(math.radians(45.0), 4, 'Z')
mat_rot2 = mathutils.Matrix.Rotation(math.radians(-45.0), 4, 'X')

# initiallay Suzanne and world coordinate system are identical. 
suz.matrix_world = suz.matrix_world @ mat_loc 
# this rotates around global z-axis
suz.matrix_world = mat_rot @ suz.matrix_world
# this rotates around local x-axis
suz.matrix_world = suz.matrix_world @ mat_rot2

In the last two operations, the suz object is rotated. Why does the axis of rotation change from local to global depending on matrix_world being first or last in the matrix multiplication?

Matrix multiplication is almost always non-commutative. This page and this page should give you a general overview.

1 Like

Take a look at this linear algebra series on 3blue1brown’s youtube channel for some great, clearly-explained info about matrix math and vectors.

1 Like

Thanks @ThatAsherGuy and @Josephbburg for the advices. I’ll will go study linear algebra.

I found these two youtube videos very helpful. I can answer my question myself: https://youtu.be/4I2S5Xhf24o https://youtu.be/KAW7lXxMSb4