Introduction To Python In Rhino

David Tracy

Level 1

3h 23m

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.

Introduction and Core Concepts (2h 3m 53s)

Course Videos
Duration
Viewed

Hi, I'm David Tracy and today we're going to learn how to use the Python programming language with Rhino to automate tasks, create complex forms, and simulate physical phenomena. The ability to write code for creative applications has become increasingly important for the designer and extends beyond just architectural applications. This course will serve as a gentle introduction to programming to unlock capabilities for generative computational design with Rhino, Python, and Grasshopper.

So once you, at first you'll see it, and you'll be like no that doesn't read just like English, but after awhile of getting used to it, you'll see that there is kind of like very easy flowing syntax with the language that almost makes it feel like English. And it's also really powerful. Because of its popularity, there are thousands of libraries that extend the functionality of Python.

Another thing to keep in mind about writing code is that code is executed line by line. So if this is my first line of code here, the compiler will evaluate this line of code. And once it does that it will move on to the next one.

And what that's gonna do is that's gonna open the Rhino Python editor and you have a few things here to pay attention to. If you've used Windows applications, this should all be familiar to you. But on the left hand side of the screen, there is this window that has some drop-down menus which hold all of the built in classes or, kind of like, sub-programs, for Python in Rhino.

3m 56s
Log in

And so Codeacademy is a great interactive tool and resource that's free for learning code, and there is a pretty solid Python course here. And so the way that Codeacademy works is that it has a window on the left side of the screen that kind of gives you some text-based instruction, and then it has an in-browser code editor, and you can save and submit your code. And when you successfully execute your code, it will move you on to the next lesson.

Some of the course materials that we're gonna be going over today are going to be hosted on Black Spectacles and others are on my GitHub account which is David P. Tracy. So you can navigate to GitHub.com David P.

4m 26s
Log in

For instance, if I open this sample script called draw parametric curve, there are like 52 lines of code here, but I can just if I want to see what this does I can start debugging it, and it's going to prompt me for parameter value T, and I'll accept the default. When you're prompted with messages and you see these two carat brackets, the value in between those two brackets is the default, so if I just hit enter or space it's going to accept zero as the default, and now it's prompting me for the maximum parameter value, so I'm just going to hit enter, again to accept the default, and then it's going to prompt me for the number of points, so I'm going to drop this down to 25. And so what that did was it generated this spiral curve so if I turn on the points here, you'll see I have about 25 points.

So we're just going to write hello world. And when we do that, we'll see our message pops up in our debug console and in the Rhino window, and we can put as many of these messages as we want. Okay, and so you'll see it goes line by line and prints, uses this print function, to output a message to the console.

6m 52s
Log in

What we're gonna do is we're going to print my variable, plus my second variable. When we do that, we come out with our sum, which is 14. I can change my original value here.

So, we can create, we can just type in the sphere command, it's gonna prompt us for a couple... We'll call them arguments. So, first thing it's gonna ask me for is the center of my sphere.

So we're gonna say select and object. Okay and so I'm going to delete this line here, I'm going to run my function and now you'll notice on the command line up here, it's going to ask us to select an object. Okay so that's great, but we didn't do anything with that, but one thing we can do is we can use this getobject function to set a variable.

So when I run this it's going to prompt me for select an object and we have another error here and that's because it's still trying to run these two functions that have my base point in them so I'm just gonna comment those out really quickly. So I'm gonna select my object. When I select this object it prints out the object ID, which isn't entirely helpful for us right now, but it will be helpful for us later when we wanna do things with this object.

So you can have numbers in your variable but the first character of the variable can't be a number. So now that we've got that resolved, I'm going to run my script. It asked me to select an object.

But if you need to search for extra documentation the Rhino Python reference on rhino three.com is another good resource and you can search through it to search for functions and have like another place to reference what these methods do.

So, we're going to just accept the default argument of degree three for our curve, and now we're gonna run our function. So, what the get points method does is it prompts me to specify a series of points on the screen and it's storing them in a list which we're calling points and points is then used by this add curve function. So, I can run that again.

Okay, so what that did was it ran through all of our methods, so it added the line, it added the curve, it added the circle and then it created our rectangle at this base plane, zero, zero, zero with a width of 25 and a height of 50.

So just as we did before, we can add a sphere, and this is very similar to the circle command it's just asking us for a base point and a radius. So we can just even copy our arguments from our circle command above. And we can run this.

6m 16s
Log in

So remember in our first example here we have this very basic vector one one and to make it negative we multiply both of its components by negative one. So if we wanted to subtract V2 from V1 we take the negative value of V1 and we go and we add them end to end. So first we have V1 and we're subtracting V2 from it so we go over one zero then we go down minus one minus one.

So if I create, If I create a variable called My Vector and set that equal to one, one, zero and starting at zero, zero, zero, let's call this My Vector Start, and then let's copy and paste this. My Vector, and then let's change the name of this first one to My Vector End. So, we'll make this zero, zero, zero.

3m 34s
Log in

So, it's built into Rhino script and we're just going to select that and this is going to ask us for two things. It's going to ask us for an object I.D. and a translation.

5m 48s
Log in

So, while we can say count is less than 100, and we'll create a variable called count equals zero, while count is less than 100 and we use the colon to create the scope for this while loop, we're going to do something so the first thing we're going to do is we're gonna rotate this object. So we already have this prompt to rotate the object so we know it takes a shape, a point, and a degree of rotation. So we're gonna just do this over and over again.

9m 57s
Log in

So we're going to now run this again so if I hit play and we need to import RhinoScript syntax as RS, so if I run this again it's just going to create this random point 500 times. So now I have 500 points that I just selected. So the reason that's happening is because all of my x, y, and z coordinates are being set outside of my while loop.

1m 35s
Log in

So if I type inBoolean here and add this to my list, this is really just either a true or false value, 0 or 1. So-So as I mentioned, there can only ever be two possible values for a Boolean. And the way that we write that in Python-Let's get our script editor back up here.

For for instance, the way that we would test to see if A is true, is we can write, "If A is true, "print the truth." So with an if statement, there are couple ways to expand on our if statements. "Else if A is not true, "print "'It was all a lie'". So right now, A is set to true, so if I run this script...

9m 2s
Log in

And first, we're going to start by creating this variable called points, and it's just going to be an empty list, and the way that we create a list is just by including these two square brackets. So, if I go back into my, my Rhino viewport, remember this can be considered a list, so this is the index of the list, and these are the values of the list. So in this case, I have one, five, seven, eight, 12 and so on.

7m 14s
Log in

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.

3m 57s
Log in

But for now we're just gonna leave this as an argument list function. So what we're gonna do is we're going to generate some random values. So I'm just gonna copy.

Classes and Object Oriented Programming (59m 51s)

Course Videos
Duration
Viewed

For instance, in our math class, when we import math, so in our math class we have access to things like, sine, cosine, and tangent. Things like that so these are a few of many, many methods that are included in this math class. So we're gonna start writing our own classes and so what this is useful for is we can start creating elements that have built-in functions and intelligence of their own.

4m 7s
Log in

So in the update function, this is where we're gonna do our vector math. So we're gonna do our vector math here, and then in display we're gonna draw something. And so in run, we're gonna call these methods.

So, we're just going to give it a start point of zero zero zero, or better yet, just to review some of the vector stuff we did, we're gonna use the RhinoScript vector create method. And we're gonna generate this from two points. We're gonna have the start at zero zero and then we're gonna put it up at 20.

2m 59s
Log in

We're creating the particle with a start point of zero, zero, 20, and that's a vector. Then we're also going to give it an initial velocity. What we'll do is we're gonna create another vector and this is just going to be a series of random points.

So, what I'm gonna do is I'm going to create a list, an empty list, and I'm going to loop through, I'm gonna create another loop, for i in range. Let's just create 10 different particles. I'm gonna create a series of particles.

3m 48s
Log in

So for, we're gonna have 100 steps and then for P in our list particles we're gonna run every particle. So let's just take a look at what this looks like. Okay, so I think my initial velocity is still quite high because these aren't really ever overtaken by gravity.

10m 21s
Log in

And this is just gonna return, and this is just gonna be called getlocation, and it's just gonna take a reference to itself, and we're going to create a class variable. We're gonna change shape into a class variable, self.shape, and so we're going to return, again, self.shape. So, in addition to drawing the point onto the screen, we're also going to add it to a list.

So, the first thing I'm gonna do is, because my parameter counter is at first equal to zero, I'm going to set parameters dot append and I'm gonna just add my parameter counter because I'm adding a number to my list at point zero zero and then, what I want to do is I want to iterate on that value, that parameter counter value so, that it only goes from zero to one through my whole list of points. One way I can do that is I can iterate on my parameter counter by one over the length of my list. So, basically, I'm incrementing on my parameter counter by some fractional value that's proportional to the size of my list.

11m 35s
Log in

So we're just gonna do a four loop, so for I in range 100, we're gonna do particle system dot apply force, and we're gonna apply wind particle system apply force gravity, we're gonna do I'm sorry particle system one and particle system two dot apply force wind, particle system two dot apply force gravity and then we'll add the particles so we're gonna add a particle for each location so particle system one dot add particle and that's just gonna create a particle at the current location, particle system two add particle and then we're gonna run them. Hit run particle system two dot run. Okay.

Grasshopper Integration (18m 27s)

Course Videos
Duration
Viewed

So with kind of this foundation that we've formed in the Python editor and Rhino, we're gonna move some of those skills into, we're gonna migrate some of those skills into Grasshopper. And before we can do that though, we need to add the Python component to Grasshopper, so we're going to go and search for Python component, Grasshopper, and the top hit is gonna be Food4Rhino, and the component is called GhPython. And what this is gonna do is this is gonna add a Python editor component to Grasshopper.

Okay and we'll just remove my input and all this is going to do, let's call this Steps. Okay and then, I'm gonna open up my Python Script Editor and let's just go to, if we open up our random Walker class, we can start copying some of this stuff. So, I'm going to first do import RhinoScript syntax as RS.

Thanks for watching and remember to check out the other courses that we're always updating on blackspectacles.com.

icon: hand holding tablet

Get licensed faster. Become a member now.

Choose your subscription