Smartbutton fun

Here’s a method to control whether or not to run a hook script:

Create a .smartbutton bundle and have it create and toggle an envvar. Then in the hook files, check that same envvar using get_envvar.

"""Toggles No Plot functionality"""
from pyrevit import script

def __selfinit__(script_cmp, ui_button_cmp, __rvt__):
    script.set_envvar('NPLTstate', True)
    state = script.get_envvar('NPLTstate')
    script.toggle_icon(state)

if __name__ == '__main__':
    state = script.get_envvar('NPLTstate')
    new_state = not state
    script.set_envvar('NPLTstate', new_state)
    script.toggle_icon(new_state)

And in the hook script, at the top:

from pyrevit import script

if not script.get_envvar('NPLTstate'):
    script.exit()

The one issue I’m having is that the initial call to toggle_icon, in the selfinit function, does not seem to go through - the icon is not loaded this way. Are there any pyrevit methods to get the icon to load?

Another potential issue is that the script.get_envvar method is always set to True on startup. This is ok for now, but I am curious how one would go about initializing a more permanent envvar?

I’m hoping that someone will find this method of controlling hook scripts useful! This way you don’t need to set up your own eventhandlers :+1:

2 Likes

Here’s an update that gets the icon to load in the selfinit function:

def __selfinit__(script_cmp, ui_button_cmp, __rvt__):
    script.set_envvar('NPLTstate', True)
    state = script.get_envvar('NPLTstate')
    if state:
        has_update_icon = script_cmp.get_bundle_file('on.png')
    else:
        has_update_icon = script_cmp.get_bundle_file('off.png')
    ui_button_cmp.set_icon(has_update_icon)
1 Like