From the course: Grasshopper Essential Training (2017)

Iteration and recursion

From the course: Grasshopper Essential Training (2017)

Start my 1-month free trial

Iteration and recursion

- [Instructor] In this video, I'd like to talk about some of the specific ways that data flows through a Grasshopper sketch and some of the ways that it's similar and some of the ways that it's different from other styles of programming. So I've got a blank Grasshopper file open right now, and in Rhino I've got the file simple_fractal.3dm open, and you can see here I have this shape and we're going to work on making a Grasshopper definition that could generate a form like this so this is a very simple fractal where we start with an initial line, we duplicate that line making it a little bit shorter and we rotate it by a given angle around the line's starting point. You can see we do that again and again and again, so each successive line is a little bit shorter than the one before it and it's rotated by the same angle. So let's think about how we might start setting this up in Grasshopper and that's going to help us illustrate a couple of unique ways that data flows through a sketch. So I'm going to start with a line parameter and I'll just go ahead and right click and I'll set one line, so I'll come down here a little bit farther. I'm actually going to set this right at the origin so I want to turn my grid snap on. So I'll set it at zero and the end somewhere over here. So remember the two steps that we want to apply to all of our successive lines are we want to scale down and we want to rotate, so I'll do the scale first. So our geometry is going to come from that line parameter. The center I'm going to leave on the default of the origin, and for the scale factor, rather than .5 I'm going to plug in a different number so I'll just use a panel, so I'll say .95. So each line will be almost as long as the one before it, and that just plugs right into the factor. So you can see now I have two lines that are right on top of each other, I have the initial line that's coming through the line parameter and then the slightly shorter line that's coming out of the scale component. So that takes care of our first step. Let's go onto the second step of rotating. So I want the rotate component where I rotate an object in a plane. Give myself a little bit more room here. So my geometry for the rotate is going to be the geometry output of the scale component. I also need to specify an angle. Go ahead and do that with a panel and let's just say something small like 0.2. So that plugs into my angle input. And lastly I'm just going to right click on this plane input and what I'd like to do here is just extract that parameter and that allows me to disable preview and get rid of this little grid pattern that we see in Rhino. And I can go ahead and right click and disable preview on my scale. So the rotate component gives me the final stage of this first step in my recursive pattern. Now that gets us one step into the finished product here, if I look back up at what I'm going for, I need many many many more versions of that. Now you might think very logically that you could just take the output of this rotate and plug it right back into scale and you'd just get a loop and you'd keep making more and more lines that are smaller and continuously rotated, that would be a very logical thing to do so let's try that and see what happens. Oops and you can see when I do that I get this error message here. Recursive data stream found, this component depends on itself. So this kind of an operation is not actually possible in Grasshopper as it exists right out of the box. Data has to flow from the left side of your definition to the right side, it can't actually loop back around. So that's kind of one of the big differences that you'll see when you're working in Grasshopper versus working with other programming languages. So the way that we could try to tackle this is to maybe let's just duplicate all of these steps. Alright so I could just chain together that output, and I could do that again and again and again and keep going and going and going. You'll start to see though I'm building up a lot of components here just for a few steps that should be pretty simple, really because I've already set up the structure here I just need to repeat these. So this is one of the biggest hitches that people start to run into when you're working with Grasshopper is that recursion doesn't work the same way that it would in other programming languages. Now there's a couple ways we could get around this if we wanted to. One is we could use a script component. So under the Math tab you can find several different options for script components and we're going to cover some of those in the customizing Grasshopper chapter and we'll look at some ways to use scripting languages along with Grasshopper to get recursion. The other way to do it is with a plugin, so I'm going to switch to my browser and I'm bringing up this site food4rhino.com, and this is a great resource for lots of apps and plugins that go along with Rhino and Grasshopper, so an additional option that we have for building recursion into our sketch is to use a plugin like this one called HOOPSNAKE. So on the food4Rhino site we could download that plugin and Grasshopper plugins and how to install them are another topic that we'll cover in the Customizing Grasshopper chapter. So that's just a quick introduction to some of those key components of data flow and recursion when working in Grasshopper.

Contents