Method to launch command from Fusion360 API for user.

Method to launch command from Fusion360 API for user.

Ryryog25
Participant Participant
1,150 Views
5 Replies
Message 1 of 6

Method to launch command from Fusion360 API for user.

Ryryog25
Participant
Participant

I'll back up first and explain my use-case, since there may be a better way to do this (although my current setup it technically unsupported, which is why I'm pursuing this option)

I'm on Linux, so running Fusion360 through Wine. I also use a SpaceMouse quite heavily, and I've gotten that to work for navigation without issue. However, one thing I relied on under macOS was radial menus to expand the functionality of my SpaceMouse side buttons, and there's no such radial menu tool for the spacemouse under linux. I've created my own program for this that simulated a keyboard entry for macros when an option was selected, but I quickly realized that those keyboard entries don't actually get properly transmitted through Wine.

Because of this, my plan was to create an AddIn using the Fusion 360 API which would establish a socket connection to my radial menu program, and when I choose a menu option, an event would be fired which could be received on the Fusion API side. Up to this point, I've a plan for how this can work, I don't need help (well now I've jinxed myself) with that.

What I'm having a hard time with is trying to figure out how my Fusion AddIn could open a command for a user to interact with. I can't seem to figure out how to do it. I either can't seem to find a method for doing this or I've found documentation such as this which seems to indicate that executing a function from the API is possible (maybe, unless this is purely something to be ran in the background) But the syntax is not explained, it's just a brief one liner "Any command (standard Fusion 360 or API created commands) can be run by the user clicking a button or by a program calling the command definitions execute method." 

With that said then, does anyone know how to go about this? I'm only interested in launching native fusion commands (e.g. Extrude, Revolve, Create Sketch, etc.) but if it could be interoperable with custom commands it'd be good to know.

Thank you for any help.

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

BrianEkins
Mentor
Mentor
Accepted solution

All commands, both native and custom, are represented by a CommandDefinition object in the API. The code below shows accessing the entire list of commands and writing information about them to a file.

def writeCommands(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        result = ''
        cmdDef = adsk.core.CommandDefinition.cast(None)
        for cmdDef in ui.commandDefinitions:
            result += cmdDef.name + ', ' + cmdDef.id + '\n'

        output = open('C:/Temp/FusionCommands.txt', 'w')
        output.writelines(result)
        output.close()

        ui.messageBox('Finished writing to "C:/Temp/FusionCommands.txt"')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

The important bit of information is the ID. You can use this to quickly get a specific CommandDefinition and once you have the desired CommandDefinition, you can call its execute method, which will start the command, just the same as if the user clicked the command button in the UI.

 

You can use the CommandDefinitions.itemById method to easily get a specific command, if you know it's ID.

 

I think this topic in the help will also be useful.

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

Ryryog25
Participant
Participant

Gotcha, thank you so much, this seems like exactly the bit of information I was missing!

0 Likes
Message 4 of 6

Ryryog25
Participant
Participant

Interesting... when trying to use itemById() I'm running into an issue:

 

 

 

ui.messageBox("Received Action: \'" + args.additionalInfo + "\'")
cmdDefs = adsk.core.CommandDefinitions
cmdId = str(args.additionalInfo)
cmd = cmdDefs.itemById(cmdId)

 

 

 

sounds like it should work, but I get the error:

 

Failed:


Traceback (most recent call last):

File "C:/users/ryanogurek/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/AddIns/SpacenavdRadialMenus/SpacenavdRadialMenus.py", line 38, in notify

cmd = cmdDefs.itemById(cmdId)

TypeError: itemById() missing 1 required positional argument: 'id'

 

 

With intellisense showing a different method definition than what the Fusion API references:

 

(method) def itemById(
    self: CommandDefinitions,
    id: str
) -> CommandDefinition
Returns the CommandDefinition that has the specified ID.
id : The ID of the command definition to return. Returns the CommandDefinition with the specified ID or null if there isn't a command definition with that ID.

 

 

So it seems like it's taking what's supposed to be the id and using it as this other argument "self"
I then used id=, then I just got the same traceback error but regarding self... so it seems like it's expecting that as an argument.

 

Ok, well then, easy enough, I pass cmdDefs for self... and then I get an error about incompatible types (cmdDefs gets passed as a pointer?):

 

Failed:
Traceback (most recent call last):
  File "C:/users/ryanogurek/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/AddIns/SpacenavdRadialMenus/SpacenavdRadialMenus.py", line 38, in notify
    cmd = cmdDefs.itemById(cmdDefs, cmdId)
  File "C:\Program Files/Autodesk/webdeploy/production/dabca83aceed67f5b8555a5b9697a3fc08792c77/Api/Python/packages\adsk\core.py", line 5996, in itemById
    return _core.CommandDefinitions_itemById(self, id)
TypeError: in method 'CommandDefinitions_itemById', argument 1 of type 'adsk::core::Ptr< adsk::core::CommandDefinitions > const *'

 

 

 

I've also tried just passing None for self, that causes Fusion to hang and I have to force quit it. (I've left it for several minutes, it never recovers)

Not sure what to do with this, since it's not documented?

0 Likes
Message 5 of 6

BrianEkins
Mentor
Mentor

Change your code:

ui.messageBox("Received Action: \'" + args.additionalInfo + "\'")
cmdDefs = adsk.core.CommandDefinitions
cmdId = str(args.additionalInfo)
cmd = cmdDefs.itemById(cmdId)

to this:

ui.messageBox("Received Action: \'" + args.additionalInfo + "\'")
cmdDefs = ui.commandDefinitions
cmdId = str(args.additionalInfo)
cmd = cmdDefs.itemById(cmdId)

 

This gets a CommandDefinitions object by calling the commandDefinitions property of the UserInterface object. You were getting a reference to the class definition.

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

Ryryog25
Participant
Participant

Haha, thanks! Was just about to post that I had figured out my mistake, you're fast!

0 Likes