command does not execute twice

command does not execute twice

Anonymous
Not applicable
863 Views
4 Replies
Message 1 of 5

command does not execute twice

Anonymous
Not applicable

Hi All.

I've been trying to create an add-in with a button to execute it like:

 

def run(context):
    try:
        global _app, _ui, _design, _thistree, thisdocsunits
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        product = _app.activeProduct
        _design = adsk.fusion.Design.cast(product)

        thisdocsunits = _design.unitsManager.defaultLengthUnits         
        
        for handler in logging.root.handlers[:]:
            logging.root.removeHandler(handler)
        logging.basicConfig(filename=os.path.join(os.path.expanduser("~"),'urdfgen.log'),level=logging.DEBUG)


        workSpace = _ui.workspaces.itemById('FusionSolidEnvironment')
        tbPanels = workSpace.toolbarPanels
        
        global tbPanel
        tbPanel = tbPanels.itemById('NewPanel')
        if tbPanel:
            tbPanel.deleteMe()
        tbPanel = tbPanels.add('NewPanel', 'New Panel', 'SelectPanel', False)

        # Get the existing command definition or create it if it doesn't already exist.
        addlinkcmdDef = _ui.commandDefinitions.itemById('cmdInputsAddLink')
        if not addlinkcmdDef:
            addlinkcmdDef = _ui.commandDefinitions.addButtonDefinition('cmdInputsAddLink', 'Make URDF', 'My attempt to make an URDF.')

        # Connect to the command created event.
        onCommandCreated = AddLinkCommandCreatedHandler()
        addlinkcmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        # will try to create a button for this guy
        
        tbPanel.controls.addCommand(addlinkcmdDef)  
        _thistree = UrdfTree()
               
        # Execute the command definition.
        #addlinkcmdDef.execute()
        
        
        
        # Prevent this module from being terminated when the script returns, because we are waiting for event handlers to fire.
        adsk.autoTerminate(False)
        
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

I create a tab in the class (and also the handles for execute, inputChanged and destroy):

AddLinkCommandCreatedHandler()
## things...

 and it all works just fine the first time around.
But once I cancel the command and try to execute it a second time, nothing happens, as in, it does not seem to trigger neither execution nor the class.

I wonder If i should have registered more handles, or if I am creating the tab in the wrong context.

 

The whole code is rather lengthy, but in case anyone wants to see it, one may see it here: https://github.com/GummiArmCE/fusion360_scripts/tree/master/urdf_generator

 

Thank you very much for the help,

Frederico.

0 Likes
Accepted solutions (2)
864 Views
4 Replies
Replies (4)
Message 2 of 5

goyals
Autodesk
Autodesk

My guess is that your add-in is getting unloaded after execution of command. Please look at the sample add-in which come as a sample with Fusion. You can look at it by running File->Scripts and Add-ins... command and clicking on "Add-Ins" tab. 



Shyam Goyal
Sr. Software Dev. Manager
0 Likes
Message 3 of 5

goyals
Autodesk
Autodesk
Accepted solution

I looked at your Add-in code. Please try after commenting following lines in AddLinkCommandCreatedHandler class

 

onDestroy = AddLinkCommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)



Shyam Goyal
Sr. Software Dev. Manager
Message 4 of 5

Anonymous
Not applicable

Good catch.

 

Well it seems trivial now if I think about it - if I want the command to be loaded I should not destroy it.
Thank you!

0 Likes
Message 5 of 5

Anonymous
Not applicable
Accepted solution

turn out the problem is the asdk.terminate() call inside the destructor. I can still register a destructor in my oncreate constructor, I just need to avoid the terminate call.

0 Likes