algorithmic modeling for Rhino
Tags:
Hi,
What you can do is use "Save State" in Grasshopper to recall different states of sliders and so on, almost like calling different presets. I havent tried exactly what you can change, but I would have thought all input types should be supported (not sure about panels).
The states are saved in the file though.
Otherwise you can read an .xml file in yourself and use those values instead of sliders. You can have the sliders in a different part and have them write their values to that .xml file. But that wont set the sliders once you load a config from the .xml file. So save states are the only thing that does that.
There was a discussion the maybe 3 or 4 weeks ago about parametically setting the values and domains of sliders and there was a solution for it, but it was kind of a hack using a C# component (cant find it right now). I agree that there should be a simple way to set the domains parametically.
Have a look at Metahopper, I think it can set sliders and so on.
Thank you Armin for your reply, I think Andrews Answer is more towards what i was looking for
Hi Tudor -
It is possible to read just about any information from a GH file if you are tenacious enough and willing to write some code :) .GH file format is just a binary encoding of .GHX - same information, just one additional step to read from it.
Things are naturally much easier if you have a running instance of GH - you can utilize the GH_DocumentIO class to open GH files directly from disk and parse their object table in order to get / modify values. However, you can also utilize the more "bare bones" GH_IO namespace to parse .gh and .ghx files.
With GH_DocumentIO (available from inside a running GH instance only):
GH_DocumentIO io = new GH_DocumentIO();
io.Open(filePath);
GH_Document document = io.Document;
A = document.Objects;
at that point you can do anything with the object table you want (filter by type, modify values, etc) - if you want to commit your changes to the document be sure to call io.Save();
From an executable running outside of Rhino/GH - you'll use GH_IO.dll.
You'll be able to traverse the document object structure by navigating and traversing down the "chunks" of the archive. The hierarchy of this matches the xml structure - what you see if you save the file as a .ghx and then open it with a text editor.
You can start by retrieving the root node of the document, like so:
GH_Archive archive = new GH_Archive();
archive.ReadFromFile(filePath);
GH_Chunk rootNode = archive.GetRootNode;
Then you'll want to burrow down a bit to get to the document objects:
GH_Chunk definitionChunk = rootNode.FindChunk("Definition") as GH_Chunk;
GH_Chunk definitionObjChunk = definitionChunk.FindChunk("DefinitionObjects") as GH_Chunk;
int objectCount = definitionObjChunk.GetInt32("ObjectCount");
for(int i = 0;i < objectCount;i++){
GH_Chunk objectChunk = definitionObjChunk.FindChunk("Object", i) as GH_Chunk;
}
Inside that for loop, you can do stuff like get the name or guid of the object and burrow down even further. For the example of a slider, here's how you'd access the current value of the slider:
for(int i = 0;i < objectCount;i++){
GH_Chunk objectChunk = definitionObjChunk.FindChunk("Object", i) as GH_Chunk;
string name = objectChunk.GetString("Name");
//This may not be foolproof - probably better to check the object GUID
if(name == "Number Slider"){
GH_Chunk containerChunk = objectChunk.FindChunk("Container") as GH_Chunk;
GH_Chunk sliderChunk = containerChunk.FindChunk("Slider") as GH_Chunk;
double sliderValue = sliderChunk.GetDouble("Value");
}
You can also modify this value. None of the normal safeguards are in place so make sure you're not setting some illegal value. It's easy to make a slight mistake and make a corrupt/unreadable file!
To set the value of the slider, first you need to remove the value "item" and then re-add it:
sliderChunk.RemoveItem("Value");
sliderChunk.SetDouble("Value", 10.0);
Then, at the end, you can call archive.WriteToFile(filePath,true,false); to overwrite or modify the file (or supply a different path to save elsewhere).
Hopefully this gives you enough of an example - you can obviously get as sophisticated as you want setting values for different objects, or assigning values based on the slider nickname value (user-assigned name).
Hi Andrew,
your answer is really more then i was expecting so thank you a lot! Great to have such a detailed explanation and examples. Was not sure if and how it is possible to access the gh file values without actually running the file but it s great to see both situations.
Best,
Tudor
Thanks! This thread was helpful!
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by