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):
        rot_mat = Matrix.Rotation(radians(angle), 4, axis)
        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
        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)?

  • 1
    $\begingroup$ 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. $\endgroup$ – batFINGER Nov 29 '19 at 17:19
  • $\begingroup$ Thanks for the suggestion. I will definitively implement it this way. $\endgroup$ – amaizel Dec 1 '19 at 16:00

Mesh transform

# transform the mesh using the matrix world
# then reset matrix to identity

ob.matrix_world = Matrix()

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

  • $\begingroup$ Exactly what I was missing. Thanks a lot. $\endgroup$ – amaizel Dec 1 '19 at 16:07
  • $\begingroup$ 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() $\endgroup$ – batFINGER Dec 1 '19 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.