Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

command does not execute twice

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
frederico.klein
541 Views, 4 Replies

command does not execute twice

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.

4 REPLIES 4
Message 2 of 5
goyals
in reply to: frederico.klein

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
Message 3 of 5
goyals
in reply to: goyals

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
frederico.klein
in reply to: goyals

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!

Message 5 of 5

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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report