2

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

brockmann
  • 12,613
  • 4
  • 50
  • 93

1 Answers1

2

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

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • 2
    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. – batFINGER Sep 17 '21 at 08:57
  • Sounds great, all batHandler approaches are welcome @batFINGER – brockmann Sep 17 '21 at 09:11
  • 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? – batFINGER Sep 26 '21 at 17:05
  • 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. – batFINGER Sep 26 '21 at 17:09
  • @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. – brockmann Sep 26 '21 at 18:21
  • This does not find CubeAction in a simple animation of a cube moving on the Y axis. Million bucks says it's not added to NLA. – batFINGER Oct 02 '21 at 14:17
  • Oh boy, lol. My suggestion simply let it go, lol... I know... it's not easy but I have followed your advice and I'm feeling good. Pretty smart comment below his answer btw... pretty smart. Thanks for keeping me posted, if some volcanic explosion comes up, turn on the bat light or just let me know and try to help @batFINGER – brockmann Oct 02 '21 at 16:43
  • Sage advice, often given by me to others, sometimes not taken. btw Another free hit available https://blender.stackexchange.com/questions/240222/use-2-color-pickers-to-drive-one-value re incorrect use of update method ie using context.scene rather than self will not always work as expected when there are more than one scene. Speaking of comments, tempted to "Maybe they downvoted by mistake @drf speaking of tools, can the mods see deleted comments?" on that OTT whingey whiney why are my perfect answers downvoted meta post. – batFINGER Oct 12 '21 at 07:27
  • @batFINGER lol, I couldn't resist being honest and thought that might help, could point him directly to q No2 and his "righteous rant" over there. My recommendation to be more careful wasn't helpful either: Got two comments yesterday on https://blender.stackexchange.com/a/240372/ (1) "You should mention that your code does not work when no object is selected", I replied with a quote from the answer "Make sure... object is selected and run the script...", (2) "yup, was burried in the answer, add that as comment in your code too." I just flagged both and they are gone, thanks to Duarte I guess. – brockmann Oct 12 '21 at 09:13
  • @batFINGER Yes, apparently they mods can see deleted comments: https://meta.stackexchange.com/questions/126351/are-my-deleted-comments-visible-to-others – brockmann Oct 12 '21 at 09:15
  • Good to know. Have a long saved draft of an answer to that meta, noticed DRF commented on other answer and an upvote on yours at same time. Had already added the triangles question as an example of "either no one should have answered or mine is the only answer" and pretty sure MF has downvoted every other answer on that question.... he has added it as an example of someone, albeit wrongly, giving a reason for a dv on one of his perfect answers. Truly is a prat. Been guilty myself to a degree, but MF is a master https://en.wikipedia.org/wiki/The_Art_of_Being_Right – batFINGER Oct 12 '21 at 09:35
  • Nice article! Ironically, the comments below my answer on meta even emphasizing that my answer is not far away from reality. Was about adding 1-2 lines at the end pointing the reader to the comments below to give another example, lol. Then I realized that the MF is quite old, has a lot of time and most likely tries to "be right" all day long. I think he doesn't deserve so much attention at all. It's always going to be an endless loop as long as the MF is right at some point and I also recall "stop making stupid people famous" @batFINGER – brockmann Oct 12 '21 at 10:30
  • It's getting wild: https://blender.stackexchange.com/a/240486/ @batFINGER – brockmann Oct 12 '21 at 17:33
  • @batFINGER Thanks that you helped me out yesterday on https://blender.stackexchange.com/q/241863/, I had to work and there wasn't much time to look at the source code yesterday. Also, I flagged the comment below: https://blender.stackexchange.com/a/241923/ Have a nice weekend! – brockmann Oct 30 '21 at 14:06