Note: this article has an update
If you want to write a program that drives the model then you can do that through driving parameters. I have the following simple model with Revolute Joints. These types of joints also have an Alignment Angle parameter which we can modify programmatically as well :)
I wanted to drive the model through the arrow buttons on the keyboard. Fusion API provides a keyDown event through the Command object. So in order to use it we need to define a command and run it. Since we are modifying the model before the user clicks "OK" on the command dialog, therefore we also need to listen to the executePreview event of the Command. There we can set the isValidResult to True. This way if the user clicks "OK" then the changes will be kept without us having to redo the changes inside the execute event handler. It's like so because each command automatically starts a transaction which gets undone by default right before the program creates the final model changes inside the execute event handler.
See the Python code below:
#Author- #Description- import adsk.core, adsk.fusion, adsk.cam, traceback # Global variable used to maintain a reference to all event handlers. handlers = [] # Other global variables commandName = "MoveRobot" app = adsk.core.Application.get() if app: ui = app.userInterface # Event handler for the keyDown event. class MyKeyDownHandler(adsk.core.KeyboardEventHandler): def __init__(self): super().__init__() def notify(self, args): try: eventArgs = adsk.core.KeyboardEventArgs.cast(args) keyCode = eventArgs.keyCode paramName = "" diffVal = 0 if keyCode == adsk.core.KeyCodes.DownKeyCode: paramName = "d41" diffVal = -0.1 elif keyCode == adsk.core.KeyCodes.UpKeyCode: paramName = "d41" diffVal = 0.1 elif keyCode == adsk.core.KeyCodes.LeftKeyCode: paramName = "d59" diffVal = -0.1 elif keyCode == adsk.core.KeyCodes.RightKeyCode: paramName = "d59" diffVal = 0.1 design = app.activeProduct params = design.allParameters param = params.itemByName(paramName) newVal = param.value + diffVal param.value = newVal adsk.doEvents() except: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) # Event handler for the executePreview event. class MyExecutePreviewHandler(adsk.core.CommandEventHandler): def __init__(self): super().__init__() def notify(self, args): eventArgs = adsk.core.CommandEventArgs.cast(args) # Make it accept the changes whatever happens eventArgs.isValidResult = True class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler): def __init__(self): super().__init__() def notify(self, args): try: command = adsk.core.Command.cast(args.command) onExecutePreview = MyExecutePreviewHandler() command.executePreview.add(onExecutePreview) handlers.append(onExecutePreview) onKeyDown = MyKeyDownHandler() command.keyDown.add(onKeyDown) handlers.append(onKeyDown) onDestroy = MyCommandDestroyHandler() command.destroy.add(onDestroy) handlers.append(onDestroy) inputs = command.commandInputs inputs.addTextBoxCommandInput( commandName + '_textBox', 'Usage:', 'Use the arrow buttons to drive the robot arm', 2, True); except: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) class MyCommandDestroyHandler(adsk.core.CommandEventHandler): def __init__(self): super().__init__() def notify(self, args): try: commandDefinitions = ui.commandDefinitions # Check the command exists or not cmdDef = commandDefinitions.itemById(commandName) if cmdDef: cmdDef.deleteMe # When the command is done, terminate the script # this will release all globals which will remove all event handlers adsk.terminate() except: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) def run(context): try: product = app.activeProduct design = adsk.fusion.Design.cast(product) if not design: ui.messageBox('It is not supported in current workspace, please change to MODEL workspace and try again.') return commandDefinitions = ui.commandDefinitions # Check the command exists or not cmdDef = commandDefinitions.itemById(commandName) if not cmdDef: cmdDef = commandDefinitions.addButtonDefinition( commandName, commandName, commandName, '') onCommandCreated = MyCommandCreatedHandler() cmdDef.commandCreated.add(onCommandCreated) # Keep the handler referenced beyond this function handlers.append(onCommandCreated) inputs = adsk.core.NamedValues.create() cmdDef.execute(inputs) # Prevent this module from being terminated when the script returns, # because we are waiting for event handlers to fire adsk.autoTerminate(False) except: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Here is the script in action:
-Adam