Any sample for 3 level of branches in DataTree Grasshopper c# dotnet code?

I am looking for a sample code to create three level of braches in a DataTree.
I know I will need to use IGH_Goo but I do not know how to use it.

I could not find GH_Structure either. which namespace I should use?

How about the code below. You don’t need to use IGH_Goo or GH_Structure, I think.

DataTree<Brep> brepTree = new DataTree<Brep>();
for (int i = 0; i < 3; ++i)
{
    for (int j = 0; j < 2; ++j)
    {
        for (int k = 0; k < 4; ++k)
        {
            GH_Path path = new GH_Path(i, j, k);
            Sphere sphere = new Sphere(new Point3d(i, j, k), 0.5);
            Brep brep = Brep.CreateFromSphere(sphere);
            brepTree.Add(brep, path);
        }
    }
}
3 Likes

Many thanks Menno ! Really? I do not need to use Goo. But I am very interested to understand IGHGoo how it would be used in this particular case.

Many thanks ! :smile:

When you get a data tree, you will use GH_Structure like so:

GH_Structure<GH_Brep> brepTree;
da.GetDataTree(0, out brepTree);

I’m not sure when you would need IGH_Goo…

1 Like

Data trees have been implemented twice (and twice badly in my current opinion). Depending on what sort of code you’re writing, you’ll need to pick one of these.

  1. If your code lives inside a VB, C# or Python component then you use Grasshopper.DataTree<T> where T can be anything.

  2. If your code lives inside a Visual Studio project which compiles to a GHA plugin, then you need to use Grasshopper.Kernel.Data.GH_Structure<T> where T must implement IGH_Goo.

Grasshopper internally uses the latter and heavily relies on IGH_Goo for conversions, previewing, formatting and other operations. The scripting components have logic build in that can convert DataTrees to GH_Structures and vice versa.

3 Likes

Dear David,
It is a GHA plugin. Would you please give me a sample code for IGH_Goo for above case. I mean I want to create a DataTree with three level of branches with IGH_Goo.

The above one Menno shared is working in c# dot net code. But I want to learn IGH_Goo for the same situation

Thanks a lot !

That question makes no sense. IGH_Goo is not a thing in and of itself. It’s an interface that is a requirement for all data going into a GH_Structure. What sort of data are you storing? Or are you only modifying an existing tree?

Oh ! Sorry I misunderstood . I mean IGH_Goo, what it is ? and how to use it?

:smile:

I have no idea about IGH_Goo, I do not know how to cook it or eat it :smiley: Last 4 years you are telling me about it to use it in my code and I still do not know how it works. :smile: Because I diverted to other works. Currently we undertook a bigger project with Grasshopper. So I need deep understanding of it.
I know how to create a 3 level branched tree now from Menno. But what is IGHGoo ? Not sure.
My current problem is solved by Menno’s advise ebove, but I want to understand with deeper understanding about GH Data Tree. Could not find code sample which use IGH_Goo, otherwise I would ask you specific question. I do not know, what it is for.

How are you boss? Please help me to understand the famous Goo.

OK, this is how I understand it:

IGH_Goo is an interface, that is implemented by wrappers for e.g. geometry in Grasshopper, for example GH_Brep and GH_Curve are wrappers for Brep and Curve, respectively.

The class inheritance of GH_Brep and GH_Curve is this:

GH_Brep : GH_GeometricGoo<Brep> : GH_Goo<Brep> : IGH_Goo
GH_Curve : GH_GeometricGoo<Curve> : GH_Goo<Curve> : IGH_Goo

Now, to have a tree FROM grasshopper, you need GH_Structure<T> where T : IGH_Goo, this means that any type that you want to put in the tree needs to implement this interface. To expand on the second example given above:

GH_Structure<GH_Brep> brepTree;
da.GetDataTree(0, out brepTree);
List<Brep> geometryOnly = new List<Brep>();

foreach(GH_Path path in brepTree.Paths)
{
    GH_Brep wrapper = brepTree[path];
    Brep geometry = wrapper.Value;
    geometryOnly.Add(geometry);
}
2 Likes

Menno is correct about the specifics. The reason IGH_Goo exists is because Grasshopper needs to be able to do certain things with its data. It needs to be able to convert it into other types of data when needed, it needs to format the data when displaying tooltips, it needs to draw geometric data in viewports during preview display, it needs to write and read data to gh files etc. etc.

IGH_Goo provides all this functionality for types that have been designed by someone else such as bool, int, double, Line, Point3d, Curve, Brep and so on. All these types must be wrapped into GH_BooleanGoo, GH_IntegerGoo, GH_NumberGoo, GH_LineGoo, … before they are allowed to be used inside a GH_Structure.

If you want to start using a new kind of data in Grasshopper, you’ll need to provide a new type of goo as well. It is possible to put any object into GH_ObjectWrapper, which is a minimal implementation of IGH_Goo for any type, but that’ll severely limit how well your data behaves.

1 Like

Many Thanks Menno ! Oh ! OK ! Now it is very clear to me. It is a big help !

Best regards
:smile:

Many Thanks David. Stupid of me. I was hearing Goo all the time and I could not link the words and I also know about GH_Curve, GH_Brep etc are wrapped, but I was not able to understand Goo is the interface which is wrapping the actual Rhino data… I thought Goo is something else …haahaaa. Something looks like a monster.

Many thanks to Menno and you for such a big help. I am out of a big question today :smile: … I was using goo without knowing I am using it. I understand now properly, hopefully. I could not link the words in my head previously.