Is it possible to get a callback before Blender closes?

There are bpy.app.handlers. Unfortunately there is no handler for closing Blender.

So is there a way to to know in a script when blender is about to close?


1 Answer 1


While we could support this via bpy.app.handlers, we decided not to add this since Python already supports exit callbacks, so you can use these (outside of Blender too - for any Python script).

See Python's atexit module.

Simple example use:

def goodbye(name, adjective):
    print("Goodbye, %s, it was %s to meet you." % (name, adjective))

import atexit
atexit.register(goodbye, "Suzanne", "nice")

Note : To unregister the callback when disabling an add-on for instance, you can use :

  • $\begingroup$ ok. This works. But I have a problem with it. When running the script from blender this function gets registered. But when putting it into an addon I get this error: Error: EXCEPTION_ACCESS_VIOLATION $\endgroup$
    – ndee
    Commented Mar 10, 2016 at 10:21
  • $\begingroup$ Try find which line causes the error, This will run when Blender is shutting down - so its possible you access data which is already freed. $\endgroup$
    – ideasman42
    Commented Mar 10, 2016 at 10:25
  • 2
    $\begingroup$ I know this post is very old, but I wanted to add information in case anyone finds this post because of the same problem. If you try to access your add-on's preferences during this event, you will get an access violation error. I was able to access several things from Blender, but not my own addon's preferences. In my opinion, Blender should notify add-ons of a shutdown BEFORE any data is released. $\endgroup$
    – Robert
    Commented Nov 11, 2019 at 12:18
  • $\begingroup$ where do you use atexit.register? i tried placing it in the add-on def register but it doesn't seem to work $\endgroup$
    – mayotic
    Commented Jun 6, 2020 at 17:56

You must log in to answer this question.

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