Improved Garbage Collection For Commands

Improved Garbage Collection For Commands

therealsamchaney
Advocate Advocate
697 Views
4 Replies
Message 1 of 5

Improved Garbage Collection For Commands

therealsamchaney
Advocate
Advocate

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

Accepted solutions (1)
698 Views
4 Replies
Replies (4)
Message 2 of 5

j.han97
Advocate
Advocate

I remember a isNative property for command definition. Could it be used instead of manually adding the definitions to the list?

Message 3 of 5

therealsamchaney
Advocate
Advocate

Ooh if that does what it seems like it then yes that would be much better. I will look into it. Thanks!

Also, I'm looking more into the deleteMe method and seeing that it only works for API created command definitions. So, if you wanted to clean up all API-created definitions, you don't even need to use an if statement, you can just run deleteMe on all of them 😆  Of course that will delete ones created by other add-ins so that isn't exactly what I want to do here. I only want to clean up the definitions and controls that I created within the scope of this specific add-in.

0 Likes
Message 4 of 5

therealsamchaney
Advocate
Advocate

OK so looking into the isNative method, it seems like that would work for cleaning up all API-created command definitions / controls, however, it's not exactly what I was going for here, which was only to clean up the ones I created within the scope of this specific add-in

0 Likes
Message 5 of 5

therealsamchaney
Advocate
Advocate
Accepted solution

After digging more into the add-ins tutorials, it seems that Autodesk has updated the default auto-generated architecture for new add-ins, which takes care of all garbage collection automatically by separating the code into different files and folders. Now the python file in the main directory just starts the commands in the commands folder in the run function and stops all of them in the stop function, so now we are not supposed to touch this file at all, and instead only edit the files in the commands folders. While this makes things a tiny bit more complicated, I think overall it does make the process cleaner, more elegant and more intuitive.

The tutorial for this new arrangement is below:
https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-DF32F126-366B-45C0-88B0-CEB46F5A9BE8