Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Toolbar button to run a specific script

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
a.g.ashwood
540 Views, 5 Replies

Toolbar button to run a specific script

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?

5 REPLIES 5
Message 2 of 6
BrianEkins
in reply to: a.g.ashwood

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

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

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

Message 5 of 6
axalea_thomas
in reply to: a.g.ashwood

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

Try the Toolbar & Shortcuts Editor and Extra Shortcuts add-ins - assign any key (and some mouse buttons) as keyboard shortcut! - Assign shortcut to Front, Home view, Look At, Timeline Back ... - (Windows only)
Message 6 of 6
kandennti
in reply to: axalea_thomas

@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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report