Puzzled by iteration in GH Python components

Hi Djordje

please allow me to be very clear about what Mitch already said. The implied iteration that you see at work here is how every Grasshopper component works. Of course, it takes a bit of pain to switch from being a Grasshopper user to a Grasshopper algorithm writer, so many people struggle with this logic at first. This “puzzle” you feel is caused by the curiosity to explore an area that you do not know. Please take this with ease and allow yourself some time to get used to things. Try to tackle this with a fresh mind and not while tired, late at night. Sometimes these tricks help me.

So – the most important thing to realize is that you can write the logic for a single item (say, a single point), a whole list, or several lists together (with a tree). You should always try to write the algorithm with the simplest logic possible, and let Grasshopper match data structures for you. This data-matching algorithm is a tough one, written for you by a “bitsmith” called David Rutten. You should try to exploit that nice logic that is there – let’s not try to reinvent the wheel – and, if you are moving a point, always write the code that takes a point and a vector. Mostapha answered another user quite nicely in this discussion here on Grasshopper.

One thing that seem to obstruct the overview in this case is the visualization of prints in the Editor, when these prints happen sequentially. We could do something about this. I’ve added an item to the bugtracker, to make sure we consider doing something about the Editor print window, so that it shows in some other way several major iterations:

One consideration, additionally: do not use the print feature to return data. Use result variables instead.

In the image above here, you get a tree in (out), and list of points in (a). This is because (a) matches the same layout as the one we have for the (pts) input. But print() could be called several times in one code iteration. The matching algorithm takes that into account, and creates a one-deeper nested list (a tree).

I hope this is helpful,

Giulio

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

2 Likes