Function notify and its parameters

Function notify and its parameters

jf0009
Observer Observer
434 Views
0 Replies
Message 1 of 1

Function notify and its parameters

jf0009
Observer
Observer

Hello, I'm having trouble understanding the "args" parameter that is passed to def notify, and understanding notify itself.

 

In my code, I'm doing a basic script that will take a single input from the user to draw a circle in order to get the basics down of making a script with event handlers in python. In all the examples I've referenced, the event handler has a function called notify(self, args) and then uses the parameter "args" to create a reference to the command inputs object. To me, it doesn't seem like args is using the Object Model Tree how I would expect. 

 

Could someone explain how "notify" works or point me in the right direction for some documentation on the subject. Sorry if my terminology is incorrect or my question is simple. Here is my code that I am referencing:

 

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

handlers = []

class testScriptRunEventHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            app = adsk.core.Application.get()
            ui = app.userInterface
            cmd = args.command
            inputs = cmd.commandInputs
            
            valueInput1 = inputs.addValueInput('value_1','Input a number','cm',adsk.core.ValueInput.createByReal(10))
            #The OK button is pressed
            onExecute = testDialogOKEventHandler()
            cmd.execute.add(onExecute)
            handlers.append(onExecute)
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class testDialogOKEventHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        app = adsk.core.Application.get()
        ui = app.userInterface
        try:
            cmd = args.firingEvent.sender
            #from selectionInput1
            input1 = cmd.commandInputs.itemById('value_1')      
            testProperty = input1.objectType
            print (testProperty)
            #create necessary paths/objects to make a circle on xy plane
            design = adsk.fusion.Design.cast(app.activeProduct)
            activeComp = design.activeComponent
            xyPlane = activeComp.xYConstructionPlane
            sketch = activeComp.sketches.add(xyPlane)
            centerPoint = adsk.core.Point3D.create(0,0,0)
            circle = sketch.sketchCurves.sketchCircles.addByCenterRadius(centerPoint,input1.value)
            
            
            ui.messageBox('It worked')
            
            # Force the termination of the command.
            adsk.terminate()              
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        cmdDefs = ui.commandDefinitions
        buttonSample = cmdDefs.addButtonDefinition('MyButtonDefIdPython', 
                                                   'Python Test Button', 
                                                   'Sample button tooltip')
        sampleCommandCreated = testScriptRunEventHandler()
        buttonSample.commandCreated.add(sampleCommandCreated)
        handlers.append(sampleCommandCreated)
        
        # Execute the command.
        buttonSample.execute()
        
        # Keep the script running.
        adsk.autoTerminate(False)
    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
        
        #clean house
        cmdDef = ui.commandDefinitions.itemById('MyButtonDefIdPython')
        if cmdDef:
            cmdDef.deleteMe()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

0 Likes
435 Views
0 Replies
Replies (0)