1
$\begingroup$

Is there an operator I can call for closing Blender preferences pane? I can open it to show my addon prefs with

bpy.ops.preferences.addon_show(module=<myaddon>)

But what about closing? Scrubbed throu the docs to find nothing.

$\endgroup$

1 Answer 1

2
$\begingroup$

If the Preferences window is the active one you can use bpy.ops.wm.window_close()

Otherwise you have to overwrite the context.

With Blender 3.2 there was a change. Now we better use Context.temp_override(window, area, region, **keywords) for this.

The following example should work with all versions:

edited: thanks to scurest

import bpy

bpy.ops.preferences.addon_show(module='io_scene_obj')   # example

for window in bpy.context.window_manager.windows:   # searching preference window
    screen = window.screen
    for area in screen.areas:
        if area.type == 'PREFERENCES':
    
            # Blender 3.2 and more
            if bpy.app.version >= (3, 2, 0):      
                from bpy import context
                with context.temp_override(window=window, area=area):
                    bpy.ops.wm.window_close() 

            # Blender < 3.2 with override context            
            else:                                                       
                override = {'window': window, 'screen': screen, 'area': area}
                bpy.ops.wm.window_close(override)               

            break
$\endgroup$
3
  • 1
    $\begingroup$ You can use bpy.app.version >= (3, 2, 0). $\endgroup$
    – scurest
    Commented Oct 19, 2022 at 21:15
  • $\begingroup$ Thank you, works perfectly $\endgroup$
    – Kabu
    Commented Oct 20, 2022 at 9:11
  • $\begingroup$ @ scurest: True. That´s easier. Answer edited ..thanks $\endgroup$
    – relaxed
    Commented Nov 10, 2022 at 15:29

You must log in to answer this question.

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