Grasshopper PythonList-Datatree Conversion

Free conversion between python-list and datatree in grasshopper.
Chen, 2017-04-05

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

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])):
        #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] )
    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: