Toolbar button to run a specific script

Toolbar button to run a specific script

a.g.ashwood
Contributor Contributor
1,769 Views
5 Replies
Message 1 of 6

Toolbar button to run a specific script

a.g.ashwood
Contributor
Contributor

Hi All,

 

I am working on a script at the moment and I must have run it a hundred times already and it's nowhere near finished. So, what I am looking for is a way to create a button on the toolbar which will run this user script when clicked rather than having to do shift-S, select the script and click Run every time.

 

Is this possible?

0 Likes
Accepted solutions (1)
1,770 Views
5 Replies
Replies (5)
Message 2 of 6

BrianEkins
Mentor
Mentor
Accepted solution

That's not currently possible. It would be a nice feature to have.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 6

Jorge_Jaramillo
Collaborator
Collaborator

Hi @a.g.ashwood ,

 

You can create an AddIn for it.

Here is the code for it:

 

import adsk, adsk.core, adsk.fusion, adsk.cam, traceback

app = adsk.core.Application.get()
ui = app.userInterface

WORKSPACE_ID = 'FusionSolidEnvironment'
TOOLBARTAB_ID = "ToolsTab"
TOOLBARPANEL_ID = "SolidScriptsAddinsPanel"
CMD_ID = "Test"

_handlers = []

def run(context):
    global _handlers

    cmdDef = ui.commandDefinitions.itemById(CMD_ID)
    if not cmdDef:
        cmdDef = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_ID, CMD_ID, ".\\")
    onCommandCreated = scriptExecuteHandler()
    cmdDef.commandCreated.add(onCommandCreated)
    _handlers.append(onCommandCreated)

    workspace = ui.workspaces.itemById(WORKSPACE_ID)
    toolbar_tab = workspace.toolbarTabs.itemById(TOOLBARTAB_ID)
    panel = toolbar_tab.toolbarPanels.itemById(TOOLBARPANEL_ID)
    control = panel.controls.addCommand(cmdDef)
    control.isPromoted = True


def stop(context):
    global _handlers
    _handlers = []

    try:
        ui.commandDefinitions.itemById(CMD_ID).deleteMe()
    except:
        pass

    workspace = ui.workspaces.itemById(WORKSPACE_ID)
    toolbar_tab = workspace.toolbarTabs.itemById(TOOLBARTAB_ID)
    panel = toolbar_tab.toolbarPanels.itemById(TOOLBARPANEL_ID)
    try:
        panel.controls.itemById(CMD_ID).deleteMe()
    except:
        pass

class scriptExecuteHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, eventArgs: adsk.core.CommandCreatedEventArgs) -> None:
        # TODO: your script goes here!!!
        app.log(f'{eventArgs.firingEvent.sender.name} executed!')

 

 

You need to place your script in scriptExecuteHandler.notify() to be run when you click on the button.

For this case the button is created under Design workspace > Utilities tab > AddIns pannel.  You can change its place where it'd be more convenient for you.

 

Hope this could help.

 

Regards,

Jorge Jaramillo

 

Message 4 of 6

daniel_hyler
Community Visitor
Community Visitor

How do I add my script in that line of code? Just the name of my script or the entire script?

0 Likes
Message 5 of 6

axalea_thomas
Enthusiast
Enthusiast

I think it would be a paste of the entire script. You will still have to reload the add-in to load new code.

 

(If you are doing code changes between each Run I would suggest clicking "Debug" in the script dialog instead, hit F5 in VS code to run the script the first time and then Ctrl+Shift+F5 to re-run it.)

 

An alternative is to write an add-in that uses the new Scripts API to run your script (I have not yet tried it):

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-D6F04A0D-188C-4866-BB01-383E1B34A240

I would loop over all items till I find the correct one and then call run() on it.

Thomas @ Axalea

My Add-ins (Windows)
⌨️ Toolbar & Shortcuts Editor: Assign any key (and some mouse buttons) as keyboard shortcut!
⤴️ Extra Shortcuts: Assign shortcut to Front, Home view, Look At, Activate Component and more!
My Logo: Put your logo in the Fusion window (for presentations and streaming)
 Axalea Notes - Annotate your model with notes
Message 6 of 6

kandennti
Mentor
Mentor

@a.g.ashwood -San.

 

This is an add-in created for testing purposes, although it should not be made public because it is a preview function of the API.

https://github.com/kantoku-code/Fusion360_ScriptLauncher 

 

This add-in makes it easier to use scripts in sequence.