Class Methods

2m 59s

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.

What we're gonna do is we're gonna give this particle some behaviors of its own. We're gonna use some vectors. We're gonna actually get rid of this argument that we're gonna pass at a single start point. But we're gonna give it some random velocities to start with, and this will make sense in a second. What we want to happen is we want our particle that we generate to have an initial velocity and acceleration that affects its position. We're gonna show that on the screen.

So if we generate a few class variables, we can start to do some interesting things. The way that you define class variables inside an object is you use the self keyword. This marks the variable so that it belongs just to this object. So the first thing we're gonna do is we're gonna set the location. That's gonna be equal to this start point that we just passed 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 actually, I'm just gonna copy this statement from before and I'm going to create X, Y, and Z. So I'm going to create this vector X, Y, Z that starts from zero, zero, zero.

So that's gonna be my velocity. I should just add the self keyword to all of these. Self dot Z. The last thing I wanna do is I wanna set an acceleration. This is gonna simulate the force of gravity. So I'm gonna create yet another vector.

This one is gonna be actually a pretty small force, so I'm gonna do zero, zero, and then I'm gonna give it a negative .005 acceleration. So that's always gonna be accelerating down and it's gonna get faster and faster. Zero, zero, zero. Okay? Let's run this. Global name X is not defined, so I'm just gonna, to access this variable, I'm just gonna add these self keywords.

Self.Y, self.Z. Okay. Now, if I run this, it successfully ran. So nothing happened, but we know that our script compiles and runs successfully.

Log in to access files

From the course:
Introduction To Python In Rhino

Level 1

Duration: 3h 23m

Author: David Tracy