Vector Addition, Subtraction, and Importing Math

4m 12s

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.

When we're working with Vectors in Rhino, there are a couple gotchas to keep in mind. So, I'm going to put my import statement here. Import Rhino Script Syntex as RS and when I type in Vector I get a preview of all the functions that exist here. So, there are a handful of built-in methods, and including Vector Create. So, one thing that's important to remember, or to keep in mind, because this screws me up all the time, is if you look at the arguments that are required for Vector Create. You'll notice that it puts the two point or the kind of like target of the Vector first and not the origin.

So, when you're, when you're creating a point in Rhino, or a line rather.

It asks you for a start point and an end point. But with the Vector Create method it asks you for the end point and the start point first. So that can be, that can be confusing. Just keep that in mind. There's also methods built into Rhino Script Syntex to add to Vectors. 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. So if we pass these two Vectors in to this Vector add method we can, so let's put this, My Vector, start, my Vector end. It's gonna add those two Vectors and we can just print that out to see what the output is.

And we see the result in Vector as one, one, zero. So if we were to change this, let's make this two, zero, zero. Let's run this again. We can see that it's combining the components. Two in one. One in zero. Zero in zero. Likewise we can flip, there is no Vector subtract method. Actually let me make sure. Oh there is a Vector subtract method. So we can use the Vector subtract method, or we can use the Vector add method and just switch the sign of our second Vector to do subtraction.

But there is Vector subtract method built into Rhino Script. Another thing that will be important for us at some point is to be able to use Trig functions. Like Sine, Cosine, Tangent. And those are all available to us through the math class.

So we can import math simply by typing it. If you want you can import math as M, but math is kind of legible enough and easy enough to type that we can just use it on the fly. So, when we type math and put a period we can see we have access to a lot of methods. Square root, sine, radians, conversion to degrees. These are helpful methods for us to use. But we can use things like a sine function to do either radial coordinates, or just to do oscillation with our script.

Log in to access files

From the course:
Introduction To Python In Rhino

Level 1

Duration: 3h 23m

Author: David Tracy