Share inputs between event handlers

oyvindTMNSU
Advocate Advocate
629 Views
5 Replies
Message 1 of 6

Share inputs between event handlers

oyvindTMNSU
Advocate
Advocate

I want to hover over a face and getting the point the the mouse pointer is over at every moment. 
I have been able to do that by using the preselectMouseMove handler. I can print the cursor position to the console. Works perfectly. 
but how can I get that input values into the changed input handler to make a text box update live with the values of the position?



so what I really ask for: how can I access variables/inputs from one event handler in another event handler? Anyone?

 

@BrianEkins @boopathi.sivakumar @BrianEkinsADSK  

0 Likes
Accepted solutions (2)
630 Views
5 Replies
Replies (5)
Message 2 of 6

oyvindTMNSU
Advocate
Advocate

No one that can point me in the right direction?

0 Likes
Message 3 of 6

BrianEkins
Mentor
Mentor
Accepted solution

You can have global variables in your program. These are defined outside of any functions and that single variable is available within the functions. To set the value of a global variable you need to declare it within the function that will be changing it by using the "global" keyword.

lastPoint: adsk.core.Point3D = None

def getPoint():
    global lastPoint
    lastPoint = adsk.core.Point3D.create(1, 2, 3)


def usePoint():
    app.log(f'The point coords are {lastPoint.x}, {lastPoint.y}, {lastPoint.z}')
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
1 Like
Message 4 of 6

oyvindTMNSU
Advocate
Advocate

Thank you for answering! I thought I read something that global variables should be avoided. But if that’s the intended way I’ll use that. 

0 Likes
Message 5 of 6

BrianEkins
Mentor
Mentor
Accepted solution

Global variables have their place, and they're often misused, but this is a good case where they make sense. Where you need to share a value between different functions.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
1 Like
Message 6 of 6

oyvindTMNSU
Advocate
Advocate

What i want to do is to get the coordinates on a face when hoovering over with the mouse. I get that by doing this and it prints out the position perfectly in the debug console. 

 

# Event handler for the preSelectMouseMove event
def command_preSelectMouseMove(args: adsk.core.SelectionEventArgs):
    # Code to react to the event
    inputs = args.selection
   
    hoover_test: adsk.core.SelectionCommandInput = args.selection
    hoover_point = inputs.point
    # print(f'{inputs.entity}')
    print(f'{hoover_point.x:.2f}')

Now I want to display the hoover_point readings (live as the mouse moves) in a dialog box as a text. Meaning I need to transfer the hoover_point variable over to def command_inputChanged

 

# This function will be called when the user changes anything in the command dialog.
def command_inputChanged(args: adsk.core.InputChangedEventArgs):
    changed_input = args.input
    inputs = args.inputs
    futil.log(f'{CMD_NAME} Input Changed Event fired from a change to {changed_input.id}')


    start_point_selection_input: adsk.core.SelectionCommandInput = inputs.itemById('spline_start')
    position_box: adsk.core.TextBoxCommandInput = inputs.itemById('position_box')
    type_box: adsk.core.TextBoxCommandInput = inputs.itemById('type_box')

But I don’t get it to work, even if I make the hoover_point variable global. Maybe I do something wrong when I set up the global variable?🤷 would really appreciate an answer on this. Feels like this is a part of the add writing I just don’t get 100%. 

0 Likes