Grasshopper PythonList-Datatree Conversion

It’s always been a problem to transfer python list (specially nested lists) into grasshopper, which usually shows as: IronPython.Runtime.List

By using a piece of code in python inside grasshopper, you can easily transform python list into grasshopper DataTree and vice versa.

Here is the code:

import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
from clr import AddReference as addr
addr("Grasshopper")
from System import Object
from Grasshopper import DataTree
from Grasshopper.Kernel.Data import GH_Path
def raggedListToDataTree(raggedList):
rl = raggedList
result = DataTree[object]()
for i in range(len(rl)):
temp = []
for j in range(len(rl[i])):
temp.append(rl[i][j])
#print i, " - ",temp
path = GH_Path(i)
result.AddRange(temp, path)
return result
def dataTreeToList(aTree):
theList = []
for i in range(aTree.BranchCount ):
thisListPart = []
thisBranch = aTree.Branch(i)
for j in range(len(thisBranch)):
thisListPart.append( thisBranch[j] )
theList.append(thisListPart)
return theList

To make it simpler, I’ve made two customized python user component to do the job. These components are based on scripts from Giulio Piacentino.

Below shows a basic example of using it:

The components can be downloaded here: