C# data tree -get data by index number


I’m super beginner to c# data tree, and I assume this is a very simple question.

I would like to have same result as like vanilla grasshopper by using ‘list item’ in c# script.

For example, in the screenshot image, I have 3 branches and by using list item [0] the result shows 3 branches have one item with index number zero.

I wrote stupidly like ‘dataTree.Index(0)’ to represent my question.
I searched on the google but couldn’t find the correct answer.
Can you help me to figure out the right command?

CSharp_treeIndex.gh (4.4 KB)

List Item works with Lists:

2021-01-05 22_11_06-Grasshopper - CSharp_treeIndex_

List Item does not see your input tree as a tree, but it picks every branch of your tree and see it as a single list, it extract the item you are asking at “i” index, and output that item while giving it the same path it was in.
So, the output List Item will consequently be a tree again.

There are few components in grasshopper (out of the total) that work with whole data trees.

Do you really need to work with trees?

To reply your question:

 private void RunScript(DataTree<Point3d> treePts, int index, ref object A)
    Grasshopper.DataTree<Point3d> tree = new Grasshopper.DataTree<Point3d>();
    foreach(Grasshopper.Kernel.Data.GH_Path path in treePts.Paths){
      tree.Add(treePts.Branch(path)[index], path);
    A = tree;

CSharp_treeIndex_re.gh (11.6 KB)

While working with c# in grasshopper, if you have an object, after you write the period it should suggest you all its internal methods and variables:


1 Like

Thanks a lot for the code!

As you mentioned, the result is also data tree type so I couldn’t make further operation based on that. Anyhow your answer is exactly what I was looking for!

regarding period/suggestion option, unfortunately I am using Mac OS and rhino in Mac does not support that function :grinning_face_with_smiling_eyes:

thanks again!

I still have rookie knowledge in c#.
Everything i’ve learnt is thanks to looking experts here in the forum and that suggestion thing in the code.
Very often i do search for method/variables using that functionality. Even now, with my (not great) experience in c# , i probably wouldn’t be able to code at all without it.

If you plan on learn/code in c# in grasshopper, you should consider having a machine/partition/VM to work with rhino for windows.
Or ask in the forum what is the best environment to code in c# on mac… there might be some smart solution i’m completely unaware of.

1 Like

Given the opportunity, some hints/tips:

  1. GH DataTrees are good things for managing nested (so to speak) collections of data (a DT is “kinda” a Dictionary of Lists [add some “” more])… and very bad things if your world extends beyond GH/R (for instance in the real-life AEC BIM market sector where both play a very little role - if any at all). This means that you should always judge portability (if we can skip the RhinoCommon SDK Method calls part) VS any benefit (mostly due to speed VS Generative Components and the likes).
  2. So if your world is the GH/R combo … is imperative to FULLY master the art of dealing with DataTrees if you have plans to do anything - in fact - with the R/GH combo. Failing to do so … I predict pain and tears aplenty. 99.99% of issues that novices have are due to their total ignorance on that matter.
  3. Another big thing is to master LINQ: a way to manage queries in Lists be these collections of Types that you know … or Lists of custom class types (these are … er… hmm … “kinda” a DataTree).
  4. I have tons and tons of Lists/DataTree C# “tutorials” (so to speak) for my people in the practice … thus if you want some to burn your fingers notify (but rather describe what is “roughly” your goal - the less case specific the better).
1 Like

wow! thanks for the advice :smiley: Do you have a public website where I can access to your tutorials about dataTree/List c#?

Er … hmm … I confess that I don’t like publicity (at least of that type) nor I’m Academic in any sense, he he.

All the things that I have are done for usage inside the practice that I own. If they are implicitly or explicitly important then are classified (and thus sealed from any outsider) as internal.

But … well … I can prost various non sensitive things: just think what you want to do. For instance: shift items in a List or jitter a List or take random items from a List or partition a List of objects into a DataTree or query/group a List of things or filter collections or cluster collections or get cats and do dogs (LOL) … blah, blah).

1 Like