I've read in several places that its not safe to rely on constructors for types derived from Blender objects, such as property groups. But now I am creating a pop-up window with an operator class (using context.window_manager.invoke_props_dialog()), and I need to execute code when the window is opened and closed.

Previously, I was using invoke() as my open event, and execute() as my closing event. But I just found out that execute() is only called when the "OK" button is pressed. If the user clicks outside of the window to unfocus the window, it closes without executing. I'm assuming this is basically "cancel", which is cool. But I still want to execute certain code when it happens, if possible.

I've tried implementing _init__(self) and __del__(self) for the operator window, and they both appear to be called when the window opens and closes. But its difficult to know how reliable the behavior will be. Can anyone confirm that it is safe to use these as open/close events for an operator window? Or is there a better way to handle it?


I think I can safely say these are reliable, for a couple reasons:

  • They are reliable for modal operator classes, and there is little difference between a modal operator class and a non-modal operator class, except for a specific function being defined and returning certain values from specific functions. It makes sense that the constructor/destructor can be relied on for all operator classes.

  • See https://docs.blender.org/api/current/info_overview.html (thanks to Brockmann for updated version link) .. If you look under the section titled class mix-in example, it states the following:

    "Notice these classes don’t define an __init__(self) function. While __init__() and __del__() will be called if defined, the class instances lifetime only spans the execution."

If anyone can confirm or has any reason to think otherwise, please let me know. Thanks!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.