Nurbs surface UV data to Mesh object vertices

Greetings to esteemed experts.
Let’s say I get a mesh from a Nurbs surface. In a mesh object, I have data about vertices (x, y, z) and faces. The coordinates of vertices of a mesh are obtained by a mesher, in a simple explanation, on the intersection of uv isocurves of the original Nurbs surface with some step. So I have the following question: how can I create such a mesh object, in which for each vertice there would be additionally data of its initial uv coordiates of the parent Nurbs surface?
Is it possible to achieve this with the original Rhino mesher, or I need to dig deeper and somehow modify/create some custom mesher?

Hi @monkelisha,

by getting closest points from each mesh vertex to your surface, you could obtain the surface u,v values per vertex eg:

for vertex in mesh.Vertices.ToPoint3dArray():
    rc, u, v = surface.ClosestPoint(vertex)


Thank you very much, clement,

your solution really does the job. I wouldn’t think it could be solved so easily in a single line of code. I’am newbie in Rhino scripting.

But still, as I see it, it’s kind of reverse engineering solution… I mean, could we somehow get this data at the very meshing stage, when we got it all at the hand, and pack this information somehow additionally to mesh object.

And also, if we are working with a polysrf, besides UV data we would also need the id of the exact surface of which UV we are getting for each mech vertex. Otherwise we need to make these calculations for all srf’s in a polysrf.

That’s why I was thinking about some extension of mesh object, which would contain additional data of its polysrf parent…

Ok, here is simplest mesher I can think of.
Just iterating through UV of the surface getting each vertex XYZ coordinates, put these to quadratic array
and… drawing many lines between vertices along rows and columns, which corresponds to UV of the parent srf.
Now I just can apply MeshFromLines command and get the mesh… :wink: It’s not the most elegant solution though.
So I got two questions:
— I’m sure there’s a better and elegant solution which I hope you point me to.
— How do I create my own custom object (or class) which I could provide with its own special properties? At the moment my “mesh object” is just a simple array.

from Rhino import *
from Rhino.DocObjects import *
from Rhino.Commands import *
from Rhino.Input import *
from Rhino.Input.Custom import *
from Rhino.Geometry import *
from scriptcontext import doc

def RunCommand():
    rc, obj_ref = RhinoGet.GetOneObject("Select surface", False, ObjectType.Surface)
    if rc <> Result.Success or obj_ref == None:
        return rc
    surface = obj_ref.Surface()
    nu = 0
    nv = 0
    segments = 25
    step = 1 / segments
    i, j = 0, 0
    p_array = []
    while i < (segments + 1):
        p_row = []
        j = 0
        while j < (segments + 1):
            nv = j * step
            u = surface.Domain(0).ParameterAt(nu)
            v = surface.Domain(1).ParameterAt(nv)
            pt = surface.PointAt(u, v)
            #print(nu, nv)
            j += 1
        i += 1
        nu = i * step
    i = 0
    while i < (len(p_array)):
        j = 1
        if i < (len(p_array[0]) - 1):
            doc.Objects.AddLine(p_array[i][j-1], p_array[i+1][j-1])
        while j < (len(p_array[0])):
            doc.Objects.AddLine(p_array[i][j], p_array[i][j-1])
            if i < (len(p_array[0]) - 1):
                doc.Objects.AddLine(p_array[i][j], p_array[i+1][j])
            #print(i, j)
            j += 1
        i += 1

    return Result.Success

if __name__ == "__main__":

This information is only available in the C++ API, not in RhinoCommon or Python scripting AFAICS.

Forgive me for continuing to bother you.
I have looked through all the information on Mesh Class here, but I could not find an answer.
Heres my situation: I have a poly-surface which is meshed to a poly-mesh. Poly-Mesh can be exploded resulting separate Meshes. These Meshes perfectly match the initial brep’s of the poly-surface. My question is: Does the Mesh object has a property pointing to its source brep (which is part of a poly-surface)?
If not, what’s the easiest way to find it out?

Hi @monkelisha,

No, but if your mesh is derived from the original brep, eg. by getting the render meshes from it, you could iterate over the brep faces and meshes while both should have the same index.