res = requests.post(compute_url + "grasshopper", json=geo_payload)
response_object = json.loads(res.content)['values']
for val in response_object:
paramName = val['ParamName']
innerTree = val['InnerTree']
for key, innerVals in innerTree.items():
for innerVal in innerVals:
if 'data' in innerVal:
data = json.loads(innerVal['data'])
mesh_geo = rh.CommonObject.Decode(data)
att = rh.ObjectAttributes()
att.LayerIndex = topography_layerIndex
model.Objects.AddMesh(mesh_geo, att)
vertices = mesh_geo.Vertices
faces = mesh_geo.Faces
vertex_list = [[vertices[i].X, vertices[i].Y, vertices[i].Z]
for i in range(len(vertices))]
vertex_list_flat = [coord for vertex in vertex_list for coord in vertex]
face_list = [(faces[i][0], faces[i][1], faces[i][2], faces[i][3]) for i in range(len(faces))]
flattened_faces = []
[flattened_faces.extend(list(face)) for face in face_list]
speckle_mesh = Mesh.create(vertices=vertex_list_flat, faces=flattened_faces)
This is an extract from some code where I used a Rhino mesh to create a speckle mesh using the faces and vertices. I have double checked that the list of vertices and faces match the ones in Rhino by checking in Grasshopper, it seems to be the exact same. However, the speckle mesh is so distorted in the viewer.
Any idea why this is? Here is the commit of speckle_mesh I sent to a stream.
Assuming all of your mesh faces are triangles, try
face_list = [ (3, faces[i][0], faces[i][1], faces[i][2], faces[i][3]) for i in range(len(faces))]
The signifier for vertex count per face is the 3 or it would be whatever the Rhino Mesh vertex count per face is. If you are producing meshes with NGONs then that 3 would become 4 or … whatever the count is
Currently, we are expecting the textureCoordinates list to be non null (not none)
In this case, I would suggest setting it to an empty list.
We probably could do a better job of communicating these requirements/validating objects better, especially on the SpecklePy side. We don’t do a great job of enforcing/documenting which properties need to be set and which ones don’t!
While we are at it, I would also suggest ensuring the colors list is initialized as an empty list also.
And setting units to one of these values.
Thanks for the input, helped out a lot. I was also wondering how I could add the colors as well. I’ve further extended my code to include colors of the mesh into the speckle geometry. Something is definitely getting comitted but its not loading up.
vertices = mesh_geo.Vertices
faces = mesh_geo.Faces
colors = mesh_geo.VertexColors
vertex_list = [[(vertices[i].X), (vertices[i].Y), vertices[i].Z]
for i in range(len(vertices))]
vertex_list_flat = [coord for vertex in vertex_list for coord in vertex]
face_list = [ (4, faces[i][0], faces[i][1], faces[i][2], faces[i][3])
for i in range(len(faces))]
flattened_faces = []
[flattened_faces.extend(list(face)) for face in face_list]
color_list = [(colors[i][0], colors[i][1], colors[i][2]) for i in range(len(colors))]
flattened_colors = []
[flattened_colors.extend(list(color)) for color in color_list]
speckle_mesh = Mesh.create(vertices=vertex_list_flat, faces=flattened_faces, texture_coordinates=[], colors=flattened_colors)
server_transport = ServerTransport(stream_id='eb641ee093', client=client)
data = speckle_mesh
speckle_geometry = speckle_objects.geometry.Mesh(data=data)
object_id = operations.send(
speckle_geometry, transports=[server_transport])
client.commit.create(stream_id='eb641ee093', object_id=object_id)