@paulgeeser -San.
Thank you for publishing this.
There does not seem to be a problem with the process, but I can reproduce the crash when I run it.
I have tried other events and text commands and it still crashes.
The only thing that didn't crash, although it doesn't help, is the following condition

With two unsaved documents, the second one is active.
The command_execute function is set as follows
def command_execute(args: adsk.core.CommandEventArgs):
futil.log(f'{CMD_NAME} Command Execute Event')
expDoc: adsk.fusion.FusionDocument = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
expDesign = expDoc.design
expDesign.designType = adsk.fusion.DesignTypes.DirectDesignType
# time.sleep(1) # do something
tempDoc = futil.app.documents[0]
tempDoc.activate()
expDoc.close(False)
Before closing the new document, we activate the one other than the document that was active when the command was executed and then close it.
Only in this case it did not crash.
Also, I have created a sample that does not use the add-in template. (attached).
import traceback
import adsk
import adsk.core as core
import adsk.fusion as fusion
_handlers = []
CMD_INFO = {
'id': 'kantoku_test',
'name': 'test',
'tooltip': 'test'
}
class MyCommandCreatedHandler(core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: core.CommandCreatedEventArgs):
try:
global _handlers
cmd: core.Command = core.Command.cast(args.command)
onDestroy = MyCommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
onExecute = MyExecuteHandler()
cmd.execute.add(onExecute)
_handlers.append(onExecute)
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyExecuteHandler(core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: core.CommandEventArgs):
app: core.Application = core.Application.get()
doc: fusion.FusionDocument = app.documents.add(
core.DocumentTypes.FusionDesignDocumentType
)
app.log(f"documents.count:{app.documents.count}")
app.log(f"doc.name:{doc.name}")
doc.close(False)
app.log(f"documents.count:{app.documents.count}")
class MyCommandDestroyHandler(core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: core.CommandEventArgs):
adsk.terminate()
def run(context):
try:
global _app, _ui
_app = core.Application.get()
_ui = _app.userInterface
cmdDef: core.CommandDefinition = _ui.commandDefinitions.itemById(
CMD_INFO['id']
)
if not cmdDef:
cmdDef = _ui.commandDefinitions.addButtonDefinition(
CMD_INFO['id'],
CMD_INFO['name'],
CMD_INFO['tooltip']
)
global _handlers
onCommandCreated = MyCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
_handlers.append(onCommandCreated)
cmdDef.execute()
adsk.autoTerminate(False)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
The execute event simply creates a new document and closes it.
In this case, it does not crash.
I think there is a cause of the crash somewhere in the add-in template, but I could not identify the cause.