Flatten Data Tree (Do some operation), Then Return to Original Tree Structure

Given a data tree, it is sometimes congenial to change the data structure to do an operation, then return to the original data structure. I have been looking for a good way to do just that, and I think I have found a decent solution. However, I would like to hear any other suggestions as well.

In this example, I have 23 tree branches, each containing 6 list items (Point 3d). I want to flatten the tree, move the points around randomly, then return to the original tree structure.

My solution is to use Tree Statistics to get the paths of the original tree, then use Replace Paths to map the paths to the flattened data. There are two caveats that might not be plainly obvious:

  1. The original data and the flattened data need to be grafted. This is such that each item has its own tree path. This is because the tree statistics essentially produces paths for each branch.
  2. Tree statistics must all be provided for the flattened data(which is now grafted), because the Replace Paths requires a search mask for which paths are to be replaced. In this case, I want to replace all the paths, so I just provide the paths for every branch.

In the end, a simplify operation resolves the grafting. You can see that I do this with the path mapper in this case.

Attached is my gh file.
Flatten (do some operation) Unflatten.gh (26.8 KB)

these are some common tree-structure rebuilt methods I usually use
this particular case in your GH file is kind of an “easy one” because no branch number is missing (next branch number = previous branch number +1, starting from 0) and you have just one one layer of branches

Flatten (do some operation) Unflatten_Re.gh (29.4 KB)


Maybe I’m missing some detail of what you want here, but isn’t this exactly what the ‘Unflatten’ component is for?


I thought that is what the ShiftPaths component does but its in the realm of Advanced Tree Wizardry along with Path Mapper

Yes! I knew there must’ve been a simpler solution! Thank you Daniel.

To be honest, I didn’t know about the unflatten component.

1 Like

Thank you @inno , I’m not entirely sure when I would need to use these other solutions besides unflatten, but I think they are quite clever.

Unflatten is fine, Shift Paths is even better to know because it can be used more widely. But it needs to be said: don’t flatten unless absolutely necessary!

Flatten_2021_Jun30a.gh (22.0 KB)

I realize this may have been a contrived example but still…