# How to apply all transformations to an object at low level?

I am new to Blender (and to some extent to python). I have pieced together this function that rotates and scales an Object at low level by modifying the matrices:

def rotate_scale_object(inObj, angle, axis, scaling):
orig_loc, orig_rot, orig_scale = inObj.matrix_world.decompose()
orig_loc_mat   = Matrix.Translation(orig_loc)
orig_rot_mat   = orig_rot.to_matrix().to_4x4()
orig_scale_mat = (Matrix.Scale(scaling,4,(1,0,0)) @ Matrix.Scale(scaling,4,(0,1,0)) @ Matrix.Scale(scaling,4,(0,0,1)))
# assemble the new matrix
inObj.matrix_world = orig_loc_mat @ rot_mat @ orig_rot_mat @ orig_scale_mat
#update_matrices(inObj)
return inObj


I call it in my script as follow:

obj = bpy.context.active_object
obj = scale_rotate_smooth_color(obj, angle = -90, axis = "X", scaling = 0.01)


It works very well: the object is rotated & scaled as expected. Yet, I am missing a way to "apply" these transformations to the object, as one would do by pressing Ctrl + A and select Apply all transformations.

Is there a way to do this "low level", without resorting to bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)?

• Another tip, radians are used as default unit of rotation. Instead of passing angle as degrees and converting in methods, would pass converted angle to method.. eg foo(ob, radians(90)) A property with unit='ANGLE' will display in UI as degrees if that is the users selection for angle, but will always be radians internally. IMO it avoids confusion to stick to radians. – batFINGER Nov 29 '19 at 17:19
• Thanks for the suggestion. I will definitively implement it this way. – amaizel Dec 1 '19 at 16:00

Mesh transform

# transform the mesh using the matrix world
ob.data.transform(ob.matrix_world)
# then reset matrix to identity

ob.matrix_world = Matrix()


Recent answer where I used Mesh.transform Problem with rotating objects by script

• Exactly what I was missing. Thanks a lot. – amaizel Dec 1 '19 at 16:07
• cheers, prob should have used mult by inverse instead of setting to identity. eg to apply rotation make a rotation matrix, R apply it to mesh me.transform(R), then mult matrix_world by R.inverted() – batFINGER Dec 1 '19 at 16:15