In my code, some data items are created, then a new window is opened via bpy.ops.wm.window_new
. The user can then do a number of things within this window, manipulating the data just created. I have a function that is supposed to run when the user is done with the window and wants to exit. I've registered an operator that runs this function and closes the window, but what if the user just clicks the red 'X' button in the corner to close the window without calling my operator? How do I make my function run when the user closes the window?
import bpy
a = piece_of_data
b = another_piece_of_data
bpy.ops.wm.window_new()
def on_close():
do_stuff()
bpy.ops.wm.window_close()
print ('window was closed')
class WM_OT_close_my_window(bpy.types.Operator):
bl_idname = 'wm.close_my_window'
bl_label = 'Close my window'
def execute(self, context):
on_close()
return {'FINISHED'}
def draw_item(self, context):
self.layout.operator(WM_OT_close_my_window.bl_idname, icon='CANCEL')
def register():
bpy.utils.register_class(WM_OT_close_my_window)
bpy.types.VIEW3D_HT_header.append(draw_item)
def unregister():
bpy.types.VIEW3D_HT_header.remove(draw_item)
bpy.utils.unregister_class(WM_OT_close_my_window)
if __name__ == '__main__':
register()