Rhino Common: SubD.CreateFromMesh does not keep texture UVs

Hi everyone!

I discovered a bug or at least I can no get this to work

Below you see on he left twice the same mesh with a UV mapped texture.

The lower one circled in blue is a SubD created by the Rhino command “_ToSubd”.
The upper one circled in red is a SubD created with Rhino.Geometry.CreateFromMesh() method.

In he one created with Rhino Common method, he UV mapping seems to be parametrized and does no keep as on the original mesh.

I also found this and tried to implement it, but it did not seem to help:

https://developer.rhino3d.com/api/rhinocommon/rhino.geometry.subdcreationoptions.texturecoordinateoption

This is the python script I used to create the Subd from a mesh

#! python 3
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import scriptcontext as sc

import System

def mesh_to_subd():
    # Select a mesh
    mesh_id = rs.GetObject("Select mesh to convert to SubD", rs.filter.mesh, preselect=True)
    
    if not mesh_id:
        print("No mesh selected")
        return
    
    # Get the mesh object
    mesh = rs.coercemesh(mesh_id)
    
    if not mesh:
        print("Invalid mesh")
        return
    
    # Create SubD creation options
    options = rg.SubDCreationOptions()
    # Set to 1 for CopyMapping (0=Unset, 1=None, 2=Automatic, 3=Packed, CopyMapping=4, CopyCoordinates=5)
    # https://developer.rhino3d.com/api/rhinocommon/rhino.geometry.subdcreationoptions.texturecoordinateoption
    
    options.TextureCoordinateOption = 5
    
    # Convert mesh to SubD with options
    subd = rg.SubD.CreateFromMesh(mesh, options)
    
    if subd:
        # Add the SubD to the document
        subd_id = sc.doc.Objects.AddSubD(subd)

        rs.SelectObject(subd_id)
        sc.doc.Views.Redraw()


# Run the function
if __name__ == "__main__":
    mesh_to_subd()

In general the Rhino Common SubD generation API is very limited currently it seems.

Would be great if someone could help wih this.
Do I miss something here or is this indeed a bug?

Thanks already,

Best,
Christoph

P.s.:

This seems to describe the originally same problem for he command, where it seem to be fixed, but no for the Rhino Common API

Hi @CGr,

Does the ToSubD command work?

Can you post a .3dm file you want converted?

– Dale

Hi Dale,

Thanks for the quick response!
This is the file I sent the screenshot from.

To_Subd_UV_test_001.3dm (483.0 KB)

With the “_ToSubd” command everyhing works great, but with the python script I shared above he subd does no keep the UV positions of the mesh.

The script does also not keep he material while “ToSubd” does (I assigned it again manually after)

Hi @CGr,

We’ll need to add some functionality to RhinoCommon before you can to this via scripting.

In the mean time, you might consider just scripting the ToNurbs command using rs.Command.

– Dale

Thanks for this, Dale!

Unfortunately the rs.Command is not option for me, hence I am working on a generic USD ex- and importer and the meshes are loaded in memory, not in the scene yet.

The shared file and script where only used to localize the error.

Is this a functionality/fix I can expect an upcoming Rhino update?
Also would this be exposed in C++ - so Rhino SDK and/or Open Nurbs?

Hi @CGr,

The issue (above) has already been fixed and should be available in this week’s Rhino 8 SR28 release candidate. Using the Rhino C++ SDK is always an option.

– Dale

Amazing, thank you very much for the quick action.

I have another follow up quesiton - same topic but reverse.

I could also not find functionality exposed to extract the UVs / Texture Coordinates from a USD.
While I Rhino I can gather that very easily by iterating over mesh.TextureCoordinates, I couldn’t find any way to get the texture coordinates from a Subd - is that somewhere hidden in the API or is this not exposed currently?

Implementing it with Rhino C++ SDK would have been probably the better choice from the very beginning, but I got quite for with python and do not have the time to fully restart now unfortunately

Cheers,
Christoph

RH-91425 is fixed in Rhino 8 Service Release 28