Toggle Button Icon Outside Script

Is it possible to toggle the state of a smart button from a different script?

Inside button1.pushbutton I am using:

from pyrevit.loader import sessionmgr

sessionmgr.execute_extension_startup_script("button2.pushbutton\", my_extension) 


from pyrevit import script

print("Smart Button was Pressed")

Pressing button1 prints the “Smart Button was Pressed” but does not toggle the state of the button2 icon.

That is because the toggle_icon def goes grab your current bundle icons. I would try and bring the on off png’s to both bundles and start from there identifying which location is used for the png.

Or you could use the set_icon def instead and specify the on/off location

def toggle_icon(new_state, on_icon_path=None, off_icon_path=None):
    """Set the state of button icon (on or off).

    This method expects on.png and off.png in command bundle for on and off
    icon states, unless full path of icon states are provided.

        new_state (bool): state of the ui button icon.
        on_icon_path (str, optional): full path of icon for on state.
        off_icon_path (str, optional): full path of icon for off state.
    # find the ui button
    uibuttons = get_all_buttons()
    if not uibuttons:
        mlogger.debug('Can not find ui button.')

    # get icon for on state
    if not on_icon_path:
        on_icon_path = get_bundle_file('on.png')
        if not on_icon_path:
            mlogger.debug('Script does not have icon for on state.')

    # get icon for off state
    if not off_icon_path:
        off_icon_path = get_bundle_file('off.png')
        if not off_icon_path:
            mlogger.debug('Script does not have icon for on state.')

    icon_path = on_icon_path if new_state else off_icon_path
    mlogger.debug('Setting icon state to: %s (%s)',
                  new_state, icon_path)

    for uibutton in uibuttons:

Thanks Jean-Marc!

Inside button1 to set the icon of button2:

from pyrevit.coreutils import ribbon

1 Like