Python persistent state and grasshopper canvas

I want to get some input from the user about x/y offsets, but keep that state persistent rather than ask them every time the canvas runs.

I’ve tried this method to store the x/y inside the python script as a variable

import rhinoscriptsyntax as rs

if 'x_o' not in globals():
    x_o = 0.0
if 'y_o' not in globals():
    y_o = 0.0
if get_offsets:
    origin = rs.GetPoint("Select Origin")
    map_to = rs.GetPoint("Select point to Map to")
    offset = map_to - origin
    x_o = offset.X
    y_o = offset.Y

x_offset = x_o
y_offset = y_o

but I get some weird errors with the grasshopper/Rhino canvases. If I use a button on the input, like below, I have to click the button and get inputs, but then the grasshopper canvas is locked from movement and editing until I click the button again and get the input a second time.

Conversely, if I use a boolean toggle, I have to enter the first point (origin) twice, before it actually accepts the point.

Is there a better way to be going about this?

I just switched this over to using scriptcontext.sticky data storage method, and that hasn’t changed the problem. It appears to be more about the interaction between the grasshopper button and python component.

The button will send two values on every mouseclick. On mousedown you get the pressed state (usually true), on mouseup you get the unpressed state (usually false).

I can’t imagine that runs well with you if logic.

if it’s sending a True and then a False, I can’t imagine why that would play with getting the points twice and locking the canvas. It’ll run once when the button is pressed and sends the True signal, but then it should go back to False. It’s not playing with the logic because a similar component works just fine without the GetPoint(). It’s locking up the grasshopper canvas, so there’s some interaction there between the button and the rs.GetPoint() that doesn’t exist with other code I’ve tried.

@nathan.perkins, I believe this will help you read the suggestions below and the link to the old forum:

@DavidRutten, the behavior of that button-component should be changed, it is not working properly because it is always sending whatever value when unpressing. It should only send True if pressed then when unpressed it should do nothing. Then when pressed again it should send the False. I’m not saying this should be the sole behavior but it should at least be optional and not force users to have to code that behavior.

1 Like