Basics of Local and Global Coordinate systems
Each object in blender has a number of matrices. Its global transform matrix ob.matrix_world
its local space matrix ob.matrix_local
. The basis matrix ob.matrix_basis
, this is what you see in the "editor"
and its parenting matrix ob.matrix_parent_inverse
(This is set when choosing parenting Keep Offset for example)
Relation between basis and local
ob.matrix_local = ob.matrix_parent_inverse @ ob.matrix_basis
As an example, select an object and calculate its global matrix by going up the hierarchy chain.
import bpy
from mathutils import Matrix
context = bpy.context
ob = context.object
M = Matrix()
print(ob.matrix_world)
while ob.parent:
M = (ob.matrix_parent_inverse @ ob.matrix_basis) @ M
ob = ob.parent
M = ob.matrix_basis @ M
print(M)
similarly
import bpy
from mathutils import Matrix
context = bpy.context
ob = context.object
M = Matrix()
print(ob.matrix_world)
while ob.parent:
M = ob.matrix_local @ M
ob = ob.parent
M = ob.matrix_local @ M
print(M)
Result on test case, Cube is parent of Cube.001 is parent of Cube.002 some random transforms applied to each.
<Matrix 4x4 ( 0.3256, -0.9107, 0.8122, -3.8782)
(-0.6125, -0.9379, -0.8588, -6.2457)
( 1.5886, 0.1065, -0.9282, 4.2578)
( 0.0000, 0.0000, 0.0000, 1.0000)>
<Matrix 4x4 ( 0.3256, -0.9107, 0.8122, -3.8782)
(-0.6125, -0.9379, -0.8588, -6.2457)
( 1.5886, 0.1065, -0.9282, 4.2578)
( 0.0000, 0.0000, 0.0000, 1.0000)>