designcoding
About Table of Contents Keywords Monthly Archive
Support designcoding!

Vector Class in Rhino Python

August 16, 2021 | Algorithms
#linear-algebra #rhino-python #vector

In this new series, I will be using Rhino Python to create some of the fundamental mathematical objects in Rhino. We will learn how to code in Python, and also try to get deeper into the intuition behind some of the fundamental concepts we use every day in Rhino and Grasshopper. The Vector class in Rhino Python is the starting point of this journey. Just like vectors, most of the topics are already built-in object classes in RhinoPython. But I wanted to re-create them for educational purposes. As a result, we begin with the building blocks of linear algebra: vectors. We will define vectors as objects with only one attribute (variable), which is a coordinate, indicating the tipping point of the vector. However, this is the most simplistic starting point. You can refer to Wikipedia for a more formal definition of vectors.

The Code: Vector Class in Rhino Python

import rhinoscriptsyntax as rs
class Vector:
	def __init__(self, point):
		self.components = point

Above is the first step, creating a new object class called Vector. An object class is just like a blueprint that we can develop instances from. Therefore, we will be using the Vector class many times in the future. When you enter the EditPythonScript command in Rhino, enter the above code and run it by pressing F5, you will see that it will not do anything. This is because of the fact that this code only defines the Vector object class, the blueprint. The instances created from this blueprint will be the vectors we will use later on. Then, let’s explain the above code line-by-line below:

LineExplanation
1First, retrieve the functionality (objects and methods) of Rhino inside Python. “rs” is the shorter name defined for the RhinoScriptSyntax module. “rs” is a naming convention.
2Next, define a new object class named “Vector”. Similarly, as a naming convention, the class names start with an uppercase letter. Moreover, the colon symbol (:) is an important syntax rule here.
3Inside of the newly defined class, (notice the indentation) define a new method named “__init__”. This is a special method name that is used to create new instances (we can not change it). Inside of the parentheses, there are two input values. “self” represents the current vector, and “point” represents the tipping point of the vector.
4Finally, inside of the new method, “__init__”, (notice the second indentation) we define an attribute called “components” and define its value equal to the input value of the point.

Testing the Vector Class

The below figure shows the idea behind vector components. In this case, a 2D vector is represented by two numbers. These numbers are defining the length and orientation of the vector. Note that, this representation does not store any origin point. Because here, all vectors with the same components are regarded as the same vector. We will benefit from this assumption in the future.

Vector Class in Rhino Python linear-algebra, rhino-python

You can test the Rhino Python code by creating vectors. Add this testing code at the end of the above code and run it.

v = Vector([1,1,0])
print(v.components)

This will create an instance of the Vector class and assign it in a variable “v”. While defining the instance, we entered an input of three numbers in square brackets, creating a list. As a result, the list of three numbers represents the x, y, and z coordinates of the vector’s tip point. Notice that the parentheses that are used to cover the inputs of the Vector, and the square brackets covering the list of three numbers are all closed at the end of the line. Thus, this code will run the __init__ method we’ve just defined and will use [1,1,0] as the input named point. Then, it will define the “components” attribute, and put the value of the point input to it.

In the second line, we use the built-in “print” method of Python, to display the “components” attribute of our new object. The dot (.) notation here, like “v.components” accesses such attributes (values) in a hierarchical way. This statement will get the “components” value of [1,1,0] we just entered in our object “v” and print it to the Command Bar or Rhino, and the console of Rhino Python.

Conclusion

You can continue creating different instances of the Vector class. But remember to assign different variables (instead of “v”) so that it won’t overwrite the previous instance. The variable names are totally up to you, but as a convention, you can use more informative names (not “v”). For example, you can use vector1, vector2, …etc.

To sum up, if you have no previous experience in Rhino Python, it may be difficult to follow all of the concepts explained today. Therefore, a minimum of Python support and experience would be very helpful. In the next lesson, we are going to visualize these vectors in the Rhino viewport.

Cite this post

Yazar, T. (2021, August 16). Vector Class in Rhino Python. designcoding. Retrieved August 24, 2026, from https://www.designcoding.net/vector-class-in-rhino-python/

Related Posts

Vector Projection and Angle

December 21, 2021

In this session, I will add two new methods to the Vector class. I think this will finish the basics for the vectors. In the future, we are going to need several new methods like adding multiple vectors and interpolation. But for now, I think this would be sufficient to further advance into parametric curves and surfaces. These new methods will be based on the dot product method we created in the last post. I will add the vector projection…

Vector Normalization and Dot Product

October 29, 2021

This is the continuation of the Vector class we started here, and further advanced here, here, and here. This new Rhino Python implementation is mostly educational and partially a hobby. Before this session, we have developed display, magnitude, add, multiply, reverse, and subtract methods. This time, I am adding the vector normalization and dot product methods and seeing the utilizations of the dot product. Line Explanation 1-26 Already explained in the previous posts. 27 Define a new method called “normalize”….

Vector Magnitude Method in Rhino Python

October 12, 2021

Today, I am going to make only one addition to the Vector class we recently started in Rhino Python. The magnitude of a vector can be easily calculated by assuming that the axes (2 or 3 axes) of it are perpendicular to each other. This gives us an opportunity to assume a right triangle visually, and calculate the magnitude (length) of a vector by using the Pythagorean Theorem. In short, if you square and add the components of a vector,…

More Vector Operations in Rhino Python

September 1, 2021

This is the continuation of my new project of re-creating the parametric curve and surface methods of Rhino via Python scripting. If you remember, I started with the building block of vector operations, here and here. Then, I defined vector addition and multiplication, before going deeper into the geometric calculations. In fact, they are using the previously defined addition and multiplication methods. New Vector Operations: Subtraction and Reversing In the future, we are going to need to reverse a vector,…

Vector Arithmetics in Rhino Python

August 20, 2021

Today, I am going to advance the Vector class a bit more. Firstly, I will improve the display method I introduced recently. Then, I will add two new methods which handle the fundamental vector arithmetics in Rhino Python. Improving the Display Method In the previous attempt, I displayed vectors on the origin of the Rhino viewport. The coordinates of the tail of a vector are not stored within the object attributes. Normally, I can store two coordinates, one for the…

  • Chapters

    • Algorithms
    • Discourses
    • Fabrications
    • Studios
  • Explore

    • All Keywords
    • Table of Contents
    • Monthly Archive
    • #grasshopper
    • #parametric-curve
    • #tutorial
    • #parametric-surface
    • #polyhedra
    • #bezier-curve
    • #terrain
    • #design-education
    • #simulation
    • #fourier
    • #dual
    • #art
    • #design-object
    • #image-sampler
    • #growth
    • #vector-field
    • #circle
    • #goldberg-polyhedra
    • #calculus
    • #music
  • Search

  • Support designcoding!

  • Enjoying designcoding? Support me on Patreon to keep it growing. Thank you!

  • copyright 2026 designcoding.net | about | privacy policy | end user license agreement