Grasshopper

algorithmic modeling for Rhino

Hi guys!

Can anybody show me a quick example how to iterate a grasshopper tree in python, i need to read the first item of every branches going into the component?

i know Guilio have an example in the forum, but i am unable to open it(properly because it was written in an older version of python)

thanks!

Views: 7994

Replies to This Discussion

Hi Thiru,

Check out the attached file (using GH 0.80066 and GhPython 0.5.0.0). The trick is to set the input to "Tree Access" and then use the tree methods from the Grasshopper SDK. You don't need to import Grasshopper just for reading a tree, you do however if you want to write to one. Here's the code in case you can't open the file (input is called "tree" and output is called "firstItems"):

firstItems = []

for i in range(tree.BranchCount):

    branchList = tree.Branch(i)

    firstItems.append(branchList[0])

Kører det ellers i London?

Attachments:

Hi, Anders, I found ur reply and gh file and tested it, it worked in my grasshopper, nothing changed. However when I tried to write my own script using ur method, the editor always tells me, tree is not defined. So I am really confused, what's so different about ur file? Please help me with this, I am eager to find out y. Thx very much!!!!

Sounds like you are probably not importing the Grasshopper .dll assembly, or, that your code expects only the Grasshopper.DataTree class to be imported under the variable name "tree". See this reply over on the Rhino.Python forum for more info on the subject. It also explains defining DataTree types which will probably be useful to you.

Edit I: Sorry, thought you needed to write a DataTree. My bad. I currently have no access to Rhino/GH so I can't check out your file.

Edit II: However, try checking that the parameter input you want to read is actually set to "Tree Access" first.

Hi, Anders, thx for ur reply, i found out the real problem was after I changed the input into 'tree' on the component, the script doesn't recognize the change, it still thinks x was the name, that's y it tell me"tree is not defined". All i have to do is to press the test button inside the script editor. Then it works! And it turns out the name doesn't matter. :)

So as I understand, since the python script can read Daratree, that means the "tree" is already defined as a class in the ghpy module.  If so, why do we have to load grasshopper.dll assembly in order to write tree? 

And in fact, do i import the Grasshopper .dll assembly? can u kindly elaborate to a beginner like me? Thank u very much!

Thanks alot Anders, tree access was the trick!

Det gaar skam super, ret meget arbejde for tiden, arbejder med at generere nogle skyscraper i kina. . Hvad med dig i stockholm, Aedes right? lyder pretty awesome?

Actually, here's an even simpler method using the "for item in sequence" style looping and list comprehension:

firstItems = [b[0] for b in tree.Branches]

Lyder fremragende, vi må lige stikke hovederne sammen næste gang jeg er i London og dele nørderier :)

long live list operations in python!


ja for sulen! gi endelig lyd..eller glemme norderier til nogle bajere haha..!

Det gør jeg, så flækker vi London town 9000 stylee! Kommer nok i begyndelsen af December, smider en besked når vi nærmer os i tid og rum.. Happy skyscraping ;)

Lyder som et hit. ja endelig gi lyd!

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service