In this course, David Tracy will teach you how to use the Python programming language with Rhino to automate tasks, create complex forms, and simulate physical phenomena. This course will serve as a gentle introduction to programming to unlock capabilities for generative computational design with Rhino, Python, and Grasshopper.
First, you will be introduced to the core concepts of using Python in Rhino, such as drawing shapes with code, transformation, and variables. Then you will learn how to use classes and object oriented programming. Finally, you will learn how to integrate Grasshopper into your Python and Rhino workflow.
When we’re done, you will understand the core concepts of programming with Python, be able to author your own scripts, and create custom components in Grasshopper.
Okay, so we're gonna put what we just learned to some good use and we're going to generate a series of random movements through our view port. And this is an exercise that Daniel Shiffman uses in The Nature of Code in the Introduction to introduce some concepts around object-oriented programming, which we're gonna get to in a second. But we're going to write a simple script that gives us some generative random behavior and we'll call it a walker, so they're going to walk. This random position is going to kind of move through our view port.
We'll start by creating a comment at the top of our script. We'll call this random walker 1.0. (keys clicking on keyboard) Flex May 2016. We're gonna do our import statements. Import rhino script syntax is rs and we're also going to import random. So the first thing we wanna do is, we're gonna wanna remember the location of our last position and we're also gonna wanna set a starting location.
So at first what we'll do, is we're gonna set location equal to a list of points, zero, zero, zero. So this means that our walker is just going to start at our drawings origin. Then we're going to use a for loop to repeat our random steps 500 times. And so we're gonna use kinda arbitrary iterator.
So for i in range 500, this is gonna execute 500 times, we're going to generate a random coordinate. So remember we can generate a uniform distribution of randomness by using the uniform function. And we're just gonna set the range from negative one to one. And then we're going to set Y, we're gonna use a uniform distribution for Y as well.
And set that uniform to negative one to one. Okay and then we're gonna create a variable here, so we're gonna say point equals rs dot add point. And what we wanna do is actually add this X component to our original position, because this is going to update. So what we're gonna do is we're gonna access the components of our location list and add them with these randomly generated coordinates.
So if you remember from the last section, the way we do that is we select the index of the list that we wanna access. So our X coordinate of our location is index zero. So we're gonna add that with X and then we're going to do the same thing for our Y coordinate. So location one plus Y. And then we're just gonna do this on the X, Y plane. And then we're going to set our location.
We're gonna update our location to equal this point. And then we can also add these points to a list. So we're gonna create an empty list here called points. And we're going to add our point, so we're going to use points dot append points. So we're gonna add this new position to our list of points and then we're gonna update the location of our walker.
And so when we run it, it should look... Whoops. Let's do this, point. (keys clicking on keyboard) Because location is set as a list and point is set as an object, we're actually just going to copy our operation from above and update our location.
So these two lines match and in fact, I'll put them right next to each other. So now our location will get updated and the point will be added to this list. So now we have this kind of like randomly moving element through our screen and if we look at it a little bit closely, a little more closely, it doesn't look like much, but as we saw it animate, we saw that it was kinda moving, kinda rapidly moving through our view port.
So now that we have this points list, we can do other things with it. So for instance we can do, we can run our add curve method and pass it that list of points. So if I run this script again, and actually I'll go through and delete all these points. It's going to create, first all these points, and then it's going to run a curve through all of those things.
So now I have this kind of like crazy, squiggly line that randomly walks with my points. I can also run some other methods on this list of points. So I can run add polyline. And I can also pass it that same list of points. So if I go back into rhino and I do a sel the last command, select last, I can delete that.
And if run my script again and I'm walking outside of where I was looking. Now you'll see that in addition to the degree three curve that I drew, I also have some straight lines. So one last thing with this random walker that will take a look at, is we'll extend this to the third dimension. So we're just gonna add another variable for Z. And we're gonna make another uniform distribution of negative one to one.
And we're going to add our location to Z and we'll just, we'll copy this to both lines. And so now if we go and select last in my view port and just delete that. I'm gonna run this script again.
So it looks like it's doing a similar thing, but if I go into three dimensions you can see that it's this kind of crazy, like wandering, gnarled sculptural form, which kinda looks cool. So we have our curve and our polyline and it's in three dimensions. And we're starting to get some interesting geometry out of this and it's still just all randomly generated.
So if I were to run this again, we would get another set of random geometries. This one takes a totally different path. So you see this one kind of reached out and grew a little deeper into the space.
Log in to access files
From the course:
Introduction To Python In Rhino
Duration: 3h 23m
Author: David Tracy