Points in Python

Points

In Python, a Rhino 3D point is represented as a Point3d structure. Conceptually, Point3d exist in memory as a zero-based list containing three numbers. These three numbers represent the X, Y and Z coordinate values of the point.

point3D contains (1.0, 2.0, 3.0)  

Creating Points

A Point3D structure can be constructed in a number of different ways. Two common ways are:

import rhinoscriptsyntax as rs

pnt = rs.CreatePoint(1.0, 2.0, 3.0)
pnt = rs.CreatePoint(1.0, 2.0) # This creates a point with the Z coordinate set to 0

The ‘CreatePoint()’ function is very flexible. It can take a tuple of two or 3 numbers and return a Point3d. The function can also extract the coordinates of a Rhino GUID to return a Point3D.

It is not always necessary to construct a point before passing it to a function that requires a point. It is possible to construct points directly as an argument to a function. A Point is a list like structure. Wrap coordinates in parenthesis() when passing them directly to a function. For instance the rs.addline(point, point) function requires two points. Use the following syntax to construct the points on the fly:

rs.AddLine(((45,56,32),(56,47,89))

Like 3-D points, Python represents a single 2-D point as a zero-based list of numbers. The difference being that 2-D points contain only X and Y coordinate values.

Passing coordinates in () to a function is common with RhinoScriptSyntax.

Accessing Point Coordinates

A Point3D list can be accessed like a simple python tupple, one element at a time:

import rhinoscriptsyntax as rs

pnt = rs.CreatePoint(1.0, 2.0, 3.0)

print(pnt[0]) #Print the X coordinate of the Point3D
print(pnt[1]) #Print the Y coordinate of the Point3D
print(pnt[2]) #Print the Z coordinate of the Point3D

The coordinates of a Point3d may also be accessed through its .X, .Y and .Z properties.

import rhinoscriptsyntax as rs

pnt = rs.CreatePoint(1.0, 2.0, 3.0)

print(pnt.X) # Print the X coordinate of the Point3D
print(pnt.Y) # Print the Y coordinate of the Point3D
print(pnt.Z) # Print the Z coordinate of the Point3D

Editing Points

To change an individual coordinate of a Point3d simply assign a new value to the correct coordinate through the index location or coordinate property:

import rhinoscriptsyntax as rs

pnt = rs.CreatePoint(1.0, 2.0, 3.0)

pnt[0] = 5.0 # Sets the X coordinate to 5.0
pnt.Y = 45.0 # Sets the Y coordinate to 45.0

print(pnt) #Print the new coordinates

Rhinoscriptsyntax contains a number of methods to manipulate points. See RhinoScript Points and Vectors Methods for details.

Adding a point to display in Rhino

It is important to understand the difference between a Point3d and a point object added to Rhino’s document. Any geometry object that exists in Rhino’s database is assigned an object identifier. This is represented as a Guid. The Guid’s object is geometry and the associated attributes that can be drawn, belongs to a layer, is saved to a Rhino file and is counted as a Rhino object. A Point3d is simply a data structure of 3 numbers that exists in memory. It can be manipulated in memory, but will not be seen in Rhino or saved. A Point3d is added to the Rhino document through the rs.AddPoint() command. To create a Point3d from a Guid, use the rs.PointCoordinates(guid) or the rs.CreatePoint(Guid) function.