I'm trying to call Command.doExecute() in one of my add-ins, but it makes Fusion crash.
Here is a small example created from the Fusion 360 examples.
I want to use doExecute() instead of just auto-executing, since doExecute() is blocking and can return a value.
#Author-
#Description-
import adsk.core, adsk.fusion, adsk.cam, traceback
_handlers = []
_ui = None
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)
onExecute = MyCommandExecuteHandler()
cmd.execute.add(onExecute)
_handlers.append(onExecute)
cmd.doExecute(True)
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# Get the command that was created.
cmd = adsk.core.Command.cast(args.command)
_ui.messageBox('Execute!')
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
global _ui
_ui = ui
#ui.messageBox('Hello addin')
# 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()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def stop(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('Stop addin')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Solved! Go to Solution.
Solved by BrianEkins. Go to Solution.
Hi @thomasa88 .
I don't understand the purpose, but I interpret that the doExecute Method cannot be used because the command is not created until the commandCreated Event ends.
I don't think it's a bug.
Hi @kandennti ,
I perform multiple updates in the document and want to group them as one Undo action and I also want to check the result.
The way I have found to achieve this is using a Command, so I create the command definition and execute it to create the command. I know that Command.isAutoExecute=True will execute my command without doing doExecute(), but I want the blocking functionality that doExecute() provides. I know that I can build an asynchronous solution with states, but the synchronous functionality would be preferable in this case.
What you are saying sounds sound, so I guess the way to use doExecute() is to store the Command away in a global variable the CommandCreatedEventHandler. The question is, will the Command be valid if I call upon it later? I thought it was transient and will disappear when another command runs. Also, I assume it can only be executed once?
An API sample for doExecute() would really have been helpful.
edit. I just tried storing the command away to run doExecute() from an event handler. The result was that I could not click/select on anything in Fusion, after the command created handler had run.
It would be nice to have a sample that demonstrates this functionality. It was designed to support a command that doesn't have a dialog. When a typical command is run, it displayed a dialog and completes its task when the user clicks the OK button. The lifetime of the command is the same as the lifetime of the dialog.
In command terminology when the user clicks a button to run a command, Fusion creates the command. As part of that the command dialog is defined and displayed. While the command is running, Fusion automatically fires the command's executePreview event when any of the command dialog inputs change. In the executePreview event the add-in can do whatever it needs to do in Fusion to show a representation of the finished state of the command. This happens over and over as the user changes inputs. When the user clicks OK on the command dialog, the command is executed, which fires the execute event and the add-in does whatever the final result of the command is supposed to be. The command is finished.
Before the isAutoExecute property existed, commands always auto executed. What this meant is that a command is created and no inputs are added to the dialog, which means the dialog is not defined. Because there isn't a dialog to display, as soon as the command finishes the creation process it automatically executes and finishes. This is fine in a lot of cases where you don't need any user interaction but want all of the work that the command does to be grouped together within a single undo.
When you don't want to auto execute is when you don't want a standard command dialog but you still need to gather input from the user while the command is running. You might use some other method of interacting with the user, like a palette. In this case, the command is created, not dialog inputs are created, but the isAutoExecute property is set to false. Now the command finishes creation but continues to run. The idea is that you gather whatever data is needed by the command from some other source. If you need to show the user a possible result using the current inputs you can call the doExecutePreview method. This results in Fusion firing the executePreview event to tell the add-in to create the preview result. Once you gathered all of the desired input and are ready to finish the command, you call the doExecute method. This results in Fusion firing the execute event where the add-in creats the final result and command ends.
Can't find what you're looking for? Ask the community or share your knowledge.