- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I would like to link the slider to a user parameter so that the value of the slider can control the user parameter. But every time after modifying or creating a user parameter, the interface goes back to the very beginning, as if it hasn't changed, which it does, just for a moment. You can watch the attached video。
import adsk.core, adsk.fusion, traceback
_app = None
_ui = None
_handlers = []
_doc = None
_design = None
slider_value = 0.0
length_param = None
def create_or_update_length_parameter():
try:
global length_param
length_param = _design.userParameters.itemByName("Length")
if length_param:
_design.userParameters.itemByName("Length").expression = str(slider_value)
_ui.messageBox('Length parameter updated to: {} cm'.format(length_param.value))
if not _doc.isSaved:
_doc.saveAs("ceshi", "This is the first save description.")
else:
_doc.save("This is an update description.")
else:
length_value = 10.0
lengthValue = adsk.core.ValueInput.createByReal(length_value)
lengthparam = _design.userParameters.add("Length", lengthValue, "cm", " ")
_ui.messageBox('Length parameter created with value: {} cm'.format(lengthparam.value))
except Exception as e:
_ui.messageBox('Failed to create or update Length parameter:\n{}'.format(e))
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 MyInputChangedHandler(adsk.core.InputChangedEventHandler😞
def __init__(self😞
super().__init__()
def notify(self, args😞
try:
if args.input.id == "slider_controller":
global slider_value
slider_input = adsk.core.FloatSliderCommandInput.cast(args.input)
slider_value = slider_input.valueOne
_ui.messageBox('Slider value changed to: {}'.format(slider_value))
create_or_update_length_parameter()
_ui.messageBox('Length parameter value updated to:')
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)
onDestroy = MyCommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
inputs = cmd.commandInputs
slider_input = inputs.addFloatSliderCommandInput("slider_controller", "Slider", "cm", 0, 10.0)
slider_input.valueOne = slider_value
onInputChanged = MyInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
_handlers.append(onInputChanged)
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context😞
try:
global _app, _ui, _design, slider_value, length_param,_doc
slider_value = 5.0
_app = adsk.core.Application.get()
_ui = _app.userInterface
_design = _app.activeProduct
_doc = _app.activeDocument
cmdDef = _ui.commandDefinitions.itemById('sliderCommandSample')
if not cmdDef:
cmdDef = _ui.commandDefinitions.addButtonDefinition('sliderCommandSample', 'Slider Command Sample', 'Sample to demonstrate slider input.')
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()))
Solved! Go to Solution.