Grasshopper

algorithmic modeling for Rhino

Hi All,

I have written a Python script which subdivides a polygon, and then outputs the new polygons as a data tree, in which each branch is a list of points. I then want to feed this straight into the input of another python script which will then subdivide each polygon again. However, something seems to be going wrong.

In the example below, I am generating 7 polygons in the first script. I then extract just one branch from the data tree, and send it for the second round of subdivision which works great:

However, when I send all of the branches to the second script, it seems to run it 7 times on EVERY point in the data tree, which creates a mess:

Can anybody help me to solve this? I need the second python script to run seven times once, once on each branch, but only using the points from the applicable branch!

Any help would be much apprciated

Ben

Views: 1681

Replies to This Discussion

Hello Ben,

Well without the actual script its hard to tell, what goes wrong.

My guess though is that your data managent in the component only works for a provided list of objects, and not a data tree. Getting one branch of the datatree means getting a list.

You have to go through how you interate your first input in your second component.

Best,

M.

Thanks for your fast reply guys,

The python scripts are too long and laborious to post in full, but here's the essence:

I'm basically running through the list of input points as though it were a single list, and generating a list-of-list of the new polygons. I'm then using the nestedListToDataTree function to convert this to a data tree, which I output back into grasshopper.

So my data management is definately only set up to work for a provided list of objects. Should I somehow tell Python to expect an entire data-tree and somehow extract each individual branch from it within the script?

Let me know if you need more info,

Thanks again!

Ben

Hi again Ben

we need more info. Could you please post a simplified version of your script (where maybe the script prints "Hello" for each input that you need?

Thanks

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

Thanks Giulio,

I've recreated my problem in a very basic script attached here. Each polygon is very simply divided into two, and the points for each half are stored as a list of points within a list of polygons, converted to a data tree.

I think the problem is that each time the Python script runs on a branch it is outputting a list of a list into a data tree format:

[

    [polyA-pt1, polyA-pt2, polyA-pt3] ,

    [polyB-pt1, polyB-pt2, polyB-pt3] ,

]

This means that when the python script is run multiple time on multiple polygons it has to output a list of a list of a list into a data tree:

[

   [

       [polyA-pt1, polyA-pt2, polyA-pt3] ,

       [polyB-pt1, polyB-pt2, polyB-pt3] ,

   ] ,

   [

       [polyD-pt1, polyD-pt2, polyD-pt3] ,

       [polyE-pt1, polyE-pt2, polyE-pt3]

   ]

]

...and this is where the problem seems to be occurring, as in my script the final data tree seems not to have the correct number of levels, and the points are all present but exist on too few branches.

Any help would be much appreciated!

Thanks, Ben

Attachments:

Ben,

I think you should clear out the differences between nested lists and data trees. I know its difficult when you are used to work with arrays but its essential to get the best out of GH. 

Try the following:

import clr
clr.AddReference("Grasshopper")
from Grasshopper.Kernel.Data import GH_Path
from Grasshopper import DataTree

def iterateinput(t):
d1 = DataTree[object]()
for i in range(0,t.BranchCount-1):
polys = t.Branch(i)
poly1 = [polys[0],polys[1],polys[2]]
d1.AddRange(poly1,GH_Path(i,0))
poly1 = [polys[2],polys[3],polys[0]]
d1.AddRange(poly2,GH_Path(i,1))
return d1

outputPolys = iterateinput(inputPoly)

Best,

M.

Attachments:

In addition to what Marios wrote:

you should usually not create a DataTree with a List input. The Grasshopper SDK expects either a full tree, or will have to merge those trees in an unpredictable way (which final path is the 'right' path?)

I would really rather ask: what do you want to do in the second script? If you need to handle all inputs together (you need all polygons) there is no way other than setting input as 'Tree Access'.

If handling a list of points (watch out: panels transform anything to text!) is enough, then from a list, set the output to a list, or a single item. There is no other easy way otherwise to predict the right tree indentation (you could, in some scenarios, use ghenv.Component.RunCount but I strongly discourage that).

Please note: your second script is currently running 3 times, in order to account for the 3 lists that you are passing in.

Does it make sense?

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

Hi Guys,

Thanks a lot for your help, I totally get it now. For some reason I was overlooking the fact that the script needs to input and output a data tree. I added a new layer to the logic to iterate through the tree branches, and set up my output to make a data tree from a double nested loop, and now all is good.

Thanks again!

Ben

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