designcoding
Search
About Table of Contents Keywords Support designcoding!

Vector Class in Rhino Python

August 16, 2021 | Views: 4926
Design Mathematics | Linear Algebra | Rhino Python
object classes | 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 components

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.

  • Search

  • Categories

    • Education
      • Basic Design
      • Design Geometry
      • Design Mathematics
      • Digital Fabrication
      • Parametric Modeling
      • Tutorials
    • Philosophy
      • Analytical Tradition
      • Phenomenology
    • Practice
      • 3D Models
      • Projects
      • Publications
      • Workshops
    • Research
      • 3D Printing
      • Building Facade
      • Calculus
      • Climate Analysis
      • Compass Constructions
      • Computational Geometry
      • Curves
      • Decorative Arts
      • Digital Fabrication
      • Evolutionary Solvers
      • Folding Structures
      • Fractals
      • Graph Theory
      • Interlocking Structures
      • Islamic Patterns
      • Linear Algebra
      • Minimal Surfaces
      • Muqarnas
      • Non-Euclidean Geometry
      • Paneling
      • Parametric Curves
      • Parametric Objects
      • Parametric Surfaces
      • Pattern Deformations
      • Patterns
      • Pavilions
      • Polyhedra
      • Rammed Earth Structures
      • Robotic Fabrication
      • Shape Grammars
      • Simulation
      • Space Syntax
      • Surface Constructions
      • Tessellations
      • Tools
      • Vector Fields
      • Virtual Reality
    • Tools and Languages
      • 3DS Max
      • 3DS Max Script
      • Grasshopper
      • Photoshop
      • Physical Prototyping
      • Revit
      • Rhino
      • Rhino Macro
      • Rhino Python
      • Rhino Script
      • Unity
  • Monthly Archive

    • October 2025 (1)
    • June 2025 (2)
    • May 2025 (2)
    • April 2025 (5)
    • December 2024 (40)
    • August 2024 (5)
    • July 2024 (6)
    • April 2024 (4)
    • March 2024 (10)
    • February 2024 (10)
    • January 2024 (8)
    • December 2023 (10)
    • August 2023 (3)
    • July 2023 (3)
    • June 2023 (7)
    • May 2023 (8)
    • April 2023 (7)
    • March 2023 (2)
    • February 2023 (2)
    • January 2023 (3)
    • December 2022 (6)
    • November 2022 (7)
    • January 2022 (1)
    • December 2021 (1)
    • October 2021 (3)
    • September 2021 (4)
    • August 2021 (4)
    • May 2019 (2)
    • April 2019 (1)
    • March 2019 (5)
    • January 2019 (2)
    • December 2018 (1)
    • November 2018 (4)
    • October 2018 (9)
    • July 2018 (1)
    • June 2018 (4)
    • May 2018 (1)
    • April 2018 (4)
    • February 2018 (2)
    • January 2018 (7)
    • August 2017 (9)
    • July 2017 (6)
    • October 2016 (1)
    • May 2015 (5)
    • April 2015 (8)
    • March 2015 (12)
    • February 2015 (4)
    • January 2015 (11)
    • November 2014 (1)
    • August 2014 (1)
    • June 2014 (2)
    • May 2014 (12)
    • April 2014 (5)
    • March 2014 (3)
    • February 2014 (6)
    • January 2014 (4)
    • December 2013 (5)
    • November 2013 (11)
    • October 2013 (2)
    • September 2013 (9)
    • August 2013 (4)
    • July 2013 (2)
    • June 2013 (14)
    • May 2013 (4)
    • April 2013 (10)
    • March 2013 (11)
    • February 2013 (11)
    • January 2013 (10)
    • December 2012 (10)
    • November 2012 (6)
    • October 2012 (13)
    • September 2012 (2)
    • August 2012 (5)
    • July 2012 (14)
    • June 2012 (6)
    • May 2012 (17)
    • April 2012 (15)
    • March 2012 (9)
    • February 2012 (16)
    • January 2012 (18)
    • December 2011 (20)
    • November 2011 (2)
  • Support designcoding!

    Enjoying designcoding? Support its future with a small donation on Patreon. Thank you!

    Patreon


    copyright 2026 designcoding.net | about | table of contents | keywords | privacy policy | end user license agreement