Problem when applying transformations with Python

Hi all.
I am struggling with transformations and the array modifier.
To troubleshooting the issue, I am testing the various steps, and I found a strange thing.
Starting with a normal plane, I use two transformations, for resizing along x and y, and for translating along x and y. Before using the array modifier I try to apply all the transformations, as often suggested in the forums,
It works when working manually with the UI, but when I try with Python, it seems that the translations are resetted, instead than applied. The section of the script is this:

import bpy

# X Ticks

bpy.ops.mesh.primitive_plane_add(size=1)
bpy.context.active_object.name = 'XY X Grid'

bpy.ops.transform.resize(value=(0.2, 25, 0))
bpy.ops.transform.translate(value=(5, 12.5, 0))

# Apply Transformations

bpy.ops.object.transform_apply(location=True, rotation=False, scale=True)

Do you have any suggestions?
Thanks

When you apply the location part of your transform its origin will return to the center of the world (0, 0, 0). Based on the information you’ve provided that appears to be exactly what’s happening, so I’m not sure what you’re trying to do. Maybe you could show us what the expected result is supposed to be when done manually? Based on your script the result looks exactly as I would expect it to.

Hi testure.
You are right that, when applying the transformations, the origin return to the center. But when working manually, the mesh geometry remains in the new position, instead in Python it is resetted, while the scale remain applied. I tried again the whole process manually with the UI, this is the result after applying the same transformations:

right- but what are the settings on your operator that you’re calling manually? i’m 100% sure it’s location, rotation and scale all at the same time. try unchecking rotation, so it matches your script and you’ll see the result is identical. Likewise, if you set rotation to True in your script, it will match what you see in your most recent screenshot.

1 Like

Hi testure.
You are correct. I modified the script as you suggested, by setting rotation to True, and now it exactly matches the situation of my manual screenshot.
Now I am quite surprised. My transformations did not concern rotations, for that reason I left the rotation parameter False when applying them. Why instead is it necessary to put rotation to True, in order to avoid having locations resetted to initial positions? It seem quite confusing actually.
Thanks.

It’s likely a bug. applying transforms in Blender follows a very complex code path that many types of objects use to apply their transforms. As anyone who has worked with matrices can tell you, they are very sensitive to the order transforms are multiplied in. with that much branching logic there’s a lot of room for error. If I had to guess, the scale is being multiplied after the location, when it should probably be multiplied before.

in the short term you can get around this problem by calling the operator twice, once for scale, then again for location (again- the order is important. if you do location first, you’ll have the exact same problem).

Some news.
By performing other tests, I discovered that the application of transformations works as expected if I don’t use ops for performing scaling and translation:

import bpy
import math


#^^^^^ XY GRID

# X Ticks

bpy.ops.mesh.primitive_plane_add(size=1)

bpy.context.active_object.name = 'XY X Grid'

obj = bpy.context.active_object

# Attention to the notation for sum amd multiplication

obj.scale.x *=0.2
obj.scale.y *=25

obj.location.x +=5
obj.location.y +=12.5

# Apply Transformations -------------

bpy.ops.object.transform_apply(location=True, rotation=False, scale=True)

In this case the application of transformation works exactly as in the manual case: it resets only the position of the object origin, but not the geometry. Could this mean that the likely bug is present only in the operator version of transformations?
Thanks

Yeah, that’s definitely strange. Smells like a bug to me, I can’t imagine how that would be intended behavior.

1 Like