Empty Selection input crashes Add-in

Empty Selection input crashes Add-in

danielWNW3M
Contributor Contributor
482 Views
4 Replies
Message 1 of 5

Empty Selection input crashes Add-in

danielWNW3M
Contributor
Contributor

I'm trying to make an add-in that lets me enter data for one of several geometries. I'm starting with all the necessary inputs created but invisible, then when the user selects which type of geometry they want, I make those inputs visible (and re-hide the others just in case). This works fine.

 

The problem is that if I need to include a selectionInput for some of them, and it seems that any time any of them is left empty it kills my add-in. It dies before even getting to the command_execute function, so I can't put up a messageBox with any of the data to help debugging. I've confirmed it's the selection commands just by adding and removing commands until I get the errors again.

 

I'm not sure why everything is dying, so I'm not really sure how to fix it. First I tried enabling and disabling the inputs along with making them visible, but as they call out in the documentation that doesn't really work for selectionInputs. I also tried using the .deleteMe method to get rid of the ones I don't need, but that just crashes Fusion entirely.

 

Thanks for any help or insight here, I am at a loss at this point.

 

Here's the code that creates the input

 

def command_created(args: adsk.core.CommandCreatedEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Created Event')

    # https://help.autodesk.com/view/fusion360/ENU/?contextId=CommandInputs
    inputs = args.command.commandInputs

    # Create text inputs for the component
    selectionInput0 = inputs.addSelectionInput('component', 'Component', 'Select the component that contains this feature')
    selectionInput0.addSelectionFilter('Occurrences')
    selectionInput0.setSelectionLimits(1)

    # Create a text input for the feature description
    descriptionInput = inputs.addTextBoxCommandInput('description', 'Description', 'xy-plane 1', 1, False)

    # Create dropdown input for feature type selection
    typeInput = inputs.addDropDownCommandInput('type', 'Feature Type', adsk.core.DropDownStyles.TextListDropDownStyle)
    typeList = ['plane', 'well']
    types = typeInput.listItems
    for t in typeList:
        types.add(t, False)

    # Create a selection for a plane surface
    planeInput = inputs.addSelectionInput('plane', 'Plane', 'Select the plane that the feature will be created on')
    planeInput.addSelectionFilter('Faces')
    planeInput.addSelectionFilter('ConstructionPlanes')
    planeInput.addSelectionFilter('Sketches')
    planeInput.isVisible = False

    # Create a selection for points
    pointsInput = inputs.addSelectionInput('points', 'Points', 'Select the points that define the feature')
    pointsInput.addSelectionFilter('SketchPoints')
    pointsInput.addSelectionFilter('ConstructionPoints')
    pointsInput.setSelectionLimits(0)
    pointsInput.isVisible = False

    # Create a selection for well base
    wellInput = inputs.addSelectionInput('wellBase', 'Well Base', 'Select the base of the well')
    wellInput.addSelectionFilter('Faces')
    wellInput.addSelectionFilter('ConstructionPlanes')
    wellInput.addSelectionFilter('Sketches')
    wellInput.isVisible = False

    # Create a value input for well diameter
    diameterInput = inputs.addValueInput('wellDiameter', 'Well Diameter', 'mm', adsk.core.ValueInput.createByReal(0))
    diameterInput.isVisible = False

    # Create a value input for well depth
    depthInput = inputs.addValueInput('wellDepth', 'Well Depth', 'mm', adsk.core.ValueInput.createByReal(0))
    depthInput.isVisible = False
    

    # TODO Connect to the events that are needed by this command.
    futil.add_handler(args.command.execute, command_execute, local_handlers=local_handlers)
    futil.add_handler(args.command.inputChanged, command_input_changed, local_handlers=local_handlers)
    futil.add_handler(args.command.executePreview, command_preview, local_handlers=local_handlers)
    futil.add_handler(args.command.validateInputs, command_validate_input, local_handlers=local_handlers)
    futil.add_handler(args.command.destroy, command_destroy, local_handlers=local_handlers)

 

 

Here's the code that hides the unused commands:

 

def command_input_changed(args: adsk.core.InputChangedEventArgs):
    inputs = args.inputs
    cmdInput = args.input
    type: adsk.core.DropDownCommandInput = inputs.itemById('type')

    if cmdInput.id == 'type':
        if type.selectedItem.name == 'plane':
            inputs.itemById('plane').isVisible = True
            inputs.itemById('points').isVisible = True
            inputs.itemById('wellBase').isVisible = False
            inputs.itemById('wellDiameter').isVisible = False
            inputs.itemById('wellDepth').isVisible = False
        elif type.selectedItem.name == 'well':
            inputs.itemById('plane').isVisible = False
            inputs.itemById('points').isVisible = False
            inputs.itemById('wellBase').isVisible = True
            inputs.itemById('wellDiameter').isVisible = True
            inputs.itemById('wellDepth').isVisible = True

 

 

For now my command execute is very simple while I'm trying to debug this. 

 

def command_execute(args: adsk.core.CommandEventArgs):
    ui.messageBox("Feature Generator Started")
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Execute Event')

 

 

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

BrianEkins
Mentor
Mentor

I have your add-in running, but I don't see a crash. What are the exact steps to cause it to crash?

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 5

danielWNW3M
Contributor
Contributor

Thank you so much for the reply!

For me, at least:

  1. start add-inn
  2. select a component
  3. select a feature type
  4. fill in appropriate data (or not, result is the same)
  5. click okay
  6. nothing happens, the add-in crashes, the ui.messageBox does not appear. 

Here's a quick screen grab

 

0 Likes
Message 4 of 5

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi @danielWNW3M ,

 

Your code stop firing the execute event when any selection command input have a minimum selection limit, as planeInput and wellInput command inputs, and at the moment of the execute event they don't have any selected item.

 

By default a SelectionCommandInput is defined with limits[1,1] (min and max values).

You can check it after you create the command input with:

    app.log(f'{planeInput.getSelectionLimits()=}')

and you'll get:

planeInput.getSelectionLimits()=[True, 1, 1]

 

So, to make your script run just add:

...
    planeInput.setSelectionLimits(0)
...
    wellInput.setSelectionLimits(0)
...

after you create any of these selection inputs.  Or adjust their selection limits as long as you enable/disable them.

 

One suggestion: don't use "type" as a variable name since there is a built-in Python function and you're shading it in the scope where it is defined, as in the 

command_input_changed event function.
 
Regards,
Jorge Jaramillo
 
Message 5 of 5

danielWNW3M
Contributor
Contributor

That did it, thank you so much for the solution! 

 

And I have changed "type" as well... thanks for the heads up on that.