I want to add a cancel button to quit the userpref window,but it seems no such api to do this thing.How can I solve the problem with Python?
1 Answer
$\begingroup$
$\endgroup$
1
A Preference Window is a temporary window attached to the main window with one area of type 'Preferences' only.
So first identify all temporary windows by window.screen.is_temporary and check the area.type. Next you need to override the context as the button or script is called from the main window. With Blender 3.2. there was a change for the context override, so a switch was used here (tested 2.9.3, 3.6.7, 4.0.1).
import bpy
def close_pref_window():
from bpy import context
pref = False
for window in bpy.context.window_manager.windows: # loop windows
screen = window.screen
if not screen.is_temporary: # search temporary windows
continue
for area in screen.areas:
if not area.type == 'PREFERENCES': # search area Preferences
continue
if bpy.app.version >= (3, 2, 0): # override Blender 3.2+
with context.temp_override(window=window, area=area):
bpy.ops.wm.window_close()
else: # override Blender < 3.2
override = {'window': window, 'screen': screen, 'area': area}
bpy.ops.wm.window_close(override)
pref = True
break
return "preference window closed" if pref else "no preference window found"
# testcall
print(close_pref_window())
-
$\begingroup$ Oh , it's works ! Now I learn the usage of temporary window and window_close , thanks ! $\endgroup$– fageCommented Dec 26, 2023 at 2:20