Python scripting within Rhino offers a powerful way to automate and customize your 3D modeling workflows. This guide will walk you through creating circles using Python in Rhino, addressing common challenges and providing efficient solutions. Whether you’re new to scripting or looking to refine your techniques, understanding how to manipulate geometry programmatically can significantly enhance your design process.
One common scenario involves creating multiple circles based on a point and transforming their positions. Let’s examine a typical approach and understand potential pitfalls. Initially, you might attempt to create circles by iteratively transforming a point object. However, a key concept in RhinoCommon is that transformations are often applied “in-place.” This means the original object itself is modified, rather than creating copies.
Consider this initial Python script:
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
def CreateCircles():
obj = rs.GetObject('select a point')
pt = rs.coerce3dpoint(obj)
dist = 2
for i in range(10):
xform = Rhino.Geometry.Transform.Translation(dist, 0, 0)
pt.Transform(xform)
circle = Rhino.Geometry.Circle(pt, 5)
obj_ref = sc.doc.Objects.AddCircle(circle)
sc.doc.Views.Redraw()
CreateCircles()
In this script, we select a point object and intend to create ten circles, each translated by a distance dist
. However, running this script will result in circles that appear staggered, with increasing distances between them. This happens because pt.Transform(xform)
modifies the original pt
object in each loop iteration. The starting point for each subsequent circle is shifted further, leading to the staggered effect.
To create circles in a linear sequence without this staggering, we can directly calculate the center points for each circle within the loop. Instead of transforming the initial point, we can calculate new points based on the starting point and the loop index.
Here’s a refined Python script that achieves this:
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
def CreateCircles():
pick_pt = rs.GetPoint("Pick start point")
if pick_pt:
dist = 2
circle_ids = []
for i in range(10):
pt = Rhino.Geometry.Point3d(pick_pt.X + i * dist, pick_pt.Y, pick_pt.Z)
circle = Rhino.Geometry.Circle(pt, 5)
circle_ids.append(sc.doc.Objects.AddCircle(circle))
sc.doc.Views.Redraw()
CreateCircles()
In this improved script, we use rs.GetPoint()
to directly pick a point on the screen, which is often more straightforward than selecting an existing point object if you only need its coordinates. Inside the loop, pt = Rhino.Geometry.Point3d(pick_pt.X + i * dist, pick_pt.Y, pick_pt.Z)
calculates a new Point3d
for each circle’s center. This ensures that each circle’s center is correctly positioned relative to the initial pick_pt
and the dist
value, resulting in a linear arrangement of circles.
This Python Guide demonstrates a fundamental aspect of Rhino scripting: understanding object manipulation and choosing the right approach for geometric transformations. By directly calculating point coordinates, we avoid the in-place transformation issue and achieve the desired linear circle creation. Exploring different methods and understanding RhinoCommon’s behavior is key to mastering Python scripting for Rhino and creating efficient, customized workflows.