Hi srijal97 , This is the code I used to manage the joints of my model.
You have to adapt it to your needs.
Please if it is useful for you and if you think to use it, mention me in your code.
Many thanks
Dino
import adsk.core, adsk.fusion, adsk.cam, traceback
# Global list to keep all event handlers in scope.
# This is only needed with Python.
handlers = []
commandId = 'CommandInputTest'
xTarget = None
yTarget = None
zTarget = None
xOrigin = None
yOrigin = None
zOrigin = None
xRelPos = None
yRelPos = None
zRelPos = None
occOrigin = None
occTarget = None
ui = None
degree = None
def relativemeasure():
global occOrigin
global occTarget
global xOrigin
global yOrigin
global zOrigin
global xTarget
global yTarget
global zTarget
global xRelPos
global yRelPos
global zRelPos
try:
app = adsk.core.Application.get()
# Examine the current document
ui = app.userInterface
design = adsk.fusion.Design.cast(app.activeProduct)
# Get the root component of the active design.
rootComp = design.rootComponent
occurrences = rootComp.occurrences
countOccurrences = occurrences.count
for i in range(0, countOccurrences):
if occurrences.item(i).name == 'Target:1':
found=i
break
occTarget = rootComp.allOccurrences.item(found)
# trova le ccordinate dell'origine del componente Target tramite l'item
trans = occTarget.transform
xTarget = trans.translation.x
yTarget = trans.translation.y
zTarget = trans.translation.z
for i in range(0, countOccurrences):
if occurrences.item(i).name == 'Origin:1':
found=i
break
occOrigin = rootComp.allOccurrences.item(found)
# trova le ccordinate dell'origine del componente Origin tramite l'item
trans = occOrigin.transform
xOrigin = trans.translation.x
yOrigin = trans.translation.y
zOrigin = trans.translation.z
# calcola la posizione relativa di Target rispetto ad Origin
xRelPos = xTarget-xOrigin
yRelPos = yTarget-yOrigin
zRelPos = zTarget-zOrigin
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
try:
app = adsk.core.Application.get()
ui = app.userInterface
# Get the CommandDefinitions collection.
cmdDefs = ui.commandDefinitions
# Create a button command definition.
buttonSample = cmdDefs.addButtonDefinition('SliderJoint',
'Drive joints with sliders',
'Drive Joints with sliders',
'./Resources/SliderJoint')
# Connect to the command created event.
sampleCommandCreated = SampleCommandCreatedEventHandler()
buttonSample.commandCreated.add(sampleCommandCreated)
handlers.append(sampleCommandCreated)
# Get the ADD-INS panel in the model workspace.
addInsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
# Add the button to the bottom of the panel.
buttonControl = addInsPanel.controls.addCommand(buttonSample)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler for the commandCreated event.
class SampleCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
global degree
global degree1
global degree2
global degree3
global degree4
global commandId
global inputs
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
cmd = eventArgs.command
# Get the CommandInputs collection to create new command inputs.
inputs = cmd.commandInputs
# relativemeasure()
# Create readonly textbox input
title='Slider Joint'
inputs.addTextBoxCommandInput(commandId + '_textBox', title ,'', 1, True)
# Create Slider
# app = adsk.core.Application.get()
# des = adsk.fusion.Design.cast(app.activeProduct)
# Create integer slider input with one slider
degree = inputs.addIntegerSliderCommandInput(commandId + '_intSlider', 'Waist Slider', -150, 150);
degree1 = inputs.addIntegerSliderCommandInput(commandId + '_intSlider1', 'Shoulder Slider', -90, 30);
degree2 = inputs.addIntegerSliderCommandInput(commandId + '_intSlider2', 'Elbow Slider', -100, 110);
degree3 = inputs.addIntegerSliderCommandInput(commandId + '_intSlider3', 'Wrist Pitch Slider', -90, 90);
degree4 = inputs.addIntegerSliderCommandInput(commandId + '_intSlider4', 'Wrist Roll Slider', -179, 179);
# Connect to the execute event.
onExecute = SampleCommandExecuteHandler()
cmd.execute.add(onExecute)
handlers.append(onExecute)
# cmd.isRepeatable = False
onExecutePreview = SampleCommandExecuteHandler()
cmd.executePreview.add(onExecutePreview)
handlers.append(onExecutePreview)
# Event handler for the execute event.
class SampleCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
global degree
global degree1
global degree2
global degree3
global degree4
global inputs
eventArgs = adsk.core.CommandEventArgs.cast(args)
# Get the values from the command inputs.
inputs = eventArgs.command.commandInputs
# Code to react to the event.
app = adsk.core.Application.get()
ui = app.userInterface
# ui.messageBox('In command execute event handler: degree= '+str(degree))
root = app.activeProduct.rootComponent
degree = inputs.itemById(commandId+'_intSlider').valueOne
degree1 = inputs.itemById(commandId+'_intSlider1').valueOne
degree2 = inputs.itemById(commandId+'_intSlider2').valueOne
degree3 = inputs.itemById(commandId+'_intSlider3').valueOne
degree4 = inputs.itemById(commandId+'_intSlider4').valueOne
joint = root.joints[0]
revoluteMotion = adsk.fusion.RevoluteJointMotion.cast(joint.jointMotion)
revoluteMotion.rotationValue = 3.1415926 * 2 * degree / 360
joint1 = root.joints[1]
revoluteMotion = adsk.fusion.RevoluteJointMotion.cast(joint1.jointMotion)
revoluteMotion.rotationValue = 3.1415926 * 2 * degree1 / 360
joint2 = root.joints[2]
revoluteMotion = adsk.fusion.RevoluteJointMotion.cast(joint2.jointMotion)
revoluteMotion.rotationValue = 3.1415926 * 2 * degree2 / 360
joint3 = root.joints[3]
revoluteMotion = adsk.fusion.RevoluteJointMotion.cast(joint3.jointMotion)
revoluteMotion.rotationValue = 3.1415926 * 2 * degree3 / 360
joint4 = root.joints[4]
revoluteMotion = adsk.fusion.RevoluteJointMotion.cast(joint4.jointMotion)
revoluteMotion.rotationValue = 3.1415926 * 2 * degree4 / 360
def stop(context):
try:
app = adsk.core.Application.get()
ui = app.userInterface
# Clean up the UI.
cmdDef = ui.commandDefinitions.itemById('SliderJoint')
if cmdDef:
cmdDef.deleteMe()
addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
cntrl = addinsPanel.controls.itemById('SliderJoint')
if cntrl:
cntrl.deleteMe()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))