A Workaround for Iteration in Grasshopper

At some point, Grasshopper users are faced with the issue of loops. In the logic of graphical programming environments, an intuitive response would be to feed outputs of a downstream component back into an earlier part of the graph, thus completing a loop. When this is done though, components turn an angry red.

So what are the available solutions? The first is to copy and paste the part of the definition that will be re-used (figure 1). This approach works and is quick, but it becomes an impractical solution once a large number of repetitions is required. Imagine the spaghetti mess of connected components that results from 100 such cut and paste chunks. A better option is to iterate within a scripting component. This means that a single scripting component substitutes for the reused part of the definition. A user not only writes the loop structure, but must re-create all the computational logic encapsulated by the substituted components. For designers unfamiliar with programming, this may be too high a hurdle to clear, especially if the  re-used part of the definition contains many components and involves many procedural steps.

Figure 1 -"Cut and Paste" method

Figure 1 -"Cut and Paste" method

Instead I will cover another approach that allows iterations to take place in Grasshopper using several helper Python components (figure 2). This makes use of the super useful sticky dictionary. The basic idea is to store information in an entry in the sticky which is then accessible to all other python components. Dataflow management then becomes a task of accessing and adding data in the right sequence.    

There are three components that work together.  #1 creates an entry in the sticky with the key "outputs" that contains a list. #2 is responsible for initializing this list at iteration 0, and then for picking out the correct data for every subsequent iteration. #3 is responsible for adding data to "outputs" entry in the sticky. Users will interact mainly with component #2, by incrementing the loop_tick variable and setting the limit for the number of loops. The initial input geometry is re-routed through this component first. The final output of the repeated chunk (purple) is fed into #3 - this becomes the input for the next iterative step.  To step through the iterations, a  user scrolls the slider connected to #2.

Figure 2 - Python Component solution

Figure 2 - Python Component solution

There are three components that work together.  #1 creates an entry in the sticky with the key "outputs" that contains a list. #2 is responsible for initializing this list at iteration 0, and then for picking out the correct data for every subsequent iteration. #3 is responsible for adding data to "outputs" entry in the sticky. Users will interact mainly with component #2, by incrementing the loop_tick variable and setting the limit for the number of loops. The initial input geometry is re-routed through this component first. The final output of the repeated chunk (purple) is fed into #3 - this becomes the input for the next iterative step.  To step through the iterations, a  user scrolls the slider connected to #2.

Figure 3 - After 60 iterations

Figure 3 - After 60 iterations

 In this example a line is rotated a certain angle relative to a previous line. This is looped 60 times to give the result in Figure 3. An interesting opportunity arises where the rotation angle parameter can be adjusted in between steps. A user can scroll backwards and forwards in the loop and change driving parameters on the fly, gaining quick feedback. Alternatively, a timer can be used to automatically increase the loop_ticks input for #2 instead of a slider. More complex chunks of Grasshopper components can substitute for this 'rotation' placeholder.

The grasshopper definition can be downloaded here. Please note that this was written for Grasshopper version 8 and it requires you to have the Grasshopper Python component installed. 

/