1
$\begingroup$

I want to use Python to run an add-on

Something like:

import bpy
bpy.ops.export_mesh.paper_model()

But I need to change some settings before I run the add-on. I don't mind whether the settings are changed individually like this

import bpy
op = bpy.context.active_operator

op.page_size_preset = 'A3'
op.scale = 25.0

bpy.ops.export_mesh.paper_model()

or I call upon an existing preset like this:

import bpy

bpy.ops.script.execute_preset(filepath="/Users/paulmmxx/Library/Application Support/Blender/3.3/scripts/presets/operator/export_mesh.paper_model/psg3.py", menu_idname="WM_MT_operator_presets")

bpy.ops.export_mesh.paper_model()

Neither of these guesses worked because the settings were not changed.

Is there a way to do this??

$\endgroup$

1 Answer 1

2
$\begingroup$

You're missing three things :

  • Invoking the operator instead of executing it - This will open the popup window
  • Order of execution - Invoke the operator, restore the preset, execute the operator
  • Context override - The operator must be made aware of the window that just popped up
import bpy

preset_path = "/Users/paulmmxx/Library/Application Support/Blender/3.3/scripts/presets/operator/export_mesh.paper_model/psg3.py"

def preset_and_run():
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        if screen.name == "temp":
            with bpy.context.temp_override(window=window, area=screen.areas[0]):
                bpy.ops.script.execute_preset(filepath=preset_path, menu_idname="WM_MT_operator_presets")
                bpy.ops.file.execute()
    
bpy.ops.export_mesh.paper_model("INVOKE_DEFAULT")
bpy.app.timers.register(preset_and_run, first_interval=0.1)

We use an Application Timer to add a small delay otherwise the popup window isn't yet registered.

I'll link this thread for completeness about operator overrides.

$\endgroup$
1
  • 1
    $\begingroup$ Thank you so much @Gorgious That script works perfectly and the links to other things were very useful. I found the Application timer very interesting and have used it for some simulations. The instructions say, “...avoid comments like thanks”, so I will just post this and then soon delete it. $\endgroup$ Commented May 27, 2023 at 9:06

You must log in to answer this question.

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