Python script to save/load GH Inputs (sliders, panels, etc)

Sorry for the long post.

I’m working on a Python script that will save all grasshopper inputs into a CSV file, save the input NickName, Guid and Values.

As well a Script that will load the CSV file and update all inputs inside gh.

I’m not a coder, so I’m struggling a little with some of the input components, mainly the ValueList, BooleanToggle and the TableInputs.

on the BooleanToggle:

if type(obj) is gh.Kernel.Special.GH_BooleanToggle:
    print obj.ToggleValue 

I get an error:

GH_BooleanToggle has no attribute ToggleValue

but if you paste the component onto NotePad you can see the name of the attribute, so don’t know how to get this one.

on the ValueList:

I have no idea on how to get the values, I was thinking on getting the amount of list by using ListCount, and than looping to through that range by using obj.ListItem(i).Expression for value and obj.ListItem(i).Name for the name, but I get the same error that the object doesn’t have those attributes.

            #### get all Value List #####
if type(obj) is gh.Kernel.Special.GH_ValueList:
   print obj.ListCount
   for i in range(0,obj.ListCount-1):
        print obj.ListItem[i].Expression
        print obj.ListItem[i].Name

for the Panels, I got it working for both saving and loading, with one small problem. It will load the list values but if you doubleclick on the panel, all strings are together instead of different lines, but only if you double click:

1

2

attached my code, hope someone can help me finish this and it will be usefull for everyone as well.
Cheers

Python Read Inputs.gh (13.8 KB)

Are you aware you could retrieve the values of sliders and value lists with Metahopper?

no I didn’t know, is it with save snipet?

Attached is an example for a slider and a value list. Note that on the value list you must format text like this “aaa” with quotation marks

slider_value_list_metahopper.gh (11.9 KB)

I didn’t know about it, but doesn’t really do what I need. I got a massive program with many sliders, panels, etc. that’s is why I when with a script that will save all inputs into a file, instead of an 1 by 1 approach.

and also need the ability to load everything again from a file.

This will also work for batch, looping through a list of CSV files.

Before you go any further, are you aware of the native Solution > Save State functionality:

It allows you to save named states of all/selected sliders, boolean toggles, gene pools etc. You can probably make/save/read these states to file using a script if need be.

1 Like

great I didn’t know about that one, can that be scripted? and exported/imported as a file?

I’m not sure, but would assume so. It looks a bit convoluted though.

It would be pretty straightforward going down the Python path you already on. Iterating the canvas, grabbing say all named sliders, toggles, and panels and their values, stuffing that into a dictionary, and dumping this to a .json file on disk. Which you could then read in another Grasshopper file and set all named sliders, toggles, and panels to the whatever the name/key-value is in the dictionary.

1 Like

I agree, so any idea on how to get the Toggles Values and the Value List? I’m reach my limit on how to solve it

Indeed, here’s a quick example of the first part (i.e. how to get and write slider/toggle/panel data to disk):


211117_WriteNamedObjectDataToDisk_01.gh (7.2 KB) (Edit: Added “pretty print” JSON formatting)

For value lists, have a look at this example:

2 Likes