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\script.py", my_extension) 

pushbutton2.pushbutton/script.py:

from pyrevit import script

print("Smart Button was Pressed")
script.toggle_icon(True)

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

Hi,
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
.set_icon(icon_path)

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.

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

    # 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.')
            return

    # 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.')
            return

    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:
        uibutton.set_icon(icon_path)

Thanks Jean-Marc!

Inside button1 to set the icon of button2:

from pyrevit.coreutils import ribbon

ribbon.get_uibutton("button2").set_icon("button2.pushbutton\on.png")
1 Like