- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm making a new post to aggregate the findings of a few others and to track/document a specific issue in the api, mainly that mesh cross sections have no accessible object nor type.
How I got here.
I am trying to:
1. Generate a cross section on a mesh given a plane.
2. Apply a fitted spline to the cross section.
A specific example:
I took the commandInput example, and pared it down to just the selection command. Upon selection, it will display the selection entities, counts, and object types into the debug console. See below:
#Author-Autodesk Inc.
#Description-Demo command input examples
import adsk.core, adsk.fusion, traceback
_app = None
_ui = None
_rowNumber = 0
# Global set of event handlers to keep them referenced for the duration of the command
_handlers = []
# Event handler that reacts to any changes the user makes to any of the command inputs.
class MyCommandInputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.InputChangedEventArgs.cast(args)
inputs = eventArgs.inputs
cmdInput = eventArgs.input
# onInputChange for slider controller
if cmdInput.id=='selection':
selection_input = adsk.core.SelectionCommandInput.cast(cmdInput)
print(selection_input.selectionCount)
if selection_input.selectionCount:
for i in range(selection_input.selectionCount):
print(selection_input.selection(i))
s = selection_input.selection(i)
print("Selection info:")
print(f'Entity:{s.entity}')
print(f'ObjectType:{s.objectType}')
print(f'isValid:{s.isValid}')
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler that reacts to when the command is destroyed. This terminates the script.
class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# When the command is done, terminate the script
# This will release all globals which will remove all event handlers
#Print out the selection, then destroy.
adsk.terminate()
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler that reacts when the command definitio is executed which
# results in the command being created and this event being fired.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# Get the command that was created.
cmd = adsk.core.Command.cast(args.command)
# Connect to the command destroyed event.
onDestroy = MyCommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
# Connect to the input changed event.
onInputChanged = MyCommandInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
_handlers.append(onInputChanged)
# Get the CommandInputs collection associated with the command.
inputs = cmd.commandInputs
# Create a tab input.
tabCmdInput1 = inputs.addTabCommandInput('tab_1', 'Tab 1')
tab1ChildInputs = tabCmdInput1.children
# Create a selection input.
selectionInput = tab1ChildInputs.addSelectionInput('selection', 'Select', 'Basic select command input')
selectionInput.setSelectionLimits(0)
self.selectionInput = selectionInput
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def get_selection(self):
try:
return self.selectionInput.selection
except:
return None
def run(context):
try:
global _app, _ui
_app = adsk.core.Application.get()
_ui = _app.userInterface
# Get the existing command definition or create it if it doesn't already exist.
cmdDef = _ui.commandDefinitions.itemById('cmdInputsSample')
if not cmdDef:
cmdDef = _ui.commandDefinitions.addButtonDefinition('cmdInputsSample', 'Command Inputs Sample', 'Sample to demonstrate various command inputs.')
# Connect to the command created event.
onCommandCreated = MyCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
_handlers.append(onCommandCreated)
# Execute the command definition.
cmdDef.execute()
#Grab the mesh object from the selection.
# Prevent this module from being terminated when the script returns, because we are waiting for event handlers to fire.
adsk.autoTerminate(False)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
When selecting standard objects, say a sketch:
the script shows the entity is filled with the relevant sketch object:
However, when selecting a mesh intersection, the Entity field (the object returned by the selection) is empty.
Solved! Go to Solution.