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.

1 Like

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)

Hello Luis,

Is that failing because i’m loading an STL instead OBJ?

Really thanks

You should be loading the STL with the Three js STL Loader.

Please provide a simplified version of the code you are developing so we can see where it might be going wrong.

Hello Luis,

I’ve tried something, send directly the sphereGeometry created on your exmaple and it works great, but if I save that STL in some file, and then I load id from that file, when I send it to the server, the server just instantly crash without any type of error:

This is how I load my STL files:

loader.load(data.uri, (geometry) => {
let material = new THREE.MeshPhongMaterial({ color: 0xF1C27D, transparent: false, side: THREE.DoubleSide, clippingPlanes: this.clippingPlanes, flatShading: false });
this.mesh = new THREE.Mesh(geometry, material);
this.mesh.name = ‘loaded_file’
this.mesh.receiveShadow = true;
this.mesh.castShadow = true;
this.mesh.matrixAutoUpdate = true;
this.renderer.localClippingEnabled = false;
this.scene.add(this.mesh);

}, (onprogress) => {

}, (err) => {

})

The result of mesh.geometry from mainSphere (without loading the STL)

The result of mesh.geometry when I load from the STL file (Same geometry (the sphere))
In that case the server just crashes with that simple geometry(Sphere)

I think it’s because it’s a BufferGeometry?

I attach the sphere STL file.

Really appreciate your help!

sphere_saved.stl (97.0 KB)

Are you doing this?

Hello Luis,

Correct, in that way works perfectly, but not when I save the sphere in STL and then load it and I send the mesh.geometry.

In your side it’s working?

Are you using the STLLoader?

Hi Luis,

Yes, I’m using:

import { STLLoader } from ‘three/examples/jsm/loaders/STLLoader’;

Can you share your definition? I am having a difficult time debugging this. Passing the loaded stl without doing anything in GH works fine. As soon as I try to add anything else, even something as simple as adding some vertex colors causes Rhino.Compute to crash. If you can share your definition I can work with @AndyPayne to see where things are going wrong.

For sure,

It’s so simple, I attach you the GH definition.

Really thanks for helping me on that!

basic_test.gh (20.7 KB)