How to pass mesh to Hops

,

Hi there, I can’t seem to pass a mesh to my hops component, any help is appreciated.

from flask import Flask
import ghhops_server as hs
import rhino3dm as r3

#register hops app as middleware
app = Flask(__name__)
hops = hs.Hops(app)

@hops.component(
    "/meshInOut",
    name="meshInOut",
    description="randomly shift mesh vertices",
    inputs=[
        hs.HopsMesh("MeshIn", "M_i", "Mesh to be shifted"),
        hs.HopsNumber("Shift", "s", "distance of shift", default=2.0),
    ],
    outputs = [
        hs.HopsMesh("MeshOut", "M_o", "Shifted Mesh"),
    ],
)
def meshInOut(msh: r3.Mesh, s):
    if type(msh) is None:
        print('what the fuck')
    print((msh))
    for v in msh.Vertices:
        v += np.random.random(3)*s
    msh_out = msh
    return msh_out

if __name__== "__main__":
    app.run()

messhift.gh (7.3 KB)

Sorry - I was a bit frustrated. I can’t seem to find any resources that show how to pass a mesh to hops, perhaps it’s just a bug on my end.

Never mind - I got it working in Rhino 7 with hops 0.14.0.

That’s understandable. As far as I can tell the support here for Hops related stuff is not really great. In the advent of better Python 3 support in Rhino 8, my guess is that the project is less of a priority than it previously was and might be rendered superfluous in the future. This is speculative though!

Congrats on making it work.

What version were you using previously?

I was on the WIP 8.0.22361.13136, 2022-12-27 and Hops v0.15.4. I tried with Hops v.0.14.0 and it still is unable to collect the mesh.

Ah, this is a Rhino 8 thing at the moment. We are passing meshes from Hops to the CPython server in a Rhino 8 format and the CPython server only currently knows how to deal with Rhino7 and below formats. We’re actively getting the libraries that the CPython server updated so the server will work with Rhino 8 format.

1 Like

OK, I revisited this and figured out how to convert the incoming mesh from rhino3dm to Rhino.Geometry and back again in hops, this works and is relatively fast, but I’m interested to know if there’s a better way / I’m doing it wrong:

import rhinoinside
rhinoinside.load(8)
import System
import Rhino.Geometry as rg
import Rhino.FileIO as rf
import rhino3dm

# ... hops component that passes the mesh from grasshopper to python (JSON data states it is Rhino Geometry Mesh type, but in python it is rhino3dm.mesh)...

mesh_JSON = mesh.Encode()
rg_mesh = rg.Mesh.FromBase64String(
    mesh_JSON['archive3dm'],
    mesh_JSON['opennurbs'], 
    mesh_JSON['data']
)
# modify your mesh ...
modified_mesh = do_something_in_python(rg_mesh)
# pass the modified mesh back for hops
s_options = rf.SerializationOptions()
mesh_out = modified_mesh.ToJSON(s_options)

return rhino3dm.CommonObject.Decode(json.loads(mesh_out))