Converting the geometry of a Rhino .3dm-file into a speckle model

Hello,
I wrote some code that creates a new model in an existing project within a workspace, and I want to fill the model with the geometry from a .3dm Rhino file. Basically, I want to create a speckle model of a Rhino model using specklpy 3.2.0.

However, my model is empty, so I’m wondering what I’m doing wrong.
If there is an entirely different way to do this, please let me know.
Here is a snippet of the relevant code:

import rhino3dm

# Load local file
model_3dm = rhino3dm.File3dm.Read(r"...\filepath\TestModel.3dm")

root = Base()
root["@items"] = []

# here I convert Rhino Mesh to Speckle Mesh
for obj in model_3dm.Objects:
    geo = obj.Geometry
    if isinstance(geo, rhino3dm.Mesh):
        vertices = []
        faces = []

        for v in geo.Vertices:
            #vertices.extend([v.X, v.Y, v.Z])
            vertices.append(Point(x=v.X, y=v.Y, z=v.Z))
        
        for f in geo.Faces:
            if f.IsTriangle:
                faces.extend([3, f.A, f.B, f.C])
            else:
                faces.extend([4, f.A, f.B, f.C, f.D])
        
        speckle_mesh = Mesh(
            vertices=vertices,
            faces=faces,
            units=Units.m
        )
        
        root["@items"].append(speckle_mesh)

#(In this part of the code I create the client, authanticate it with my token, get my workspace and the project inside of it and create a new model)

# Here I send the root object containing the meshes
transport = ServerTransport(
    stream_id= project.id,
    client=client
)

hash = operations.send(base=root, transports=[transport])

input = CreateVersionInput(
    objectId = hash,
    modelId = model.id,
    projectId = project.id
)
version = client.version.create(input)

Are you able to share a link to a speckle model created using this script?

Yeah sure:
neues modell via api - test | Speckle

Hi,
From the model link you’ve sent. It doesn’t look like you’re sending any objects under @items

I suggest you debug your script to determine if it’s correctly adding the meshes.

You can better see the JSON object you’ve sent to speckle by enabling dev mode (shortcut key x)
image


Additionally, from the code snippet you’ve shared, what Mesh class are you importing?
If it’s one from Speckle.Objects
If so, then you are incorrectly constructing the vertices as a list[Point] when it should be a be a flat list of floats (i.e list[float])
and similar for faces, it should be a flat list of integers (list[int]) not what you currently have as a list[list[int]]

I would also double check which Mesh class you’re importing, as trying to use a Rhino mesh type here will simply not work.

Thank you! This was quite helpful.
The problem was, that the .3dm File did only have Breps and no Mash.
I also fixed the list to create a Mesh.

1 Like