2
$\begingroup$

I'm using a regular save_post handler for my add-on and I'd like to allow the user to turn it off occasionally. Removing the function from handlers.save_post list and re-append seems expensive. Is there any clever trickery to manage that easily? What's a reliable way to temporarily disable the handler?

import bpy

def my_save_handler(dummy):
    print ("Blend file has been saved.")

def register():
    bpy.app.handlers.save_post.append(my_save_handler)
def unregister():
    bpy.app.handlers.save_post.remove(my_save_handler)

if __name__ == "__main__":
    register()

This is a follow-up question emerged from: Modify the Save project function

$\endgroup$

1 Answer 1

2
$\begingroup$

One simple way is to add an if-statement to the body of the handler in order to determine whether the rest of the code should execute by using one additional Bool or EnumProperty declared in the preferences of your add-on:

class MYTOOL_AP_preferences(bpy.types.AddonPreferences):
    ...
    state: bpy.props.BoolProperty(name="Switch")
    ...

def my_save_handler(dummy):
    if state:
        ...

Demo on how the user can disable the handler via Preferences using one EnumProperty:

import bpy

bl_info = {"name": "Stats Add-on", "blender": (2, 82, 0), "category": "System"}

class MYTOOL_AP_preferences(bpy.types.AddonPreferences):
    # this must match the add-on name, use '__package__'
    # when defining this in a submodule of a python package.
    bl_idname = __name__
    
    state: bpy.props.EnumProperty(name="Switch", default = 'ON',
        items=(("ON", "On", "", 1), ("OFF", "Off", "", 2)))
    
    def draw(self, context):
        self.layout.row().prop(self, "state", expand=True)

def my_save_handler(dummy):
    if bpy.context.preferences.addons[__name__].preferences.state == 'ON':
        print ("Blend file has been saved.")
    
def register():
    bpy.utils.register_class(MYTOOL_AP_preferences)
    bpy.app.handlers.save_post.append(my_save_handler)

def unregister():
    bpy.app.handlers.save_post.remove(my_save_handler)
    bpy.utils.unregister_class(MYTOOL_AP_preferences)

if __name__ == "__main__":
    register()
$\endgroup$
14
  • 2
    $\begingroup$ Would consider going the other way and adding a wrapper around the method, rather the if within. SInce the handlers are basically a list of methods, can pop them from one list to another to enable or disable. Coincidentally recently put together a BATHandlers class to handle this very thing among others. $\endgroup$
    – batFINGER
    Sep 17, 2021 at 8:57
  • $\begingroup$ Sounds great, all batHandler approaches are welcome @batFINGER $\endgroup$
    – brockmann
    Sep 17, 2021 at 9:11
  • $\begingroup$ Re other q: My suggestion simply let it go. Reminds me of the righteous rant here ..like a dog with a bone. Given OP's question, it's not worth writing an operator with a poll method, and given OP's answer, using an index (blender collections are a mapping that can be keyed like dict or indexed like a list) not worth catching an IndexErrror. And frankly see no diff in throwing the error or reporting error. We will just have to humbly respect you know who's experience with COBOL or Turbo Pascal, and let him believe he knows best, right? $\endgroup$
    – batFINGER
    Sep 26, 2021 at 17:05
  • $\begingroup$ Or to put another way Ignore the fecker, or anyone that starts ending comments with right... and what does the best practices say about string addition... lol it could be never ending. $\endgroup$
    – batFINGER
    Sep 26, 2021 at 17:09
  • $\begingroup$ @batFINGER lol. Many thanks for your comments, helps a lot, really. This code and answer is so bad and he's so convinced about the quality, it's crazy. I know by now, that he started programming professionally in 1973. He told me to sh*t my pants obviously. Absolutely no interest in such things like his CV or whether he has a dog, won't improve the quality of the Q&A. Tried to discuss, friendly, no chance, immediately searching for new reasons why I'm "wrong", being told to "stop expressing the erroneous belief" or "making the wrong claim", never ending. Entertaining, but it's a lot of time. $\endgroup$
    – brockmann
    Sep 26, 2021 at 18:21

This site is temporarily in read-only mode and not accepting new answers.

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