RhinoCommon


import Rhino


RhinoCommon is a .NET library that allows you to interact with Rhino from Python. It is a powerful library that contains:


It is composed by many namespaces, each of them containing a set of classes, structures and functions. The most important namespace is Rhino.Geometry that contains all the classes and structures to create and modify geometries in Rhino.


🔎🔎🔎 To check what is contained in the library, check the RhinoCommon API* Documentation 🔎🔎🔎


Let’s see the basics to manipulate geometries in Rhino with RhinoCommon..


Add object

Create a geometric object (in memory) and add to RhinoDocument:

import Rhino.Geometry as rg

# get the current document
active_doc = Rhino.RhinoDoc.ActiveDoc

# create a geometric object in memory
point = rg.Point3d(0,0,0)

# you can access the object properties
x_coordinate = point.X
print(x_coordinate)  # 0.0

# if you want to add the object to the document
guid_point = active_doc.Objects.AddPoint(point)
print(guid_point)  # System.Guid



Get object

Get the geometric object from the RhinoDocument:

import Rhino

# get the object from the document
go = Rhino.Input.Custom.GetObject()
go.SetCommandPrompt("Select point")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Point
go.Get()

# get the object reference of the first object selected
obj_ref = go.Object(0)

# cast the object reference to a point geometry object
point = obj_ref.Point()

print(type(point))  # <class 'Rhino.Geometry.Point'>



Replace object

Replace the geometric object from the RhinoDocument:

import Rhino
import Rhino.Geometry as rg

# create a point
point = rg.Point3d(0,0,0)

# get the current document
active_doc = Rhino.RhinoDoc.ActiveDoc

# add the point to the document
guid_point = active_doc.Objects.AddPoint(point)

# modify the point
point.X = 10
point.Y = 10
point.Z = 10

# replace the point in the document
scriptcontext.doc.Objects.Replace(
    guid_point,  # the guid of the object to replace
    point        # the new geometric object (in memeory)
)



Duplicate object

Duplicating the geometric object from the RhinoDocument:

import Rhino
import Rhino.Geometry as rg

# create a point
point = rg.Point3d(0,0,0)

# get the current document
active_doc = Rhino.RhinoDoc.ActiveDoc

# add the point to the document
guid_point = active_doc.Objects.AddPoint(point)

# duplicate the point
guid_point_duplicate = active_doc.Objects.Duplicate(guid_point)

print(guid_point != guid_point_duplicate)  # True



Delete object

Delete the geometric object from the RhinoDocument:

import Rhino
import Rhino.Geometry as rg

# create two points
point_A = rg.Point3d(0,0,0)
point_B = rg.Point3d(10,10,10)

# get the current document
active_doc = Rhino.RhinoDoc.ActiveDoc

# add the points to the document
guid_point_A = active_doc.Objects.AddPoint(point_A)
guid_point_B = active_doc.Objects.AddPoint(point_B)

# delete the point_B from the document
active_doc.Objects.Delete(guid_point_B, True)


🛠 Exercise


🦏⬇️⬇️⬇️ Download the Rhino file here ⬇️⬇️⬇️🦏

Create: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 Get: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 Duplicate: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 Replace: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍



[1] Structures are similar to classes, but they are value types and are typically used for smaller, simpler objects. They are often used to represent geometric objects, such as points, vectors, and bounding boxes.