Working with Nurbs Curve in GhPython

This is a two part question:

  1. What is the ghpythonlib.component equivalent for NurbsCrv (the version where the input is point, weight and kink)? The NurbsCurve() method I’m getting calls for vertices, degree and periodic (see image below).

  2. I’d much rather avoid ghpythonlib.component. How do I get the control points, weight and kink of a curve via rhinocommon? I’d like to transform the control points and then reconstruct the curve. How then do I create a curve with respect to those three variables with rhinocommon?

Side question: how do I duplicate a list of points. I need to transform the duplicate without affecting the original? Point3d does not seem to have a .Duplicate() method.

Hi Lawrenceyy,

  1. There is no equivalent right now. The fact that the two are named the same has one take priority. I reported a bug.
  2. Grasshopper uses a method similar to this. I believe this will be useful for you to learn also how to modify the curve directly.

Side question: you can use rg.Point3d(pt3d). That is, with the master point as input.

import Rhino.Geometry as rg

def C_PWK(pts, knots, weights=None):
    
    if len(pts) < 2: raise Exception("Too few pts")
    if not weights: weights = [1]*len(knots)
    if len(knots) != len(weights):  raise Exception("length mismatch")
    
    degree = len(knots) - len(pts) + 1
    if degree <= 0 or degree > 11: raise Exception("invalid degree")
    
    c = rg.NurbsCurve.Create(False, degree, pts)
    
    for i in range(c.Points.Count):
        c.Points[i] = rg.ControlPoint(pts[i], weights[i])
        
    for i in range(c.Knots.Count):
        c.Knots[i] = knots[i]
    
    if not c.IsValid: raise Exception("created curve is invalid")
    
    return c

if P:
    a = C_PWK(P,K,W)

example.gh (7.4 KB)

I hope this helps,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Thanks @piac

I feel like I’ve encountered other components that have had the same name and therefore not present in the ghpythonlib.component list…

Your code works. I spent a bit of time on http://developer.rhino3d.com but could not figure it out. I usually rely on that little help menu that pops up, but count is not on the list for rg.NurbsCurve.Points. Are we supposed to just know it’s an option?

And what is the difference between

rg.NurbsCurve(args)

and

rg.NurbsCurve.Create(args)

I managed to duplicate the points via copy.deepcopy(). I was not sure that it would work because copy does not show up as an option when I type import. Why aren’t all the modules shown in the menu?

(how) Are we supposed to just know it’s an option?

The NurbsCurve.Points property gives access to an instance of NurbsCurvePointList.
http://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_Geometry_NurbsCurve_Points.htm
If you write rg.Collections.NurbsCurvePointList you can see the popup. To find out the type of an object, you can use an instance and the type() function. For example, type(c.Points).

However, I think that the design of NurbsCurvePointList can be improved. I’ll make sure that it works with Python’s len() in Rhino 6.

Sorry, I don’t have an answer to the last question.

Why aren’t all the modules shown in the menu?

@steve will be able to help regarding that.