Nurbs to mesh in python

Hi,

I have a few Nurbs generated with rs.AddSphere in my python script and I wanted to convert them into meshes within the script. However, it does appear there is a rhinoscriptsyntax MeshToNurbs command but not the opposite NurbsToMesh.

The only work around I thought was to use the grasshopper component MeshSphere, see code below, however the spheres do not seem to appear after generated, and for example I can’t change their colour. Should I do some baking somehow?

Any alternative way to convert a nurbs to a mesh (without sampling it and running a tesselation) you can think of?

import ghpythonlib.components as comps

sp1 = rs.AddSphere(pt, 2) #not needed if I use the component below
sp = comps.MeshSphere(pt,2,3,3)
rs.ObjectColor(sp, color)

Error message: Message: iteration over non-sequence of type Mesh

import Rhino

sphere = Rhino.Geometry.Sphere(pt, 2.0)
mesh = Rhino.Geometry.Mesh.CreateFromSphere(sphere, 8, 8)
1 Like

Rhinoscriptsyntax is missing methods to mesh Breps directly - you need to do some RhinoCommon. In addition to the primitives that can be meshed as in @diff-arch 's post above, you can mesh generic Breps something like this:

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def MeshBrep(brep_id,params):
    brep=rs.coercebrep(brep_id)
    if brep:
        mesh=Rhino.Geometry.Mesh()
        mesh_parts=Rhino.Geometry.Mesh.CreateFromBrep(brep,params)
        for mesh_part in mesh_parts: mesh.Append(mesh_part)
        mesh.Compact()
        return mesh
    
def TestMeshBrep():
    obj_id=rs.GetObject("Select surface or polysurface to mesh",8+16,preselect=True)
    if not obj_id: return
    
    #equivalent of 'jagged and faster'
    mesh_params=Rhino.Geometry.MeshingParameters.Coarse
    mesh_brep=MeshBrep(obj_id,mesh_params)
    if mesh_brep:
        mesh_id=sc.doc.Objects.AddMesh(mesh_brep)
        sc.doc.Views.Redraw()
        rs.SelectObject(mesh_id)
TestMeshBrep()

Look under

https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Geometry_MeshingParameters.htm

to see what different kinds of input you can use for the meshing parameters.

1 Like

Thanks, a very stupid question though: once I get the mesh how do I get back the ID of that mesh that I need to keep working with rhinoscriptsyntax?

You can for instance do this:

import scriptcontext
import Rhino

pt = Rhino.Geometry.Point3d.Origin
sphere = Rhino.Geometry.Sphere(pt, 5.0)
mesh = Rhino.Geometry.Mesh.CreateFromSphere(sphere, 8, 8)

guid = scriptcontext.doc.Objects.AddMesh(mesh)
print guid