The Setup:
I have a simple setup with two cube objects with a parent child relationship like so:
The Red cube is a child object of the green cube.
The Red cube has the following transforms:
The green cube has the following transforms:
The Problem
The thing I am confused about is, when I try to decompose the local_matrix of the red cube and compose it back to its original transform matrix. I get different results. Below is a script I used to decompose and compose the local matrix. Any clues why they are different? And what is the correct way to re-compose the local matrix given the translation, rotation and scale matrices.
Script and Result
import bpy
import mathutils
import math
myObject = bpy.data.objects['Cube.002']
print("\n location mat")
loc = myObject.matrix_local.to_translation()
print(loc)
mat_loc = mathutils.Matrix.Translation(loc)
print(mat_loc)
print("\n rotation mat")
rot = myObject.matrix_local.to_quaternion()
print(rot)
mat_rot = rot.to_matrix().to_4x4()
print(mat_rot)
print("\n scale mat")
scl = myObject.matrix_local.to_scale()
print(scl)
mat_scl = mathutils.Matrix.Identity(4)
mat_scl_x = mathutils.Matrix.Scale(scl.x, 4, (1,0,0))
mat_scl_y = mathutils.Matrix.Scale(scl.y, 4, (0,1,0))
mat_scl_z = mathutils.Matrix.Scale(scl.z, 4, (0,0,1))
mat_scl = mat_scl_x @ mat_scl_y @ mat_scl_z
print(mat_scl)
print("\n myLocalMat mat")
myLocalMat = mat_loc @ mat_rot @ mat_scl
print(myLocalMat)
print("\n local mat")
print(myObject.matrix_local)
location mat
<Vector (0.0000, 0.0000, 3.0000)>
<Matrix 4x4 (1.0000, 0.0000, 0.0000, 0.0000)
(0.0000, 1.0000, 0.0000, 0.0000)
(0.0000, 0.0000, 1.0000, 3.0000)
(0.0000, 0.0000, 0.0000, 1.0000)>
rotation mat
<Quaternion (w=0.9280, x=0.0000, y=0.0000, z=0.3726)>
<Matrix 4x4 (0.7224, -0.6915, 0.0000, 0.0000)
(0.6915, 0.7224, 0.0000, 0.0000)
(0.0000, 0.0000, 1.0000, 0.0000)
(0.0000, 0.0000, 0.0000, 1.0000)>
scale mat
<Vector (1.5811, 1.5811, 1.0000)>
<Matrix 4x4 (1.5811, 0.0000, 0.0000, 0.0000)
(0.0000, 1.5811, 0.0000, 0.0000)
(0.0000, 0.0000, 1.0000, 0.0000)
(0.0000, 0.0000, 0.0000, 1.0000)>
myLocalMat mat
<Matrix 4x4 (1.1422, -1.0934, 0.0000, 0.0000)
(1.0934, 1.1422, 0.0000, 0.0000)
(0.0000, 0.0000, 1.0000, 3.0000)
(0.0000, 0.0000, 0.0000, 1.0000)>
local mat
<Matrix 4x4 (1.4142, -1.4142, 0.0000, 0.0000)
(0.7071, 0.7071, 0.0000, 0.0000)
(0.0000, 0.0000, 1.0000, 3.0000)
(0.0000, 0.0000, 0.0000, 1.0000)>
Best Regards. QC



Matrix.decompose()– batFINGER May 26 '21 at 12:46