Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

How to create a Split Button

Anonymous

How to create a Split Button

Anonymous
Not applicable

I'm trying to add a split button to the toolbar panel of the active workspace using the "ToolbarControls.addSplitButton Method"

toolbarControls_var.addSplitButton(defaultDefinition, additionalDefinitions, showLastUsed)

where "additionalDefiinitions" is a CommandDefinition[]. The problem is, how do you create the CommandDefinition[] in python?
I've tried: 

additionalDefinitions = adsk.core.ObjectCollection.create()

additionalDefinitions = []

 

But I keep getting "Wrong number or type of arguments..." or "internal validation error: res".

0 Likes
Reply
Accepted solutions (1)
996 Views
8 Replies
Replies (8)

BrianEkins
Mentor
Mentor

It's a list of command definitions, so you would do something like this:

 

cmdDefs = []
cmdDefs.append(cmdDef1)
cmdDefs.append(cmdDef2)

or 

 

cmdDefs = [cmdDef1, cmdDef2]

I suspect a split button isn't supported by Fusion 360 everywhere.  I think it only uses them in the navigation toolbar at the bottom of the screen.  It's not clear to me where you're trying to create one, but limitations within Fusion 360 may be a problem.

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

Anonymous
Not applicable

Hello, I'm trying to make a button like these 3, with the main buttonDefinition and some extra definitions in the drop down list. I've added my code below.

 

 

Picture1.jpg

 

 

 

 

Source Code:

# -*- coding: utf-8 -*-

"""

Created on Fri Mar 16 09:05:43 2018

 

@Anonymous: jmper

"""

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

def run(context):

try:

app = adsk.core.Application.get()

ui = app.userInterface

cmdDefs = ui.commandDefinitions

 

ActiveWorkspace = ui.activeWorkspace

WorkspacePanels = ActiveWorkspace.toolbarPanels

newPanel = WorkspacePanels.itemById('UTEPPanel')

if not newPanel:

newPanel = WorkspacePanels.add('UTEPPanel', 'UTEP PANEL')

 

# Get the existing command definition or create it if it doesn't already exist.

UTEPcommand = ui.commandDefinitions.itemById('UTEPButtonDef')

if not UTEPcommand:

UTEPcommand = cmdDefs.addButtonDefinition('UTEPButtonDef', 'Component Placement',

'Opens the UTEP GUI that allows for the placement of components from chosen schematic files',

'.//Icons//Keck')

 

# additionalDefinitions = adsk.core.ObjectCollection.create()

additionalDefinitions = []

extracommand = ui.commandDefinitions.itemById('extraButton')

if not extracommand:

extracommand = cmdDefs.addButtonDefinition('extraButton', 'extraButton',

'blah')

extracommand2 = ui.commandDefinitions.itemById('extraButton2')

if not extracommand2:

extracommand2 = cmdDefs.addButtonDefinition('extraButton2', 'extraButton2',

'blah2')

 

 

additionalDefinitions.append(extracommand)

additionalDefinitions.append(extracommand2)

newPanel.controls.addSplitButton(UTEPcommand, additionalDefinitions, False)

 

except:

if ui:

ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

0 Likes

BrianEkins
Mentor
Mentor

You'll want to read through this topic in the online help.  It describes all of the pieces that make up the Fusion 360 API.

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-F31C76F0-8C74-4343-904C-68FDA9BB8B4C

 

In your case what you'll want to do is to create a new ToolbarPanel and then add to that.  There's some sample code at the end of the "Command Definitions" section of the above topic that demonstrates getting an existing panel and adding to it.  Instead of getting an existing panel, you'll create a new one.  Here's some code from an add-in of mine that does this.

 

        # Get the MODEL workspace.
        modelWS = _ui.workspaces.itemById('FusionSolidEnvironment')
        
        # Add a new panel.
        myPanel = modelWS.toolbarPanels.add('myPanel', 'SAMPLE PANEL', 'SolidScriptsAddinsPanel', False)
        
        # Add the buttons to the panel, promoting the first one to show up at the top.
        cntrl1 = myPanel.controls.addCommand(cmdDef1)
        cntrl1.isPromotedByDefault = True
        cntrl1.isPromoted = True

        cntrl2 = myPanel.controls.addCommand(cmdDef2)
        cntrl3 = myPanel.controls.addCommand(cmdDef3)
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes

Anonymous
Not applicable

I've gone through the page you mentioned previously. 
But what I'm trying to do is get the toolbarPanel of the Active workspace and add a split button to it, with a main command definition shown and a couple of other command definitions in the drop down. I've done the list of command definitions as you told me but I am getting the error "Internal validation: res".
This ONLY happens when I'm using the "addSplitButton" method, like so: newPanel.controls.addSplitButton(buttonExample, additionalDefinitions, False)

 

However, if I use the "addCommand" method, it works perfectly fine, for each command definition in the list, like so: newPanel.controls.addCommand(buttonExample)

 

So, I think it is how you said, the Split Button functionality is not supported?


Source Code:

def run(context):

try:

global _app, _ui

_app = adsk.core.Application.get()

_ui = _app.userInterface

 

# Get the UserInterface object and the CommandDefinitions collection.

app = adsk.core.Application.get()

ui = app.userInterface

cmdDefs = ui.commandDefinitions

 

# Create a button command definition.

# Get the existing command definition or create it if it doesn't already exist.

buttonExample = _ui.commandDefinitions.itemById('MyButtonDefId')

if not buttonExample:

buttonExample = cmdDefs.addButtonDefinition('MyButtonDefId', 'Sample Button',

'Sample button tooltip')

 

 

buttonExample2 = _ui.commandDefinitions.itemById('MyButtonDefId2')

if not buttonExample2:

buttonExample2 = cmdDefs.addButtonDefinition('MyButtonDefId2', 'Sample Button2',

'Sample button tooltip2')

 

 

 

buttonExample3 = _ui.commandDefinitions.itemById('MyButtonDefId3')

if not buttonExample3:

buttonExample3 = cmdDefs.addButtonDefinition('MyButtonDefId3', 'Sample Button3',

'Sample button tooltip3')

 

 

# Connect to the command created event.

onCommandCreated = MyCommandCreatedHandler()

buttonExample.commandCreated.add(onCommandCreated)

buttonExample2.commandCreated.add(onCommandCreated)

buttonExample3.commandCreated.add(onCommandCreated)

_handlers.append(onCommandCreated)

 

 

# Get the ADD-INS panel in the model workspace.

addInsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')

 

# Add the button to the bottom.

buttonControl = addInsPanel.controls.addCommand(buttonExample)

 

# Make the button available in the panel.

buttonControl.isPromotedByDefault = True

buttonControl.isPromoted = True

 

 

additionalDefinitions = []

additionalDefinitions.append(buttonExample2)

additionalDefinitions.append(buttonExample3)

ActiveWorkspace = ui.activeWorkspace

WorkspacePanels = ActiveWorkspace.toolbarPanels

newPanel = WorkspacePanels.itemById('UTEPPanel3')

if not newPanel:

newPanel = WorkspacePanels.add('UTEPPanel3', 'UTEP PANEL2')

 

 

#newPanel.controls.addSplitButton(buttonExample, additionalDefinitions, False) #GIVES ME RuntimeError 2: InternalValidationError: res

newPanel.controls.addCommand(buttonExample)

newPanel.controls.addCommand(buttonExample2)

newPanel.controls.addCommand(buttonExample3)

0 Likes

BrianEkins
Mentor
Mentor
Accepted solution

The picture that you showed above of the main toolbar is showing panels, not split buttons.  So, it's a new panel that you need to create and add buttons to it.  That's what the sample code I posted above is doing.  I think it would also be best not to use the active workspace but to find the specific workspace you want to add your new panel too.  Otherwise, it could end up adding your commands to a workspace that doesn't make sense, and I'm not sure that there is an active workspace when add-ins are loaded when Fusion is just starting.  My previous sample code adds a new panel to the MODEL workspace.  To do the same you would do something like:

 

 

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
 
        # Get the UserInterface object and the CommandDefinitions collection.
        _app = adsk.core.Application.get()
        ui = _app.userInterface
        cmdDefs = _ui.commandDefinitions
		 
        # Create the button command definitions if they don't already exist.
        # Get the existing command definition or create it if it doesn't already exist.
        buttonExample = _ui.commandDefinitions.itemById('MyButtonDefId')
        if not buttonExample:
            buttonExample = cmdDefs.addButtonDefinition('MyButtonDefId', 'Sample Button', 'Sample button tooltip')
		 		 
        buttonExample2 = _ui.commandDefinitions.itemById('MyButtonDefId2')
        if not buttonExample2:
            buttonExample2 = cmdDefs.addButtonDefinition('MyButtonDefId2', 'Sample Button2', 'Sample button tooltip2')
		 
        buttonExample3 = _ui.commandDefinitions.itemById('MyButtonDefId3')
        if not buttonExample3:
            buttonExample3 = cmdDefs.addButtonDefinition('MyButtonDefId3', 'Sample Button3', 'Sample button tooltip3')
		 		 
        # Connect to the command created event.
        onCommandCreated = MyCommandCreatedHandler()
        buttonExample.commandCreated.add(onCommandCreated)
        buttonExample2.commandCreated.add(onCommandCreated)
        buttonExample3.commandCreated.add(onCommandCreated)
        handlers.append(onCommandCreated)
		 		 
        # Get the MODEL workspace.
        modelWS = _ui.workspaces.itemById('FusionSolidEnvironment')
        
        # Add a new panel.
        myPanel = modelWS.toolbarPanels.add('myPanel', 'SAMPLE PANEL', 'SolidScriptsAddinsPanel', False)

        # Add the first button to the panel and make it visible in the panel.
        buttonControl = myPanel.controls.addCommand(buttonExample)
        buttonControl.isPromotedByDefault = True
        buttonControl.isPromoted = True
		 
        # Add the other buttons to the panel.
        myPanel.controls.addCommand(buttonExample2)
        myPanel.controls.addCommand(buttonExample3)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
1 Like

Anonymous
Not applicable

So I've created a new panel like you told me, and it is what I was looking for. However, how do you make it so that one of the command definitions shows up as the main command definition in the new panel, like shown:
By hand it is possible, by simply clicking the upwards arrow, but how can I do this in python?
Thanks for helping.

Picture1.jpg

0 Likes

BrianEkins
Mentor
Mentor

That's what these two lines in the code I provided above are doing.

 

buttonControl.isPromotedByDefault = True
buttonControl.isPromoted = True

The isPromoted causes the command to display in the panel.  The isPromotedByDefault will keep it in the panel if the user resets the toolbar customization.

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

ebunn3
Advocate
Advocate

Brian

 

What would the workflow look like to create the panel and buttons?   Currently I load a toolbar button to an existing panel when my addins are loaded.  How would one create the panel when fusion opens?   I would subsequently load my addins into this panel instead of another existing panel.  

Thanks in advance.  

Eric

0 Likes