Save and Restore Grasshopper slider positions

I’m wondering if there is a way, I can save all my slider positions and the state of various switches and things like that. I know I can save the whole grasshopper file, but it gets to be unmanageable if I modify the grasshopper definition and want to re-process 40 files, I either have to put the changes into 40 grasshopper files with the positions of the sliders I want, or what would be much better, just have one grasshopper file and just re-load the slider positions as needed.

Hi @James_Richters ,

You can use the Save State feature.

You could have all your sliders and toggles in one GH file, use the save states for those positions and feed the slider and toggle outputs into the “output data” component.

Then in your 40 other files you could use the “Input data” component to receive said “master toggle values” and influence each sub script from the control of the master script .

I’ve not actually tested this but I think that will work.

The data in/out components are in the Utilities tab.

1 Like

@michaelvollrath Wow that seems like exactly what I want to do!
I seem to be missing something though,

Say I start with these two controls


and save the state:

Then I mess them all up like this:

When I Restore state it changes the positions of the components, but it doesn’t change their outputs:

How do I get it to use the new positions after I restore?

Interesting, I’d expect it to update the output values as well but perhaps the solution needs to be recomputed after a state restore is changed?

@martinsiegrist do you happen to know the answer to this?

1 Like

It works on the Number Sliders and Boolean Toggles, but not the Control Knob or the Multidimensional slider

2 Likes

Interesting, that may just be an oversight or omission, I’m trying to think if there’s a work around for that outside of scripting the functionality.

And having a number node connected directly after the MD output doesn’t work either? I don’t expect that it would… Hmm

Not ideal but you could make your own MD slider by combining two slider inputs since we know the state saves those…

And the radial knob could be a slider as well. Again it’s nice to not NEED to have it be this way but could be a potential work around

Just reconnect one wire upstream of everything, forcing a complete re-compute.

:rofl: QA is not McNeel’s strong suit.

1 Like

Thanks for the suggestion, knowing a recompute is what is needed… I figured out that pushing the Recompute from the Solution menu works!

1 Like

Would a stream filter node so the trick with a button on the gate. The button could be called Refresh and when pressed would trigger the gate which would cause an upstream data change.

I was just trying to think of a way without manually clicking something but I guess F5 would recompute, data change would work, and a scripting solution would work I suppose

Weird. Would be nice if the update was consistent for all inputs.

1 Like

Recompute helps…

Probably overthinking it but can you call Recompute from outside the definition? I like that you can call a boolean from the Remote Control aka Grasshopper Panel in Rhino so theoretically all his master controls could exist there.

I guess the boolean in an RCP could just toggle a script that fires a recompute. Actually the boolean itself would trigger a recompute nevermind :grin:

Can the Save and Restore State and Recompute all be done with a script?
I’m thinking it would be nice if when I ran my export script it also ran another script that saved the state of all the inputs. I could then have another script that restores them when I open a new Rhino document. I would want to use the Rhino document name as the name of the saved states. It would be nice if I had a script that just sat around waiting for the Rhino document name to change and when it did it would automatically restore the state with the same name and recompute.

Yes, I believe so though I’ve not done so myself but take a look at these methods in the GH API Docs:

Save State:

Restore State:

StateServer:

1 Like

I wasn’t able to save everything I needed with the save state feature. I need to also save text input that is in panels and things like that. So I made an attempt to write a script to save and restore the input values. I have it working well for Number Sliders, Control Knobs, Multidimensional Sliders, Boolean Toggles, and Panels. It does automatically do the Re-Compute for you so everything updates.

Save and Restore Inputs.gh (13.7 KB)

It has a place for a button to SaveInputs and a button to RestoreInputs. buttons work best because there is no point in saving them so it’s not trying to restore the thing you are controlling it with. It traps out having both true at the same time as that is invalid.

When you push SaveInputs, it gets all the input values and stores them in an .ini file
if there is a Rhino document loaded, it uses the Rhino document name, with a .ini extension, this is useful for saving configurations specific to Rhino documents and is what I need to do.
if there is no Rhino document active, then it uses the Grasshopper file name, with a .ini extension, this is intended to save some default values.

the ini file looks like this:

.ini file
[Sliders]
RestoredSlider0 | 823d2edb-c63a-4ccd-bb82-9ddc11236a5f = 0.976
Slider 1        | 92e9bd54-b2d0-4374-9211-9d04aac79b3e = 0.072
Jims Slider    | 61cef2db-b01d-4574-8c36-f2b62bcd490d = 0.656
Another Silder  | 638441ac-17be-4e19-83c5-a97e14aac245 = 0.862
Slider          | 32b7efdc-10b8-496c-98e6-abdb5007d6fe = 1
Slider          | e5a22d40-2449-44f1-9c51-a2f36c206eab = 0.756
Slider          | b7cc3362-b6f5-479e-8168-7ebe9761676c = 0.163

[Control Knobs]
Jims Knob | 0bd0c5af-978d-44ee-a746-3d5df41d4ccb = 3.141592
Knob       | dc5e2de7-69fa-46b9-be59-f971abedb3f5 = 0.73
Knob       | 5193bc6a-dd7e-4ae0-9189-237cbb547eba = 1.09
Knob       | 05086434-b30d-4f3b-8dbc-4c573519202f = 0.47
Knob       | ea1ef965-e65a-4c25-8232-1f1b0e6d7478 = 0.25

[Multidimensional Sliders]
Alignment | de17dfae-9855-47cd-ae4e-1aab39b9ccb0 = 0.933141196646342,0.893598044790873,0.5
MD Slider | a886c35b-3d99-43f0-8d9b-67d8d4e97560 = 0.257698245164825,0.213643422940882,0.5

[Boolean Toggles]
Toggle     | 485a4aa3-ca8b-48aa-8bf1-8674f513cbf7 = True
Toggle     | 71821851-66da-4e58-a25b-a10da108ea70 = False
Warp Speed | ffdeeecf-4bde-46b0-af7a-33247ac3b071 = True
Toggle     | e3cd0f5e-e1a9-497c-8a7d-d6e228dac775 = True

[Panels]
Panel | f4079ddb-ce02-420e-a399-39309836fc8b = 42
Hello | 05afbaee-2657-4159-993a-2a4372b6f556 = This is a test

So you could just edit the .ini file if you wanted to and type in new numbers. It matches things up by their GUIDs if you change the name of something it ignores the name. I only put the name in the .ini file so it makes sense if you want to edit it.

I’m really amazed that I got that far because I can’t find ANY documentation on Grasshopper components like what their properties are. I got lucky with quite a few lucky guesses and managed to get something that works. if there is some documentation for Grasshopper scripting that would tell me the properties of components and I just don’t know where it is, I would really like to know about it.

I started working on saving and restoring colors, but only had limited success. I am able to save and restore the Colour Picker, but it doesn’t update the canvas icon, just the output value, even after a recompute. Even though I got that to work, I can’t get the Colour Swatch to work at all, (and of course that’s the one I wanted) even though it’s pretty much the same thing. I get an error:

1. Error (CS1061): 'Grasshopper.Kernel.Special.GH_ColourSwatch' does not contain a definition for 'Colour' and no extension method 'Colour' accepting a first argument of type 'Grasshopper.Kernel.Special.GH_ColourSwatch' could be found (are you missing a using directive or an assembly reference?) (line 259)

but the Colour Picker does have a Colour property with R,G,B,A components I can’t guess what the property might be for the Colour Swatch and have no idea why it would be different than the Colour Picker when it’s pretty much the same thing. What’s needed is some documentation.

1 Like

This is pretty awesome @James_Richters, thanks for sharing!

Regarding GH documentation this is all I know of:

Regarding the color picker logic, I have this bit of code I adapted from something that @AndersDeleuran shared with me. It stores the color picker choice (swatch if you will) in a .json file so that it is persistent across Grasshopper doc file open. Originally I had it saving in the Rhino doc but ended up wanting it embedded int the GH doc instead.

Perhaps you can adopt the logic for your .ini method?

All a color needs to be for GH to read it is the indexed values RGB, HSV, etc. 255,255,255 so it can be a comma separated string stored in your .ini and doesn’t need to specifically be the swatch component.

That being said if you want it to autopopulate a connected swatch component with the saved color state you could instantiate a swatch by AddObject method and set the script component’s color source source to the color swatch object via the AddSource method
You would also need to set the initial value of this added swatch object to be the stored color value from your .ini file (if that’s what you seek?) I believe you could set it from CreateAttributes

EDIT: Sorry forgot to attach the code:

import rhinoscriptsyntax as rs
import json

#Set Unique Value ID Per Component Instance
Color_Id = "Color UId:" + str(ghenv.Component.InstanceGuid)

# Check if the 'S' button is set to true
if S:
    # Pick New Color With Rhino Color Picker
    new_color = rs.GetColor("255,150,255")

    if new_color:
        # Serialize color to JSON and store it
        new_color_json = json.dumps(str(new_color))
        ghenv.Component.OnPingDocument().ValueTable.SetValue(Color_Id, new_color_json)

# Read data from persistent Grasshopper document value table
stored_color_json = ghenv.Component.OnPingDocument().ValueTable.GetValue(Color_Id, "")

# Deserialize JSON string back to output
if stored_color_json:
    C = json.loads(stored_color_json)
    ghenv.Component.Message = None
else:
    C = "255,150,200"
    ghenv.Component.Message = "Select Color"
1 Like

I made some updates to the script:
fixed panels to work with multi-line data and text containing “=”
added in Value Lists. Value Lists work even if you use the checkmark version where you can select more than one item.

Save and Restore Inputs.gh (14.5 KB)

1 Like

Nice! The checkmark version is really handy!

1 Like

Thank you for creating and sharing this very useful script.

1 Like

I finally figured out to save and restore the Colour Swatches!

I can also save and restore the Colour Picker, however it doesn’t change the graphics in the component, but it does change the output value. I tried all kinds of things like expiring the solution and recomputing, the graphic just doesn’t change. If you close the script and open it again then it is correct, but that’s the only way I’ve been able to figure out how to update the component graphics.

Save and Restore Inputs with colors.gh (28.3 KB)

1 Like