Write the Rotation Matrix Values of an Object by XYZ individually?

So I've got the matrix values in a row format like this.

My code:

import bpy
obj = bpy.context.object

f.write(str(obj.matrix_world))


However I'm not quite sure how I can make this write for X, Y, and Z individually. Specifically I need the rotation values which are setup like this in the grid.

$$R_x=\begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & \cos\theta & -\sin\theta & 0\\ 0 & \sin\theta & \cos\theta & 0\\ 0 & 0 & 0 & 1 \end{bmatrix}$$

$$R_y=\begin{bmatrix} \cos\theta & 0 & \sin\theta & 0\\ 0 & 1 & 0 & 0\\ -\sin\theta & 0 & \cos\theta & 0\\ 0 & 0 & 0 & 1 \end{bmatrix}$$

$$R_z=\begin{bmatrix} \cos\theta & -\sin\theta & 0 & 0\\ \sin\theta & \cos\theta & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1 \end{bmatrix}$$

How to make the three basic rotation matrices rotate around the x, y, or z axis.

import bpy
from math import pi
from mathutils import Vector, Matrix, Euler

theta = pi / 6

for i in range(3):
angle = Vector()
angle[i] = theta
mat = Euler(angle, 'XYZ').to_matrix().to_4x4()
print(mat, "\n")


Or directly

from math import pi, sin, cos
from mathutils import Matrix

theta = pi / 6
s = sin(theta)
c = cos(theta)

mat = Matrix()
mat[1][1] = c
mat[1][2] =-s
mat[2][1] = s
mat[2][2] = c

print(mat, "\n")

• Suggest creating a 4x4 rotation matrix of theta radians around x axis via mat = Matrix.Rotation(theta, 4, 'X') Nov 18 '17 at 2:55