Grasshopper

algorithmic modeling for Rhino

Parse Data tree branches into 2d array or their own array : python

I'm working with a simple data tree, in python.

the data tree is just two points on their own branch. currently I have a single python array and when I input my data tree and .append the list both branches are places in the first index of the list. so mylist[0] -> both points, as I operate on these points to grow the array, this continues, and when I feed the array out of python, the GH branch structure is preserved. 

I would like to compare the branches to one another within python. I need to understand how to break up the input data tree within the code, and assign them to their own array.

Even better would be to create a 2D array from the data tree, assigning each branch a row and each branch item to a column.

any explanation or reference is appreciated.  

this is my initial assignment code. 

userIn = x
pt = rs.AddPoint(userIn)
pts = []
pts.append(pt)

Views: 3484

Replies to This Discussion

This might be a simpler way to ask the question

I am a trying to understand how python deals with GH data. 

If I have a data tree with two branches, each containing 4 items as a input to a python component. What code is required to translate this into a 2D array with 2 rows and 4 columns? Does the type of data stored in the Items matter? If data types matter, lets assume that we are dealing with points. 
If anyone knows of a reference that explains this or is willing to explains it would be much appreciated. 

seems i figured it out

import Rhino
#import grasshopper classes
import clr
clr.AddReference("Grasshopper")
from Grasshopper.Kernel.Data import GH_Path
from Grasshopper import DataTree


newlist = {}
newlist = x[:][:]
print newlist
print newlist[:][:]
A = newlist[:][:]

Hi Nico


just make sure that you know what you are doing with this method, as Grasshopper datatrees contain slightly more information than simple lists of lists. For example, they can have gaps and the only way to assure that you are going through them as they are precisely intended is to explicitly go by each path, one at the time. See here for an example.

Btw, in the last release of ghPython, there is no need to add import clr / clr.AddReference()

I hope this helps,

Giulio
--

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi Nico,

You can read the branches with a code similar to this. You need to set the input access to "tree access":

x.Branch(branchNumber)

In your case it should be something similar to this:

ptList1 = pts.Branch(0)

ptList2 = pts.Branch(1)

Hello, I am a bit confused here. When I read a branch with this method it outputs the type List[Object] and I cannot use the standard Python list methods on it like slice, pop, append. How do I cast this list into a regular Python list? Thanks.

Can't you just take the items from List[object] and append them to a regular python list?

someList = x.Branch(1)    #List[Object]

pythonList = [item for item in someList]

The Python list() function will also do the trick:

someList = x.Branch(1)    #List[Object]

pythonList = list(someList)

KEYWORDS: how to turn a Grasshopper tree into a Python list life-saving best answer ever.

Yes - that is a .Net list, because Grasshopper is written in .Net so its SDK uses those classes. You can do as Djordje proposes if you prefer to get a normal Python list.

Thanks,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service