I’m trying to make the leap from Grasshopper1 to Grasshopper2 with my plugin. It has been interesting so far…
Forgive me if this has been answered before, I could not find it if it has. I’m finding it tricky to figure out how to work with collections in Grasshopper2. In Grasshopper1, you might have built a list, very simply, or in some cases, might need to build a tree. In the case of Grasshopper2, I can’t seem to be able to find any lists, and it seems like I can’t actually build a tree at all. VisualStudio is telling me that Trees are abstract or interfaces, and can’t be created.
So does this mean that if I want to build a tree, I need to make my own child object of the Tree object in grasshopper, or am I about to go down a rabbit hole to wonderland? What am I intended to do here?
Thanks, and looking forward to getting something running in GH2.
the two main differences between GH1 and GH2 are that trees and twigs (or ITree, Tree<T>, ITwig, Twig<T> to name the actual types involved) are all immutable types, and that they allow for metadata to be stored alongside each value. The immutability means it would be very inefficient to build them one value at a time.
There’s also a lot of very intricate code behind the scenes which makes sure that there’s a minimum of memory overhead being wasted when a twig or tree is created. There are actually loads of different Twig<T> implementations which are all hidden from the public api and thus cannot be constructed directly.
Instead, use the Grasshopper2.Data.Garden class to create twig and tree instances from regular collections. It acts as the central factory for these data types.
ps. IPear and Pear<T> is nothing more than a grouping of a value with its metadata. Comes in handy when you need to keep values and metadata synchronised during operations.
Also, it allows for the representation of nulls. A Twig<int> internally stores an int[] which is incapable of containing null values, so if some of the values are supposed to be nulls, it’ll also keep a bool[] storing the null-states. Then, ITwig.NullAt(int index) can be used to get the null-state for any given value. However, since pears are reference types and ITwig[int index] returns an IPear, nullness can be directly communicated.