How to add an index at the beginning of the path of a tree?

E.g. given a data tree with a path structure like {A}, {A;B} or {A;B;C}…, I want to remap it to a new path like {0;A}, {0;A;B}, and {0;A;B;C} … i.e., adding a specific index number such as “zero”, or “one”, etc, at the beginning of the path of each branch.

Or, put it in another way, I want “graft” a data tree, keeping its current structure, but put the newly added index to the begining, rather than the end, of the path, and assign an arbitrary number as this new index.

I’m not sure how to do this via path mapper because it needs to explicitly specify the data structure of the original tree which may vary depending on the upstream data.

Appreciate your kind help.


ReplacePaths.gh (9.5 KB)

private void RunScript(DataTree<object> dt, int index, ref object A)
{
  var output = new DataTree<object>();
  for (var i = 0; i < dt.Paths.Count; i++)
    output.AddRange(dt.Branches[i], dt.Paths[i].PrependElement(index));
  A = output;
}


ReplacePaths.gh (4.4 KB)

2 Likes

Thank you very much, @Mahdiyar

However, when I try to recreate your solution step-by-step, i got the following error as shown in the image below.

It seems the error is caused by the connection between the deconstruct path component and the replace paths component.

As shown below, in your solution the connection between them is a continuous hollowed line, whereas in my re-recreation of the workflow, the connection is a dashed hollowed line.

I’m not sure the cause of this discrapency, and I’d like to ask if you can explain how this type of connection in contiuous hollowed line is created and what is its impact on the data structure.

Thank you, again.

ReplacePaths_Mahdiyar_solution.gh (23.2 KB)

S input of Replace Paths must be connected to P output of Tree Statistics:
ReplacePaths

2 Likes

opsi… :sweat_smile: my bad, for not seeing the overlap…

Thanks for the clarification.

1 Like