- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've been digging more into Fusion 360 API and when I was looking at the bare minimum command example, I was irked by the one-by-one garbage collection in the stop function at the end. It just seemed inelegant and like a lot of manual work if one were to add more commands in the future. So, I created a little pythonic way to walk through all existing command definitions and controls and check whether they are a in the lists of our custom created ones, and delete them if they are. Yes I know looping through every command in the program isn't optimized for efficiency but the whole process is imperceptibly fast.
Anyway here's the code:
def stop(context):
try:
app = adsk.core.Application.get()
ui = app.userInterface
# Check every command definition and delete any of our custom command definitions from it
for definition in ui.commandDefinitions:
if definition in definitions:
# ui.messageBox(f"command {definition} was found in definitions")
definition.deleteMe()
# Check every command control and delete any of our custom controls from it
for panel in ui.allToolbarPanels: # Look through all panels
for control in panel.controls: # Look through all the controls in each panel
if control in controls:
# If one of the controls is in the list of controls, delete it
# ui.messageBox(f"control {control} was found in controls")
control.deleteMe()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
The only thing you have to do manually is remember to add all of your command controls to the controls list and add all of your custom command definitions to the definitions
Solved! Go to Solution.