Python: How to make a closed curve?

How can I make a closed curve? Here’s my attempt, but it was unsuccessful. :frowning:

#! python 2

import Rhino.Geometry as rg
import rhinoscriptsyntax as rs
import scriptcontext as sc
import math

points = []
radius = 5
total_points = 20
for i in range(total_points):
    angle = (2 * math.pi * i) / total_points
    x = radius * math.cos(angle)
    y = radius * math.sin(angle)
    z = 0.0
    point = rg.Point3d(x, y, z)
    points.append(point)
    sc.doc.Objects.AddPoint(point)

curve = rg.Curve.CreateInterpolatedCurve(points, 3, rg.CurveKnotStyle.Uniform)
print('is closable?', curve.IsClosable(0.001)) # returns False :(
success = curve.MakeClosed(0.001)
print('success', success) # returns False :(
sc.doc.Objects.AddCurve(curve)

Well, shoot, I’ve been struggling with this for hours, but it looks like all I had to do was duplicate the first point and add to the end of the list of points. :man_facepalming: I’ll mark this as the solution unless there’s a better way.

#! python 2

import Rhino.Geometry as rg
import rhinoscriptsyntax as rs
import scriptcontext as sc
import math

points = []
radius = 5
total_points = 20
for i in range(total_points):
    angle = (2 * math.pi * i) / total_points
    x = radius * math.cos(angle)
    y = radius * math.sin(angle)
    z = 0.0
    point = rg.Point3d(x, y, z)
    points.append(point)
    sc.doc.Objects.AddPoint(point)

# oh, you have to add the first point onto the end of the points list!!!
points.append(rg.Point3d(points[0]))

curve = rg.Curve.CreateInterpolatedCurve(points, 3, rg.CurveKnotStyle.Uniform)
print('is closable?', curve.IsClosable(0.001))
success = curve.MakeClosed(0.001)
print('success', success)
sc.doc.Objects.AddCurve(curve)

I’ve discovered that if you want the curve seam to be smooth, you need to make the curve be periodic. This was not apparent while I was working with circles but became very noticeable when I tried it out on ovals.

closed_curve_test.py (881 Bytes)

1 Like