Python: access tree branch, make changes

Halo friends!

I am trying to mess around with DataTrees in Python.
I need to cull one item from a branch, if the length of lists in a branch is odd.

However, when I do so, it stacks all the items in one branch, culls all the branches that are odd, and returns 1 stacked branch and even branches…

What am I doing worng? (26.4 KB)

Is there a reason for using tree access? Because with list access you could just do:

if len(x)%2:

Thanks for reply Adam!

I want to learn how I can access and change data within a branch in python, without affecting the tree structure.

But also, I need to work with DataTree, cause I am working with multimodular geometry like so:

If I understand your intent, you are trying to cull the last item from the branches with odd indices. (8.3 KB)

Edit: There are more efficient ways of doing this, I was trying to minimally modify your code. Instead of iterating the list, you could delete the last item with:
del branch[-1] (8.4 KB)

Edit2: Further simplified, you can just modify the original tree and pass it on to the output (only had a couple of minutes to look at this earlier):

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Grasshopper as gh
import ghpythonlib.treehelpers as th
from pprint import pprint
import itertools

print x
for i, branch in enumerate(x.Branches):
    if branch.Count % 2:
        del branch[-1]

DT = x

print DT (8.6 KB)


1 Like