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.
Showing results for 
Show  only  | Search instead for 
Did you mean: 

UserParameters.add causing SelectionCommandInput.selection to be empty

Message 1 of 4
228 Views, 3 Replies

UserParameters.add causing SelectionCommandInput.selection to be empty

I created a command script that will generate multiple offsets from a single-loop Profile.

The command inputs are:

  1. A SelectionInput filtered for 1 Profile
  2. An IntegerSpinnerCommandInput to specify the number of sketch curves to generate.
  3. A DistanceValueCommandInput to specify the total offset distance to span.

I decided to add a userParameter for the totalOffsetDistance so that I could make adjustments. Adding this line of code..


design.userParameters.add('totalOffsetDistance', ValueInput.createByReal(totalOffsetDistance), "cm", "")


to the execute handler gives two different behaviors based on whether it is before or after this (assuming there is exactly 1 valid Profile selected).


profileSelectionCommand:SelectionCommandInput = inputs.itemById('profileSelection')
profileSelection:Selection = profileSelectionCommand.selection(0)


If the UserParameter is added before the selection is gotten, it we get an invalid index and we can test to see that the selectionCount is indeed 0. However, if we move that line afterwards, we are able to retrieve the selected Profile just as normal. It's not hard to work around but it is undocumented/unregulated and perhaps unintended behavior from a software engineering perspective.

Message 2 of 4

Interesting, nice discovery 

Message 3 of 4
in reply to: mcd8604

Selections are somewhat fragile and it's by design that when you perform an operation that makes a change to persistent data that the current selections are cleared.  To work around this you can get the entities currently selected and save them in a variable and then do your work using these saved references.

Brian Ekins
Inventor and Fusion 360 API Expert
Message 4 of 4
in reply to: BrianEkins

I see. Thanks for the clarification! I ended up creating a utility method to make this easier:


def GetSelections(sci:SelectionCommandInput):
    return [sci.selection(i) for i in range(sci.selectionCount)]


I just have to make sure to remember to cache references prior to making any changes to persistent data.

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