Ghpython Assign colors to tree branches

how to assign different n colors to n branches?

color (15.0 KB)

a = []
b = []

#if y have tree access
#for i in range(y.BranchCount):
#branchList = y.Branch(i)
#for j,k in enumerate(branchList):
    #print k

for i in range(x.BranchCount):
branchList = x.Branch(i)
branchPath = x.Path(i)

for j in range(branchList.Count):
    s = str(branchPath) + "[" + str(j) + "] "
    #s += type(branchList[j]).__name__ + ": "
    #s += str(branchList[j])
    s += y[j]
    c = y[j]
1 Like

I have a ColorB cluster for that in my User Object “library”. (green group) (13.7 KB)

The ColorB cluster:

I replied without looking closely at your code, only the thread title (“Assign colors to tree branches”) but when I did look later, I was confused by it. What I expected was similar to the circled text panel on the right - colors by branch. But neither of your Python outputs produce that pattern. If I wanted to use an explicit list of colors as you did, I would connect color Swatch components to Entwine, one per branch, and be done with it, as in the purple ‘Color list’ group below.

So I created this model to illustrate the difference between ColorB (by branch), ColorJ (by item) and ‘Color list’ (Entwine). I created a grid pattern of points because it makes it easier to see how ColorB and ColorJ work when their ‘J’ (Jitter) inputs are set to zero (no jitter).

Two Value List components (blue groups) allow you to choose which color method is used and a ‘SRC’ (source) geometry, either the grid of points or your curves.

I am surprised to discover two things:

  • ColorJ has a bug that causes it to show nothing (!!) when there is only one item in a branch. I’m stunned by this (I’ve been using it for a long time) but understand it and will fix it somehow.

  • ‘Color list’ behaves as expected with the point grid: branch 0 is white, branch 1 is red, branch 2 is blue and all remaining branches are yellow because I have more branches than colors. I am baffled, however, by what I see when I use your curves: branch 0 is white, branch 1 is red and branch 2 is yellow, not blue!!?? Why is blue missing? Can anyone explain this mystery to me? (30.0 KB)

I forgot this thread , i will check it later
And i think group every branch and assign a color is a solution with regular components

This is a simplified version illustrating the mystery issue. (12.3 KB)

Same thing happens with the point grid when the number of branches is less than the number of colors. (10.3 KB)

I’m stumped? :thinking:

if you bake the Preview component and drag a rectangle selection to pick the circle-shape you’ll see you have 2 circles

Preview component gets 3 branches from Geometries and 4 branches from Material
so colour #3 and #4 are both applied to Geometry #3 which getss duplicated (they both exist, but you only see the last color applied), maybe this is an explanation?

1 Like

Thank you. It didn’t occur to me that assigning colors would have a similar impact on the data tree as a list of vectors supplied to a shorter list of points (white group), since the resulting data tree is hidden within the Preview component. I expected what I see from Merge (cyan group) but that’s wrong. (23.9 KB)

This model contains fixed versions of both ColorJ (by item) and ColorB (by branch). Both failed for the same reason when there was only one item or one branch. (33.0 KB)

NOTE: Both are based on a Colorful cluster I found on the forum, possibly by @HS_Kim?