How do I fix an entry with the same key already exists when renumbering paths in grasshopper?

Viewed 252

Coding in grasshopper to try to renumber my tree branches from {0}, {1}, .... to diff indices of branches {230}, {234}, .... Only the first tree is more regular in its naming, the second tree came from a bigger tree and I picked these branches as I needed to manipulate them. However, when manipulating them, I had to change my branch index from the {230}, {234} .... to that of one beginning with zero to match that of incoming data. As a result, I tried manipulating my data in python to reverse the manipulation done previously.

I tried a code in python taking in the branch indices I needed and outputting modified indices according to Rhino/Grasshopper syntax

import rhinoscriptsyntax as rs
for i in x:
    a = y.RenumberPaths("%s" %i)

Expected output of data tree with manipulated branch indices. Error: Runtime error (ArgumentException): An entry with the same key already exists.

Traceback: line 13, in script

Line 13 is just the one that says a = y.Renumber...

2 Answers

I am not familiar with the python API, but in C# I would do something like:

// inputDataTree is the tree you want to renumber, ideally you would change
// the DataTree<object> for your data type such as DataTree<Curve> or whatever.
DataTree<object> dataTree = new DataTree<object>();

for( int i = 0; i < inputDataTree.BranchCount; ++i )
{

    GH_Path path = new GH_Path(i);

    for( int j = 0; j < inputDataTree.Branch(i).Count; ++j )
    {

        // If you don't need the j index, you could compute the 
        // path in the outer loop. You can add logic to how you 
        // create branches.
        // GH_Path path = new GH_Path(i, j);

        dataTree.Add( inputDataTree.Branch(i)[j], path );

    }

}

You can use GH Python's Tree Helper:

from ghpythonlib import treehelpers as th

From there you can do th.tree_to_list(tree) to convert the tree into an array of arrays; or th.list_to_tree(list) to convert an array of arrays to a DataTree.

This will greatly simplify your life, since working with datatrees in python is a little bit of a pain...

Source: https://developer.rhino3d.com/guides/rhinopython/grasshopper-datatrees-and-python/

Related