[API] Simple Add-in Error 'A command definition with this id already exists'

[API] Simple Add-in Error 'A command definition with this id already exists'

Anonymous
Not applicable
955 Views
1 Reply
Message 1 of 2

[API] Simple Add-in Error 'A command definition with this id already exists'

Anonymous
Not applicable

Hi, I am having the following script (which I already run a couple of times before). Now, whenever I run it again a get the error (shown below). I assume there is some small bug in the stop function, but I cannot really find it. If anyone can help, I would very much appreciate it!

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback
_handlers = []

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

        ### SETUP:
        # Create a command definition and add a button to the CREATE panel.
        createHolesCmdDef = ui.commandDefinitions.addButtonDefinition('CreateHolesAddIn', 'Add holes in model to reduce heat', 'Creates holes in the model that help reduce heat', 'resources')        
        # grabbing correct toolbar panel 
        addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        createHolesButton = addinsPanel.controls.addCommand(createHolesCmdDef)
        # adding the button into the Solid Addins Panel 
        createHolesControl =  addinsToolBar.controls.addCommand(createHolesCmdDef, 'createHolesControl')
        # putting this button in the panel instead of dropdown menu 
        createHolesControl.isPromotedByDefault = True
        createHolesControl.isPromoted = True 
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def stop(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('You stopped "CREATE HOLES" addin.')

        createHolesButton = addinsPanel.controls.itemById('CreateHolesAddIn')       
        if createHolesButton:
            createHolesButton.deleteMe()
        
        cmdDef = ui.commandDefinitions.itemById('CreateHolesAddIn')
        if cmdDef:
            cmdDef.deleteMe()

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

#class CreateHolesCommandCreatedHandler(adsk.core.CommandCreatedEventHandler): 

 

svitlana96MQ7_0-1597061164298.png

 

0 Likes
Accepted solutions (1)
956 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
Accepted solution

I figured this out! Here is the code if anyone will ever encounter something similar. 

 

#Author- Svitlana Midianko
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback
_handlers = []

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ### SETUP:
        # Create a command definition and add a button to the CREATE panel.
        createHolesCmdDef = ui.commandDefinitions.addButtonDefinition('CreateHolesAddInPYTHON', 'Add holes in model to reduce heat', 'Creates holes in the model that help reduce heat', 'resources')        
        # grabbing correct toolbar panel 
        addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        # adding the button into the Solid Addins Panel 
        createHolesControl = addinsPanel.controls.addCommand(createHolesCmdDef, 'createHolesControl')
        # putting this button in the panel instead of dropdown menu 
        createHolesControl.isPromotedByDefault = True
        createHolesControl.isPromoted = True 
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def stop(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('You stopped "CREATE HOLES" addin.')

        addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        createHolesControl = addinsPanel.controls.itemById('CreateHolesAddInPYTHON')       

        if createHolesControl:
            ui.messageBox('hi')
            createHolesControl.deleteMe()
        
        cmdDef = ui.commandDefinitions.itemById('CreateHolesAddInPYTHON')
        if cmdDef:
            cmdDef.deleteMe()

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

#class CreateHolesCommandCreatedHandler(adsk.core.CommandCreatedEventHandler): 

 

0 Likes