Help with GHPython treehelper needed

Hi,

I need your help regarding accessing items from a tree using GHPython. Please see attached image:

Here is the corresponding GH file:
treehelper_1.gh (7.2 KB)

1.)
What is the correct way to access the individual elements (like the green marked 11.4 in this example)? I tried tree_to_list from treehelpers, but for some reason, although I can access the {0;x} elements, I cannot access the {1;x} elements.

Shoould be really easy… for someone who knows how to do it.

2.)
How can I check programmatically that there is {0;x} and {1;x} but no {2;x} ?

3.)
Is there an article / video you could recommend to better understand the topic? I already watched some videos about data trees, but when it comes to the details, I am still lost. :frowning:

Thank you for your support.

Just found that x.Branch(5)[1][0] works. But this is not a very elegant solution. How to get there using something like …{1;1}[1][0] ?

Try this, maybe it will gve you some ideas:

import ghpythonlib.treehelpers as th
import Grasshopper.Kernel.Data.GH_Path as gp

print x.BranchCount # 8 
print len(x.Branch(0)) # 6
print x.DataCount   # 48 = 8 x 6

print x.Branch(3)[1].X
print x.Branch(5)[1].X

print x.Path(0)

path = gp(1,0)
print x.PathExists(path)

for i in x.Paths:
    if i[0]==1: print i
3 Likes

also this:

3 Likes

Thanks @Adam_M that works nicely and has everything I need to continue!

Therefore I marked your answer as the solution.

One thing was missing, so in case anyone out there now wants to know how to access the red marked “13.5” from my initial post - it is now pretty easy (based on the solution):

print x.Branch(gp(1,1))[1][0]

Means branch {1;1} list element 1 index 0

Thanks @inno for showing this solution. I tried it and it works perfectly as well. So now I have two different solutions that both work properly. Unfortunately I can only mark one as the solution. But yours is one as well.

to get to 13.5 value using the method from @inno, it would be

print test[1][1][1][0]

or

print test[1][1][1].X