Mesh transform
Does a child object inherit the matrix from the parent?
Applying the transform.
Set the basis matrix value to identity for any or all of location, rotation scale.
Transform the mesh and or child objects to reflect the change
For all transforms.
import bpy
context = bpy.context
ob = context.object
mb = ob.matrix_basis
if hasattr(ob.data, "transform"):
ob.data.transform(mb)
for c in ob.children:
c.matrix_local = mb @ c.matrix_local
ob.matrix_basis.identity()
To apply only rotation.
Similarly will show the process of only applying rotation. Transform the data and children such that the rotation part of matrix basis is identity.
Method below uses the Matrix.decompose
to reduce the transform into its three components. The rotation is defined as a quaternion. Another way to decompose is discussed in
How to Decompose and Compose the local transform matrix
import bpy
context = bpy.context
from mathutils import Matrix
ob = context.object
mw = ob.matrix_world
mb = ob.matrix_basis
loc, rot, scale = mb.decompose()
# rotation
T = Matrix.Translation(loc)
R = rot.to_matrix().to_4x4()
S = Matrix.Diagonal(scale).to_4x4()
if hasattr(ob.data, "transform"):
ob.data.transform(R)
for c in ob.children:
c.matrix_local = R @ c.matrix_local
ob.matrix_basis = T @ S
For rotation and scale
...:
ob.data.transform(R @ S)
for c in ob.children:
c.matrix_local = (R @ S) @ c.matrix_local
ob.matrix_basis = T
ie set the basis to make identity of applied property, transform mesh and children by the jconverse. eg applying all is transforming by T @ R @ S
and setting to I @ I @ I
transform & scale T @ I @ S
--> I @ R @ S
(where I
is identity matrix)
Method for all or any
import bpy
from mathutils import Matrix
def apply_transfrom(ob, use_location=False, use_rotation=False, use_scale=False):
mb = ob.matrix_basis
I = Matrix()
loc, rot, scale = mb.decompose()
# rotation
T = Matrix.Translation(loc)
#R = rot.to_matrix().to_4x4()
R = mb.to_3x3().normalized().to_4x4()
S = Matrix.Diagonal(scale).to_4x4()
transform = [I, I, I]
basis = [T, R, S]
def swap(i):
transform[i], basis[i] = basis[i], transform[i]
if use_location:
swap(0)
if use_rotation:
swap(1)
if use_scale:
swap(2)
M = transform[0] @ transform[1] @ transform[2]
if hasattr(ob.data, "transform"):
ob.data.transform(M)
for c in ob.children:
c.matrix_local = M @ c.matrix_local
ob.matrix_basis = basis[0] @ basis[1] @ basis[2]
# test call
apply_transfrom(bpy.context.object, use_rotation=True)
Recent answer using Mesh.transform
Problem with rotating objects by script