"Number of elements in knots must equal the number of elements in points plus degree minus 1"

I am trying to generate a new curve by identifying knots, degrees, points and weight from an existing curve. I am getting all the 4 necessary information to feed it back into the command “newcurve_id = rs.AddNurbsCurve(new_curve_points, knots, degree, weights)”. However, it shows me an error that Number of elements in knots must equal the number of elements in points plus degree minus 1. Can someone please help?
Below is the script that I am using.

import rhinoscriptsyntax as rs

def smoothingvector (point, prev_point, next_point, s):
pm = (prev_point+next_point)/2.0
va = rs.VectorCreate(pm, point)
vm = rs.VectorScale(va, s)
return vm

def smoothcurve(s):
curve_id = rs.GetObject(“Open curve to smooth”, 4, True)
curve_points = rs.CurvePoints(curve_id)
print curve_points
new_curve_points =
for i in range(1, (len(curve_points)-1)):
vm = smoothingvector(curve_points[i], curve_points[i-1], curve_points[i+1], s)
new_curve_points.append( rs.PointAdd(curve_points[i], vm) )
knots = rs.CurveKnots(curve_id)
print knots
degree = rs.CurveDegree (curve_id)
print degree
weights = rs.CurveWeights(curve_id,0)
print weights
newcurve_id = rs.AddNurbsCurve(new_curve_points, knots, degree, weights)
print newcurve_id
if newcurve_id: rs.DeleteObject(curve_id)
return newcurve_id

smoothcurve(.9)

I am trying to understand what knots mean and I get this, which I am still struggling to understand-
Knots
The knots are a list of (degree+N-1) numbers, where N is the number of control points. Sometimes this list of numbers is called the knot vector. In this term, the word vector does not mean 3‑D direction.

This list of knot numbers must satisfy several technical conditions. The standard way to ensure that the technical conditions are satisfied is to require the numbers to stay the same or get larger as you go down the list and to limit the number of duplicate values to no more than the degree. For example, for a degree 3 NURBS curve with 11 control points, the list of numbers 0,0,0,1,2,2,2,3,7,7,9,9,9 is a satisfactory list of knots. The list 0,0,0,1,2,2,2,2,7,7,9,9,9 is unacceptable because there are four 2s and four is larger than the degree.

The number of times a knot value is duplicated is called the knot’s multiplicity. In the preceding example of a satisfactory list of knots, the knot value 0 has multiplicity three, the knot value 1 has multiplicity one, the knot value 2 has multiplicity three, the knot value 3 has multiplicity one, the knot value 7 has multiplicity two, and the knot value 9 has multiplicity three. A knot value is said to be a full-multiplicity knot if it is duplicated degree many times. In the example, the knot values 0, 2, and 9 have full multiplicity. A knot value that appears only once is called a simple knot. In the example, the knot values 1 and 3 are simple knots.

If a list of knots starts with a full multiplicity knot, is followed by simple knots, terminates with a full multiplicity knot, and the values are equally spaced, then the knots are called uniform. For example, if a degree 3 NURBS curve with 7 control points has knots 0,0,0,1,2,3,4,4,4, then the curve has uniform knots. The knots 0,0,0,1,2,5,6,6,6 are not uniform. Knots that are not uniform are called non‑uniform. The N and U in NURBS stand for non‑uniform and indicate that the knots in a NURBS curve are permitted to be non-uniform.

Duplicate knot values in the middle of the knot list make a NURBS curve less smooth. At the extreme, a full multiplicity knot in the middle of the knot list means there is a place on the NURBS curve that can be bent into a sharp kink. For this reason, some designers like to add and remove knots and then adjust control points to make curves have smoother or kinkier shapes. Since the number of knots is equal to (N+degree‑1), where N is the number of control points, adding knots also adds control points and removing knots removes control points. Knots can be added without changing the shape of a NURBS curve. In general, removing knots will change the shape of a curve.

Hello,

Yes this has confused someone else recently : does this thread help you sort out your problem? Note that there is an error in the sample code from the Rhino Python primer pdf. The linked thread includes a link to the corrected code.

1 Like

@stevebaer is it possible to correct the online pdf with the corrected sample on GitHub? This has tripped a couple of people up recently…

1 Like

Yes, I see the pdf has a lot of errors, which causes lots of confusion for beginners like me. I hope they revise it carefully soon.