2010-04-11 14:22:27 +00:00
|
|
|
import mathutils
|
2011-10-17 02:20:53 +00:00
|
|
|
import math
|
2010-04-11 14:22:27 +00:00
|
|
|
|
2025-01-31 15:11:29 +11:00
|
|
|
# Create a location matrix.
|
2011-10-17 02:20:53 +00:00
|
|
|
mat_loc = mathutils.Matrix.Translation((2.0, 3.0, 4.0))
|
|
|
|
|
|
2025-01-31 15:11:29 +11:00
|
|
|
# Create an identity matrix.
|
2011-10-17 02:20:53 +00:00
|
|
|
mat_sca = mathutils.Matrix.Scale(0.5, 4, (0.0, 0.0, 1.0))
|
|
|
|
|
|
2025-01-31 15:11:29 +11:00
|
|
|
# Create a rotation matrix.
|
2011-10-17 02:20:53 +00:00
|
|
|
mat_rot = mathutils.Matrix.Rotation(math.radians(45.0), 4, 'X')
|
|
|
|
|
|
2025-01-31 15:11:29 +11:00
|
|
|
# Combine transformations.
|
2019-06-06 17:12:49 +02:00
|
|
|
mat_out = mat_loc @ mat_rot @ mat_sca
|
2011-10-17 02:20:53 +00:00
|
|
|
print(mat_out)
|
|
|
|
|
|
2025-01-31 15:11:29 +11:00
|
|
|
# Extract components back out of the matrix as two vectors and a quaternion.
|
2011-10-17 02:20:53 +00:00
|
|
|
loc, rot, sca = mat_out.decompose()
|
|
|
|
|
print(loc, rot, sca)
|
|
|
|
|
|
2025-01-31 15:11:29 +11:00
|
|
|
# Recombine extracted components.
|
2021-05-14 19:46:19 +03:00
|
|
|
mat_out2 = mathutils.Matrix.LocRotScale(loc, rot, sca)
|
|
|
|
|
print(mat_out2)
|
|
|
|
|
|
2025-01-31 15:11:29 +11:00
|
|
|
# It can also be useful to access components of a matrix directly.
|
2011-10-17 02:20:53 +00:00
|
|
|
mat = mathutils.Matrix()
|
|
|
|
|
mat[0][0], mat[1][0], mat[2][0] = 0.0, 1.0, 2.0
|
|
|
|
|
|
|
|
|
|
mat[0][0:3] = 0.0, 1.0, 2.0
|
|
|
|
|
|
2025-01-31 15:11:29 +11:00
|
|
|
# Each item in a matrix is a vector so vector utility functions can be used.
|
2011-10-17 02:20:53 +00:00
|
|
|
mat[0].xyz = 0.0, 1.0, 2.0
|
2025-08-16 17:38:20 +10:00
|
|
|
|
|
|
|
|
# Direct buffer access is supported.
|
|
|
|
|
print(memoryview(mat).tobytes())
|