2
$\begingroup$

I am trying to scale a mesh via Python similar to how it would be done in Blender itself.

Basically: The Transform Orientation is Global and Pivot Point set to 3D Cursor. Then Select the object. Go into Edit mode. Press A to select all verts. Press S and enter value and press Enter to have it scaled the way I want.

When I try this via the Python console or in a script it always seems to scale as if Pivot Point is set to Active Element/Median Point - can not really tell which but certainly not 3D Cursor.

I've tried this in the Python console for example expecting it to scale relative to cursor but it did not work like that. The mesh was selected and in Edit Mode to test this. Also tried without the context override with same results.

view3d = next(area for area in bpy.context.screen.areas if area.type == 'VIEW_3D')
with bpy.context.temp_override(area=view3d):
    bpy.context.scene.tool_settings.transform_pivot_point = "CURSOR"
    bpy.ops.transform.resize(value=(0.794479,0.794479,0.794479))

Blender 3.3

$\endgroup$

2 Answers 2

2
$\begingroup$
matrix = object_being_transformed.matrix_world.to_3x3()
bpy.ops.transform.resize(value=scale_value, orient_matrix=matrix)
$\endgroup$
1
$\begingroup$

I too have been searching for how to do this for hours yesterday and fell into a rabbit hole about contexts and transform polling, it is quite irritating that the answer is quite simple.

The Blender API Documentation for bpy.ops.transorm.resize offers a center_override parameter that takes in a vector. In order to scale the mesh relative to the position of the cursor, simply pass the cursor's location as the center_override.

  • NOTE: It is not required to change the transform_pivot_point to the 3D cursor to be able to do this, center_override is pretty much the equivalent of doing it through scripting. It would seem as though Blender ignores the real location of the 3D cursor when performing bpy.ops.transform.resize, even if the Transform Pivot Point has been set as such. Similarly, no context overrides are necessary to perform this operation. Kinda weird.

All that's required of your entire script if you wanted to do this single operation is to import bpy and do the following:

   bpy.ops.transform.resize(value=(0.794479,0.794479,0.794479), center_override=bpy.context.scene.cursor.location)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .