GH send Geometry from Threejs

I’m using threejs to load some scan, but I want to send that geometry to Rhino compute with the GH definition using the component Get Geometry.

Anyone know how I have to parse my data from Threejs to send to that component?

I’m doing like that in my ts code:

// Convert Three.js mesh to Rhino mesh
const response = await this.rhinoService.solve(
[
{
name: “Option_1”,
value: 0
},
{
name: “geometry”,
value: this.mesh.geometry
}

      ]
  );

Really thanks
imagen

You can do this with the rhino3dm.js library:

const rhinoMesh = rhino.Mesh.createFromThreejsJSON( { data: mesh.geometry } )

Where mesh is a threejs mesh.

See: rhino-developer-samples/script.js at 7 · mcneel/rhino-developer-samples · GitHub

Hi Luis,

I’ve tried that but wihout luck, It looks like the conversion to rhino mesh works:

But I still get the error on the rhino compute:

More code:

Blockquote
const response = await this.rhinoService.solve(
[{
name: “Elevar Arco Interno”,
value: this.productGenerator.archModification
},
{
name: “Texto”,
value: “Test \n test \n 1 \n 2”
},
{
name: “Tipo De Agujeros”,
value: 1
},
{
name: “Nervios Talón”,
value: 0
},
{
name: “geometry”,
value: rhino.Mesh.createFromThreejsJSON( { data: this.mesh.geometry } )
}

        ]
    );

    const responseJson = await response.json();

Solve function

async solve(inputs: { name: string, value: any }) {
compute.url = “http://localhost:8081/”;
compute.apiKey = “”;
let GrasshopperFile = “https:xxxx.gh”;

// this.firstVersion.map((input) => {
//   this.addDataTree(input.name, input.value);
// })

// Convert Three.js mesh to Rhino mesh
inputs.map((input) => {
  this.addDataTree(input.name, input.value);
})

const RhinoResponse = await compute.Grasshopper.evaluateDefinition(GrasshopperFile, this.trees, false);
console.log('Received response from server:', RhinoResponse);

return RhinoResponse;

}

addDataTree(key, value): void {
let param = new compute.Grasshopper.DataTree(key)
param.append([0], Array.isArray(value) ? value : [value])
this.trees.push(param)
}
}

You still need to format the result. Look at this example: rhino-developer-samples/script.js at 7 · mcneel/rhino-developer-samples · GitHub

  1. convert from threejs to 3dm and encode:
const rhinoMesh = rhino.Mesh.createFromThreejsJSON( { data: mainSphere } )
const rhinoMeshData = JSON.stringify( rhinoMesh.encode() )
  1. prep to send to compute:
// format data
let param1 = new RhinoCompute.Grasshopper.DataTree('mainMesh')
param1.append([0], [rhinoMeshData] )
...
// Add all params to an array
let trees = []
trees.push( param1 )
  1. send to compute
const res = await RhinoCompute.Grasshopper.evaluateDefinition(definition, trees)

Hi @fraguada , it looks now is doing somehting,

But I’m getting other erros:

When I use directly an stl from the local directory on grasshopper that doesn’t happens, and I’m loading the exact same file, but when I send it from threejs it fails and generates something weid:

Threejs result(with error, the object it’s strange because it finishes with errors)
imagen

Normal functionality:

imagen

What can cause that?

I think the mesh is not arriving correctly to rhino/GH because it’s generating this result because the mesh is empty.

Really appreciate your help!

I’d need some sample geometry to attempt to diagnose this.

For sure,

Maybe the problem it’s because I use STL instead OBJ?

I’ve tried to just return the same mesh, ignoring the other processes and it still fails.

I attach you the STL
stl_convert.stl (1.9 MB)