# How to run script on Blender Quit

Is it possible to set Blender quit callback? I just need to execute come code when quiting from Blender

You might also try the atexit module

import time
import atexit

filepath = r"C:\Users\pink_vertex\Documents\Blender\log.txt"

@atexit.register
def on_exit():
with open(filepath, "a") as output:
output.write(time.strftime("Exit on %d.%m at %H:%M:%S\n"))


There's currently no event handler for blender Quit. See event handlers

What I might do in your situation is write an operator and make it an add-on (or add it to an existing add-on), which first runs some arbitrary code, then triggers bpy.ops.wm.quit_blender()

quit_with_sidefx.py:

bl_info = {
"name": "Quit with Side Effects",
"version": (1, 0),
"blender": (2, 76, 0),
"description": "Quit blender after running some code",
"category": "User Interface"}

import bpy

def do_this_code():
print('this happened')
pass

class INTERFACE_OT_quit_withfx(bpy.types.Operator):
bl_idname = "wm.quit_with_sidefx"
bl_label = "Quit With Side Effects"
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
do_this_code()
bpy.ops.wm.quit_blender()
return {'FINISHED'}

def register():
bpy.utils.register_class(INTERFACE_OT_quit_withfx)

def unregister():
bpy.utils.unregister_class(INTERFACE_OT_quit_withfx)

if __name__ == "__main__":
register()


enabled here

triggered either via a custom keyboard shortcut (different topic) or simply via spacebar search

You could ofcourse also add the operator to the File menu. Making modifications in standard menus (like swapping out the existing Quit operator with the Quit With SideEffects operator) is not a convenient way to do this and I won't cover it.

Keyboard shortcuts maybe the easiest way to trigger the operator. It's easy to re-purpose the existing Quit shortcut by changing the operator path.

• Thanx, but user shouldn't know about callback executing. He just quit from blender. And i want to run script at this point. Oct 10 '15 at 18:50