<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Problem with pre-populating multiple selection inputs in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783162#M7367</link>
    <description>&lt;P&gt;If I understood correcly, you need to remember with what selections the last commands were executed with right ? In that case, why not put the 'memory' step in the command execute handler rather than the input changed handler ?&lt;/P&gt;</description>
    <pubDate>Fri, 26 Nov 2021 08:15:35 GMT</pubDate>
    <dc:creator>tykapl.breuil</dc:creator>
    <dc:date>2021-11-26T08:15:35Z</dc:date>
    <item>
      <title>Problem with pre-populating multiple selection inputs</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10782696#M7366</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have multiple selection inputs for my addin. They are designed to hold different selection filters (body &amp;amp; face) and must be separated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;command = adsk.core.CommandCreatedEventArgs.cast(args).command
            inputs = command.commandInputs
            body_selection = inputs.addSelectionInput('body_selection', 'Select body', 'Click on body to select')
            body_selection.addSelectionFilter('Bodies')
            body_selection.setSelectionLimits(0)
            
            face_selection = inputs.addSelectionInput('face_selection', 'Select face', 'Click on face to select')
            face_selection.addSelectionFilter('Faces')
            face_selection.setSelectionLimits(0)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;The selections made by users have to be remembered and re-applied next time when users use this command again. Therefore, I stored the selections in two global lists, and used the activate event of command to pre-populate the selections.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;global _sel_body, _sel_face
            ipts = adsk.core.CommandEventArgs.cast(args).command.commandInputs
            #Pre-populate body selections if any
            if len(_sel_body) &amp;gt; 0:
                body_selection = ipts.itemById('body_selection')
                for body in _sel_body:
                    body_selection.addSelection(body)
            #Pre-populate face selections if any
            if len(_sel_face) &amp;gt; 0:
                face_selection = ipts.itemById('face_selection')
                for face in _sel_face:
                    face_selection.addSelection(face)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To record the selections, I used inputChanged event to detect changes in the selection inputs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;global _sel_body, _sel_face
            ipt = adsk.core.InputChangedEventArgs.cast(args).input
            if ipt.id == 'body_selection':
                _sel_body = [ipt.selection(s).entity for s in range(ipt.selectionCount)]
            elif ipt.id == 'face_selection':
                _sel_face = [ipt.selection(s).entity for s in range(ipt.selectionCount)]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when I tested my code, it did not work as expected (in the pre-populating step, to be precise). First I selected a body and a face for the two selection inputs respectively. Note that the selected face did not belong to the selected body.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jhan97_0-1637890546814.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/993931iF486C5977DBA18E4/image-size/large?v=v2&amp;amp;px=999" role="button" title="jhan97_0-1637890546814.png" alt="jhan97_0-1637890546814.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;After clicking OK, I opened the command again, expecting the same selection (1 body, 1 face) would be made. But instead, 2 bodies &amp;amp; 1 face were selected.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jhan97_1-1637890705850.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/993932iCA9BE99D40E05604/image-size/large?v=v2&amp;amp;px=999" role="button" title="jhan97_1-1637890705850.png" alt="jhan97_1-1637890705850.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The body of the selected face was selected, although I never selected it. For my understanding, it is because the pre-populating action triggers the inputChanged event.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I avoid this behavior? If anyone wants to reproduce this situation, I have attached the addin &amp;amp; the test model in a zip file. Many thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 01:45:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10782696#M7366</guid>
      <dc:creator>j.han97</dc:creator>
      <dc:date>2021-11-26T01:45:33Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with pre-populating multiple selection inputs</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783162#M7367</link>
      <description>&lt;P&gt;If I understood correcly, you need to remember with what selections the last commands were executed with right ? In that case, why not put the 'memory' step in the command execute handler rather than the input changed handler ?&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 08:15:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783162#M7367</guid>
      <dc:creator>tykapl.breuil</dc:creator>
      <dc:date>2021-11-26T08:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with pre-populating multiple selection inputs</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783163#M7368</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/11275785"&gt;@j.han97&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was able to reproduce the phenomenon.&lt;/P&gt;
&lt;P&gt;I tried de-focusing the body_selection and reselecting the face_selection first, but the result was the same.&lt;/P&gt;
&lt;P&gt;It seems to be a bug, but I'm not sure if the cause is the SelectionCommandInput or the activate event.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 08:16:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783163#M7368</guid>
      <dc:creator>kandennti</dc:creator>
      <dc:date>2021-11-26T08:16:00Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with pre-populating multiple selection inputs</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783269#M7369</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am guessing that the command input is focusing on the first selection input, therefore in the pre-population step, the selected faces are added to the body_selection (which is the first selection input). To verify this, I used only activate &amp;amp; execute handler to store &amp;amp; pre-populate the selections.&lt;/P&gt;&lt;LI-CODE lang="general"&gt;class selections_activateHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    
    def notify(self, args):
        try:
            global _sel_body, _sel_face
            ipts = adsk.core.CommandEventArgs.cast(args).command.commandInputs
            _app.log('\tPrepopulate started')
            if len(_sel_body) &amp;gt; 0:
                body_selection = ipts.itemById('body_selection')
                for body in _sel_body:
                    body_selection.addSelection(body)
            
            if len(_sel_face) &amp;gt; 0:
                face_selection = ipts.itemById('face_selection')
                for face in _sel_face:
                    face_selection.addSelection(face)
                    
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class selections_executeHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    
    def notify(self, args):
        try:
            global _sel_body, _sel_face
            _sel_body = []; _sel_face = []
            
            command = adsk.core.CommandEventArgs.cast(args).command
            inputs = command.commandInputs
            body_selection = inputs.itemById('body_selection')
            for i in range(body_selection.selectionCount):
                _sel_body.append(body_selection.selection(i).entity)
            face_selection = inputs.itemById('face_selection')
            for i in range(face_selection.selectionCount):
                _sel_face.append(face_selection.selection(i).entity)
            
            _ui.messageBox(f'{len(_sel_body)} bodies, {len(_sel_face)} faces')
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;The situation remained the same. Now it seems that the the problem comes from either the activate handler or the selectionCommandInput as suggested by&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3787950"&gt;@kandennti&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;Until this step I am completely confused because this pre-population method is suggested in the API reference manual &lt;EM&gt;(If you want to pre-populate the selection when the command is starting, you can use this method in the activate method of the Command)&lt;/EM&gt;. I am hoping someone from the development team can provide some guidance or confirm that this is a bug (so it won't trouble me anymore!).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/11498985"&gt;@tykapl.breuil&lt;/a&gt;&amp;nbsp;, the real code is slightly more complex than this sample script: I wanted to 'extend' the selection from user, for example, include similar bodies (in terms of volume, mass, etc) when a body is selected. Therefore I had to monitor the new selection and do the calculations in the inputChanged handler. I didn't realize that this is actually kind of stupid here (:D).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you all for your comments!&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 09:06:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783269#M7369</guid>
      <dc:creator>j.han97</dc:creator>
      <dc:date>2021-11-26T09:06:16Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with pre-populating multiple selection inputs</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783298#M7370</link>
      <description>&lt;P&gt;Small update: I thought I found the solution by setting the focus to face_selection before adding selections to it:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;class selections_activateHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    
    def notify(self, args):
        try:
            global _sel_body, _sel_face
            ipts = adsk.core.CommandEventArgs.cast(args).command.commandInputs
            _app.log('\tPrepopulate started')
            if len(_sel_body) &amp;gt; 0:
                body_selection = ipts.itemById('body_selection')
                for body in _sel_body:
                    body_selection.addSelection(body)
            
            
            if len(_sel_face) &amp;gt; 0:  
                face_selection = ipts.itemById('face_selection') 
                #Set focus to face_selection
                face_selection.hasFocus = True
                for face in _sel_face:
                    face_selection.addSelection(face)
            
                    
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But it still failed.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 09:23:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783298#M7370</guid>
      <dc:creator>j.han97</dc:creator>
      <dc:date>2021-11-26T09:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with pre-populating multiple selection inputs</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783307#M7371</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/11275785"&gt;@j.han97&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's just "face_selection", it works correctly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is true that if there are multiple SelectionCommandInputs, setSelectionLimits will not work correctly and the OK button will function even though the condition is not met.&lt;/P&gt;
&lt;P&gt;It's possible that SelectionCommandInput is not designed to be used more than once.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 09:30:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783307#M7371</guid>
      <dc:creator>kandennti</dc:creator>
      <dc:date>2021-11-26T09:30:59Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with pre-populating multiple selection inputs</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783318#M7372</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3787950"&gt;@kandennti&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I see. But since it is not practical for me to mix up the selection inputs, I guess I will have to create separate buttons for users for different selection types.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 09:36:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783318#M7372</guid>
      <dc:creator>j.han97</dc:creator>
      <dc:date>2021-11-26T09:36:29Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with pre-populating multiple selection inputs</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783451#M7373</link>
      <description>&lt;P&gt;This addin only adds a face to the face selection input and still displays the bug :&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#Author-Juan
#Description-Testing for addins

import adsk.core, adsk.fusion, adsk.cam, traceback

class selections_commandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    
    def notify(self, args):
        try:
            product = _app.activeProduct
            design = adsk.fusion.Design.cast(product)
            rootComp = design.rootComponent
            command = adsk.core.CommandCreatedEventArgs.cast(args).command
            inputs = command.commandInputs
            body_selection = inputs.addSelectionInput('body_selection', 'Select body', 'Click on body to select')
            body_selection.addSelectionFilter('Bodies')
            body_selection.setSelectionLimits(0)
            
            face_selection = inputs.addSelectionInput('face_selection', 'Select face', 'Click on face to select')
            face_selection.addSelectionFilter('Faces')
            face_selection.setSelectionLimits(0)

            face_selection = inputs.itemById('face_selection')
            face_selection.hasFocus = True
            face_selection.addSelection(rootComp.bRepBodies.item(0).faces.item(0))

        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


_app = adsk.core.Application.get()
_ui  = _app.userInterface
_handlers = []


def run(context):
    try:
        
        #Get command definitions &amp;amp; design workspace
        cmd_def = _ui.commandDefinitions
        des_wp = _ui.workspaces.itemById('FusionSolidEnvironment')
        product = _app.activeProduct
        design = adsk.fusion.Design.cast(product)

        #Get tools tab
        tools_tab = des_wp.toolbarTabs.itemById('ToolsTab')
        
        #Get add-in panel
        addin_panel = tools_tab.toolbarPanels.itemById('SolidScriptsAddinsPanel')
            
        
        #Add selections command definition
        selections_cmdDef = cmd_def.itemById('selections_cmd')
        if not selections_cmdDef:
            selections_cmdDef = cmd_def.addButtonDefinition('selections_cmd', 'Multiple selections', 'Sample addin to test multiple selections', './resources/selections')
        
        #Add to panel
        selections_ctrl = addin_panel.controls.itemById('selections_cmd')
        if not selections_ctrl:
            selections_ctrl = addin_panel.controls.addCommand(selections_cmdDef)
        
        #Set visibility of button
        selections_ctrl.isVisible = True; selections_ctrl.isPromoted = True; selections_ctrl.isPromotedByDefault = True
        
        #Connect to commandCreatedEvent
        selections_commandCreated = selections_commandCreatedEventHandler()
        selections_cmdDef.commandCreated.add(selections_commandCreated)
        _handlers.append(selections_commandCreated)
        
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def stop(context):
    try:
        pass
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 26 Nov 2021 10:49:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/problem-with-pre-populating-multiple-selection-inputs/m-p/10783451#M7373</guid>
      <dc:creator>tykapl.breuil</dc:creator>
      <dc:date>2021-11-26T10:49:36Z</dc:date>
    </item>
  </channel>
</rss>

