Message 1 of 8
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Calling "document.close" from within the execute handler of commands with user interface will cause Fusion 360 to close unexpectedly. It will not cause any problem if it is called from within the run method or in response to an input change (like pressing an input button)
To reproduce the problem please follow the following steps:
- Run the following code
- Press "Test" button. A new document will be created and after pressing Ok button of a message box that appears, the created document will be closed without any problem
- Press Ok button of the command dialog box. A new document will be created and after pressing Ok button of a message box that appears, Fusion 360 will be closed unexpectedly after a few seconds.
It is worthwile to mention that if we create n new documents, we can close (n - 1) documents safely without any problem.
import adsk.core, adsk.fusion, traceback _app = None _ui = None _handlers = [] class MyCommandExecuteHandler(adsk.core.CommandEventHandler): def __init__(self): super().__init__() def notify(self, args): try: testDocuments() except: _ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) 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 if cmdInput.id == 'test': testDocuments() 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 = MyCommandExecuteHandler() cmd.execute.add(onExecute) _handlers.append(onExecute) onDestroy = MyCommandDestroyHandler() cmd.destroy.add(onDestroy) _handlers.append(onDestroy) onInputChanged = MyCommandInputChangedHandler() cmd.inputChanged.add(onInputChanged) _handlers.append(onInputChanged) inputs = cmd.commandInputs testCmdInput = inputs.addBoolValueInput('test', 'Test', False) except: _ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) def testDocuments(): try: docs = _app.documents newDoc = docs.add(adsk.core.DocumentTypes.FusionDesignDocumentType) _ui.messageBox('"close" method will be called for this document after pressing Ok button') newDoc.close(False) except: if _ui: _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('cmdDocsTest') if not cmdDef: cmdDef = _ui.commandDefinitions.addButtonDefinition('cmdDocsTest', 'Command Docs Test', 'Sample to test Document commands.') 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()))
Website: https://perceptino.com
Solved! Go to Solution.