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


2 Answers 2


You might also try the atexit module

import time
import atexit

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

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()


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')

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):
        return {'FINISHED'}

def register():

def unregister():

if __name__ == "__main__":

enabled here

enter image description here

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

enter image description here

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.

enter image description here

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

You must log in to answer this question.

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