Reply
Highlighted
Contributor
23 Posts

Can't get my script to run! No dialog box -_-

43 Views, 5 Replies
Thursday

Hi all,

 

Let me begin by saying I am new to coding with Python (about 3 weeks experience), but am quickly learning how to navigate through the API and execute some tasks. I have been following these videos:   click here and learning fast.

 

BUT I have run into a problem. I am trying to create a little dialogue box within fusion when my script runs. I have defined an event and a handler and all that and still nothing comes up. Here is a breakdown of my code...

 

1. Random extrusions at start up are created

2. Created a command definition for a button and placed that button inside the toolbar

3. Created an event and handler for the button

4. Created command inputs (selection inputs and then a dropdown)

4. When i click the button in the toolbar.... nothing happens Smiley Mad

 

I am looking to create a dialogue box which I can then link to  drawing functions. But I can't even get as far as creating a dialogue.

 

 

Any help is appreciated.

 

 

CODE:

#Author-
#Description-


import adsk.core, adsk.fusion, adsk.cam, traceback


def run(context):
   
    try:
        
        app = adsk.core.Application.get()
        ui = app.userInterface
        
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        
        design = app.activeProduct

        handlers = []
        # Get the root component of the active design.
        rootComp = design.rootComponent

        # Create a new sketch on the xy plane.
        sketches = rootComp.sketches
        xyPlane = rootComp.xYConstructionPlane
        sketch1 = sketches.add(xyPlane)

    
        Load_circles = sketch1.sketchCurves.sketchCircles                                                               #load circle API
        circle1 = Load_circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), 1.5)                                #define the circle and draw it
        
        #get profile of sketch
        profile1 = sketch1.profiles.item(0)
        
                                                                                                                 
        extrusion1 = rootComp.features.extrudeFeatures                                                                  #define the extrusion
        extrude1_input = extrusion1.createInput(profile1,adsk.fusion.FeatureOperations.NewComponentFeatureOperation)    #load the extrusion input library
                                                                                                                        
        extrude1_input.setDistanceExtent(False, adsk.core.ValueInput.createByReal(4))                                   #Set parameters for the extrusion input
        FinishExtrude1 = extrusion1.add(extrude1_input)                                                                 #execute the extrusion                                                          
 
 
 
        sketch2 = sketches.add(xyPlane)                                                                                     #create sketch 2
        Load_lines = sketch2.sketchCurves.sketchLines                                                                       #load the line API
        Line1 = Load_lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(2, 0, 0))
        Line2 = Load_lines.addByTwoPoints(adsk.core.Point3D.create(2,0,0), adsk.core.Point3D.create(2,2,0))
        Line3 = Load_lines.addByTwoPoints(adsk.core.Point3D.create(2,2,0), adsk.core.Point3D.create(0,2,0))                 #draw lines (start, end)
        Line4 = Load_lines.addByTwoPoints(adsk.core.Point3D.create(0,2,0), adsk.core.Point3D.create(.5,1,0))
        Line5 = Load_lines.addByTwoPoints(adsk.core.Point3D.create(.5,1,0), adsk.core.Point3D.create(0,0,0))
        
        
        profile2 = sketch2.profiles.item(0)                          #grab the profile of sketch 2
        extrusion2 = rootComp.features.extrudeFeatures                                                                  #define the extrusion
        extrude2_input = extrusion2.createInput(profile2,adsk.fusion.FeatureOperations.NewComponentFeatureOperation)    #load the extrusion input library
       
        extrude2_input.setDistanceExtent(False, adsk.core.ValueInput.createByReal(4))                                   #Set parameters for the extrusion input
        FinishExtrude2 = extrusion2.add(extrude2_input)     

     
      
                                                                                           #adding to handler
       
        class addin_menu_button_pressedEventHandler(adsk.core.CommandCreatedEventHandler):
            def __init__(self):
                
                super().__init__()
            
            def notify(self,args):
                
                try:
                    
                    app = adsk.core.Application()
                    ui = app.userInterface
                    cmd = args.commandInputs
                    inputs = cmd.commandinputs
                    handlers = []
                    
                    input1 = inputs.addSelectionInput(Selection1, "Face: ", "Select a Face") 
                                       
                    input2 = inputs.addDropDownCommandInput(Dropdown1,Dropdown, 1)
                
                    on_execute = addin_menu_button_pressedEventHandler()
                    cmd.execute.add(on_execute)
                    handlers.append(on_execute)
                        
                    
                    
                       
                except:
                    if ui:
                        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
               
                   

        Load_command = ui.commandDefinitions                                                                                            #load command defs
        
        definition_1 = Load_command.addButtonDefinition("identifier","BMScript","BOb First Addin for Fusion360!","Resources") #button definition
       
        addin_location = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')                                                        #button location
        
        addin_menu_button = addin_location.controls.addCommand(definition_1,"Script")                                                   #place the button
        addin_menu_button.isPromotedByDefault = True                                                                                    #premoted = on top of toolbar
        addin_menu_button.isPromoted = True
        
        #handler:
        addin_menu_button_pressed = addin_menu_button_pressedEventHandler()                                                                    #make var for class
        definition_1.commandCreated.add(addin_menu_button_pressed)                                                                     #
        handlers.append(addin_menu_button_pressed)              

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

 

 

Regards,

X

Contributor
23 Posts

Re: Can't get my script to run! No dialog box -_-

Friday in reply to: bmiller.pct

Bumpppp

Autodesk Support
1259 Posts
353 Kudos
369 Solutions

Re: Can't get my script to run! No dialog box -_-

Friday in reply to: bmiller.pct

The API isn't designed to do what you're trying to do within a script.  Typically, when a script is executed, it does what it's supposed to do and then is done.  This means its no longer around to handle the button click. That's why when you click the button nothing happens because the script isn't running any more to listen for the click.

 

Customization of the user-interface should be done in an add-in.  An add-in differs from a script in that it is started and doesn't stop until Fusion is shut down or the user explicitly stops it using the Scripts and Add-Ins command.  When the add-in is started (which is typically when Fusion starts if the add-in is set to run on startup) it adds its commands to the user-interface and connects t the events it needs to listen for.  Then is continues to run in the background waiting for one of the events to occur.  If one of its buttons is clicked, then the command created event is fired and it can create the dialog and connect to other command related events and finally react the command execute event where the work that command does should be performed.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Contributor
23 Posts

Re: Can't get my script to run! No dialog box -_-

Friday in reply to: ekinsb

ekinsb,

 

Now I feel stupid! I thought that may have been the case but I wasn't exactly sure. None of the sources I read really defined that clearly. Is transferring my current script to an Add-in something that is difficult to do? Or does it just require a few commands? 

 

Thank you very much!

Autodesk Support
1259 Posts
353 Kudos
369 Solutions

Re: Can't get my script to run! No dialog box -_-

Friday in reply to: bmiller.pct

It shouldn't be too difficult.  I would create a new add-in and copy and paste pieces of your script code.  Hopefully this topic on Commands will help, but I'd be interested to hear where it didn't and you still had problems.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Contributor
23 Posts

Re: Can't get my script to run! No dialog box -_-

Friday in reply to: ekinsb

ekinsb,

 

So, i created a new Add-in. The only real difference in code that I see is the stop function. I pasted my code.

 

The result is that I now have no icon at all in the toolbar and nothing happens when the addin* runs.

 

Very confused! 

 

I will look at the documentation you provided, thank you

 

Code:

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        
        Load_command = ui.commandDefinitions                                                                                            #load command defs
        
        definition_1 = Load_command.addButtonDefinition("identifier","My Addin","Bobs First Addin for Fusion360!","Resources") #button definition
       
        addin_location = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')                                                        #button location
        
        addin_menu_button = addin_location.controls.addCommand(definition_1,"Script")                                                   #place the button
        addin_menu_button.isPromotedByDefault = True                                                                                    #premoted = on top of toolbar
        addin_menu_button.isPromoted = True
        
        
        
    
        class addin_menu_button_pressedEventHandler(adsk.core.CommandCreatedEventHandler):
            def __init__(self):
                
                super().__init__()
            
            def notify(self,args):
                
                try:
                    
                    app = adsk.core.Application()
                    ui = app.userInterface
                    cmd = args.commandInputs
                    inputs = cmd.commandinputs
                    handlers = []
                    
                    input1 = inputs.addSelectionInput(Selection1, "Face: ", "Select a Face") 
                                       
                    input2 = inputs.addDropDownCommandInput(Dropdown1,Dropdown, 1)
                
                    on_execute = addin_menu_button_pressedEventHandler()
                    cmd.execute.add(on_execute)
                    handlers.append(on_execute)
                        
                    
                    
                       
                except:
                    if ui:
                        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
               
        
        #handler:
        addin_menu_button_pressed = addin_menu_button_pressedEventHandler()                                                                    #make var for class
        definition_1.commandCreated.add(addin_menu_button_pressed)                                                                     #
        handlers.append(addin_menu_button_pressed)        


    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('Stop addin')

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