Grasshopper

algorithmic modeling for Rhino

I am having trouble converting multi dimensional python lists to a data tree

the following works

def PyListToDataTree(PyList):

    result = DataTree[object]()

    for i in range(len(PyList)):

        temp = []

        for j in range(len(PyList[i])):

                temp.append(PyList[i][j])

        path = GH_Path(i)

        result.AddRange(temp, path)

    return result

but how would one modify this so it can handle 3D and 4D lists 

Views: 1149

Replies to This Discussion

You're going to have to make more complicated paths. Instead of GH_Path(i), you'll have to append each list using a path like GH_Path(i,j,k)

--

David Rutten

david@mcneel.com

Thx for the quick reply David

I understand I will need to specify path = GH_path(i,j,k) for a 3D array. However i am not clear on how i should iterate through the python lists and add the path's. Ive tried the following but the output is incorrect

def PyListToDataTree(PyList):

    result = DataTree[object]()

    for in range(len(PyList)):

        for in range(len(PyList[i])):

            temp = []

            for in range(len(PyList[i][j])):

                temp.append(PyList[i][j][k])

            path = GH_Path(i,j,k)

            result.AddRange(temp, path)

    return result

Looks correct to me -at least in principle-. Python isn't my strong suit though so I shan't comment too much.

How is the output incorrect?

--

David Rutten

david@mcneel.com

Try this one:

def PyListToDataTree(PyList):

    result = DataTree[object]()

    for i, listsOfLists in enumerate(PyList):

        for j , otherList in enumerate(listsOfLists):

            for k, finalList in enumerate(otherList):

                     path = GH_Path(i,j,k)

                     result.AddRange(finalList, path)

    return result

Cheers, works like a charm

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service