Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Unexpected behavior with ui.activeSelections

rmiglioriEKDK2
Contributor

Unexpected behavior with ui.activeSelections

rmiglioriEKDK2
Contributor
Contributor

Hi,

I'm trying to write a script that does essentially the following: 

 

  • Find bodies inside components.
  • Select them.
  • Create a selection set from the selected bodies. 
  • Deselect them
  • Select other bodies.
  • Create a selection set from these selected bodies.
  • Deselect them.
  • Repeat 

I've managed to write a script that successfully finds bodies inside a design, select them, and then is supposed to create a selection set from them. However, when I run

ui.activeSelections.clear()

to clear the selections, the selection set created by the script is empty. 

 

I don't really get it, and I'm kind of at a loss as to what to do. Here's the script: 

 

#Author-
#Description-

# https://forums.autodesk.com/t5/fusion-360-api-and-scripts/api-for-working-with-selection-sets/td-p/8837150

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

# Produces empty selection set when it should produce a selection set containing selected bodies then clear the selection set. 
def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        design = app.activeProduct
        rootComponent = design.rootComponent        
        
        # Add all bodies in components to acrtive selections 
        allComponents = rootComponent.allOccurrences        
        for component in allComponents:  
        	componentName = component.name.split(':')[0]
        	bRepBodies = component.bRepBodies
        	for body in bRepBodies:
        		ui.activeSelections.add(body)

        # Create selection set 
        cmd = ui.commandDefinitions.itemById('CreateSelectionGroupCmd')
        cmd.execute()      
        # Clear selections 
        ui.activeSelections.clear()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

I made a screencast of the script creating an empty selection set. Here's a direct link in case it doesn't work: https://autode.sk/36elSyz

 

And I've also attached the test design illustrating the problem. 

 

Thanks for the help, and I'm happy to answer any questions! 

 

 

0 Likes
Reply
765 Views
3 Replies
Replies (3)

kandennti
Mentor
Mentor

Hi rmiglioriEKDK2.

 

At the end of the command, I felt that the selection set was completed, so I created the following code.
However, only an empty selection set was made.
Since it is not provided by API, it seems impossible.

_app = None
_ui  = None
_handlers = []

class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            cmd = _ui.commandDefinitions.itemById('CreateSelectionGroupCmd')
            cmd.execute()
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            adsk.terminate()
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            cmd = adsk.core.Command.cast(args.command)

            onExecute = MyExecuteHandler()
            cmd.execute.add(onExecute)
            _handlers.append(onExecute)

            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        cmdDef = _ui.commandDefinitions.itemById('test')
        if cmdDef:
            cmdDef.deleteMe()
        cmdDef = _ui.commandDefinitions.addButtonDefinition('test', 'test', 'test')

        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        des = _app.activeProduct
        root = des.rootComponent
    
        occs = root.allOccurrences

        for occ in occs:
            bRepBodies = occ.bRepBodies
            for body in bRepBodies:
                _ui.activeSelections.add(body)

            cmdDef.execute()
            _ui.activeSelections.clear()
except: if _ui: _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes

rmiglioriEKDK2
Contributor
Contributor

So even your attempt creates an empty selection set? Wow. I really wonder what is going on that clearing active selections executes with a higher priority than anything else. 

 

Sorry to take this in another direction, but I have another question. How could I run one script from another script? Is that possible to do ? 

0 Likes

kandennti
Mentor
Mentor

I'm sorry to bring up the old story.


With Ver2.0.8335, I found that this method can be used for processing.

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

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        des  :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        txtCmd :str = 'NuComponents.CreateSelectionGroupCmd'
        actSels :adsk.core.Selections = _ui.activeSelections
        for occ in root.allOccurrences:
            bRepBodies = occ.bRepBodies
            actSels.clear()
            [actSels.add(body) for body in bRepBodies]
            _app.executeTextCommand(txtCmd)

        actSels.clear()
        _ui.messageBox('done')
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

0 Likes