Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Changing existing user parameters.

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
james00white
135 Views, 2 Replies

Changing existing user parameters.

Hi all,

 

I am experienced with parametric modelling but I am completely new to writing scripts so apologies if this is all a bit basic!

 

So I have a fully parametric model which is dimensioned by user parameters and which updates as the parameters are updated.

I would like to be able to run a script which would call up each user parameter value and allow for it to be edited (or not). For example, if I have a cube with user parameters Length, Width, Height I would like to be able to change none, one or all using a dialog box.

I have been following a few tutorials which detail how to create new user parameters but I just want to edit the existing ones. 

Any help with a bit of code to get me started would be awesome!

Cheers

 

2 REPLIES 2
Message 2 of 3

Hi @james00white ,

 

Here you have a script to update "length" parameter from user input in a command input:

 

import adsk.core, adsk.fusion, adsk.cam, traceback

app = adsk.core.Application.get()
ui = app.userInterface

COMMAND_ID = 'commandParameter'
LENGTH_PARAMETER = 'length'
LENGTH_COMMAND_ID = 'lengthCommandId'
_handlers = []

class CommandParameterExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: adsk.core.CommandEventArgs):
        try:
            # get reference to the length command input
            cvi = adsk.core.ValueCommandInput.cast(args.command.commandInputs.itemById(LENGTH_COMMAND_ID))
            # get reference to the length parameter
            lenPar = adsk.fusion.Design.cast(app.activeProduct).userParameters.itemByName(LENGTH_PARAMETER)
            # udpate parameter expression with user defined expression in the command value input
            lenPar.expression = cvi.expression
        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class CommandParameterDestroyHandler(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 CommandParameterCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: adsk.core.CommandEventArgs):
        try:
            des = adsk.fusion.Design.cast(app.activeProduct)
            # Connect to the command destroyed event.
            onDestroy = CommandParameterDestroyHandler()
            args.command.destroy.add(onDestroy)
            _handlers.append(onDestroy)
            # Connect to the command execute event.
            onExecute = CommandParameterExecuteHandler()
            args.command.execute.add(onExecute)
            _handlers.append(onExecute)

            # Get the CommandInputs collection associated with the command.
            inputs = args.command.commandInputs

            # get length parameter's expression
            lenValInp = adsk.core.ValueInput.createByString(des.userParameters.itemByName(LENGTH_PARAMETER).expression)
            # create command value input and assign it's initial expression as defined in the length parameter
            inputs.addValueInput(LENGTH_COMMAND_ID, 'Length', des.unitsManager.defaultLengthUnits, lenValInp)
        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def commandParameter():
    try:
        # Get the existing command definition or create it if it doesn't already exist.
        cmdDef = ui.commandDefinitions.itemById(COMMAND_ID)
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition(COMMAND_ID, 'Update parameter sample', 'Update parameter from CommandInput')
        # Connect to the command created event.
        onCommandCreated = CommandParameterCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)
        # Execute the command definition.
        cmdDef.execute()
        # Prevent this module from being terminated when the script returns, because we are waiting for event handlers to fire.
        adsk.autoTerminate(False)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def run(context):
    try:
        commandParameter()
    except:
        app.log('Failed:\n{}'.format(traceback.format_exc()))

 

 

Before running this script, make sure you have the "length" parameter defined, or rename it (in line #7) in the code with the name you defined it.

Please note that in the command input you can supply any valid expression with system defined units, not only values, and that same expression will be updated to the parameter's expression.

You can add other parameters as needed.

 

Regards,

Jorge

Message 3 of 3

@Jorge_Jaramillo 

 

Hi Jorge,

 

Thank you so much for this code, it is exactly what I was looking for! I really appreciate you taking the time to do this and help me out.

 

It works perfectly and as I add further parameters it is really going to help me learn how the code works. I am looking forward to learning more!

 

Thanks again

James

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report