Grasshopper within compute.rhino3d

A post was split to a new topic: GH Plugin issues and compute

Thank you for sharing this @jho , a great step by step guide. I followed this but I’m having some issues.

This solution works in the preview viewport. But then when I build the unity project it breaks and no meshes are returned.

Compute sends an error saying it cannot load the grasshopper script and it appears that all arguments sent to compute are null. Is this an issue with the json serialisation?

Anyone else had similar issues?

Thanks.

Using the sample files, how would I go about creating a 2nd set of meshes so that I can reference them in the JavaScript docs, and change their material?

I’m not sure how/where gh is referencing this key for the Mesh output.

Which sample are you using (we’ve recently moved them here)

In the extrusions example, there is this line which gets the mesh:

let data = JSON.parse(result.values[0].InnerTree['{ 0; }'][0].data);

If you want to add another output, you should group your output param and give the group a label with the following format: RH_OUT:SomeName

In that case, you should check the length of result.values and you’ll probably want to parse it like:

let data = JSON.parse(result.values[1].InnerTree['{ 0; }'][0].data);

(notice the index value in result.values[1])

1 Like

Thank you for this! And yes, I’m using the Extrusion/Slider example.

With having 2 meshes in a scene, I found the traverse() function didn’t work as well for me so I used this instead to remove existing meshes from the scene:

scene.remove.apply(scene, scene.children);

scene.add(threeMesh1);
scene.add(threeMesh2);

I wanted to follow up on this. Can you list/explain the available list of GHTypeCodes for the most recent javascript examples?

The type codes aren’t required anymore. Some of these are old samples that have been recently moved to the samples repo.

Here’s an up-to-date example without the redundant type code… https://github.com/mcneel/rhino-developer-samples/blob/7/compute/js/SampleGHCurveInput/app.js#L11

1 Like

Hi will, I tried the code you provided and didn’t change anywhere. However, there were an error. Could you help me to check it?

import compute_rhino3d.Util
import compute_rhino3d.Grasshopper as gh
import rhino3dm
import json

compute_rhino3d.Util.authToken = MYTOKEN

pt1 = rhino3dm.Point3d(0, 0, 0)
circle = rhino3dm.Circle(pt1, 5)
angle = 20

# convert circle to curve and stringify
curve = json.dumps(circle.ToNurbsCurve().Encode())

# create list of input trees
curve_tree = gh.DataTree("RH_IN:curve")
curve_tree.Append([0], [curve])
rotate_tree = gh.DataTree("RH_IN:rotate")
rotate_tree.Append([0], [angle])
trees = [curve_tree, rotate_tree]

output = gh.EvaluateDefinition('workshop_step5.ghx', trees)
# print(output)

# decode results
branch = output['values'][0]['InnerTree']['{ 0; }']
lines = [rhino3dm.CommonObject.Decode(json.loads(item['data'])) for item in branch]

filename = 'workshop_step5.3dm'

print('Writing {} lines to {}'.format(len(lines), filename))

# create a 3dm file with results
model = rhino3dm.File3dm()
for l in lines:
    model.Objects.AddCurve(l) # they're actually LineCurves...

model.Write(filename)

the error message was as below:

KeyError                                  Traceback (most recent call last)
<ipython-input-4-5663ad866f1e> in <module>
     24 
     25 # decode results
---> 26 branch = output['values'][0]['InnerTree']['{ 0; }']
     27 lines = [rhino3dm.CommonObject.Decode(json.loads(item['data'])) for item in branch]
     28 

KeyError: 'values'

Hey @ruishuyan, that code won’t work unless you modify the client’s connection settings. I’d recommend spinning up your own Compute server on your local machine and replacing…

compute_rhino3d.Util.authToken = MYTOKEN

…with…

compute_rhino3d.Util.url = 'http://localhost:8081/'

We’re working on providing better guides on our https://developer.rhino3d.com website.

1 Like

Hello!

I’ve gotten a series of errors in different scenarios, I’m wondering how I can debug for these. This is pulled from an example I made using Extrusions JS Sample to start:

Error 1: Run-ins with CORS Policy

Access to fetch at ‘https://compute.rhino3d.com/grasshopper’ from origin ‘https://myRootURL.com’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.

I couldn’t find an immediate solution, but after a few hours, this error disappeared and the app started working again… however every so often this CORS error returns. Is there a way to prevent this from happening?

Errors 2-4
I’m having trouble getting myAuthToken to work in order to run the rhino3dm() function in app.js, and depending on whether I’m using hosting on the web, or my localhost, the error differs.

Here’s my code:

    rhino3dm().then(async m => {console.log('Loaded rhino3dm.');
    rhino = m; // global
    RhinoCompute.url = "http://localhost:8081/" // Rhino.Compute server url
    RhinoCompute.apiKey = "myAuthTokenFromRhino3d.com/compute/login" // your Rhino.Compute server api key

    // load a grasshopper file!
    let url = 'grasshopper/BranchNodeRnd.gh'; // /grasshopper is a subfolder
    let res = await fetch(url); //line32
    let buffer = await res.arrayBuffer();
    let arr = new Uint8Array(buffer);
    definition = arr;

    init();
    compute();
});

Error 2 - When myExample is on the web

Error 3 - When I run myExample on my local host

Error 4 - When I pulled the latest basic js Extrusion example and opened the index in Chrome:

@gloryah.allen are you running your own instance of Rhino Compute or are you using compute.rhino3d.com?

The first CORS error (error 1) looks familiar and I think it happens when compute.geometry.exe has crashed. This happens every now and again with compute.rhino3d.com and I don’t always notice straight away. I think this error happens because when compute is down, the (error) response from the server doesn’t include the correct CORS headers. So it’s a bit of a red herring…

There are no guarantees that compute.rhino3d.com will remain up. It’s just a test server. We recommend running your own Rhino Compute server and we have a guide for setting this up.

http://localhost:8081/ points to an instance of Rhino Compute running on your computer. This is a good option for testing things out locally before publishing them to the web.

You shouldn’t need an API key when you’re testing with localhost; you’ll only need one if you’ve configured your Rhino Compute server to require one.

Errors 2 and 3 suggest that Rhino Compute isn’t running locally, but it’s pretty straight forward to get it running if you’re familiar with .NET development.

what is GHTypeCodes for BRep?

Type codes are no longer needed.

what is the way then? please help me .

What are you trying to do?

we are trying to input the Brep in GH definition using the rhino compute server( localhost).

@fraguada We are trying to input a brep curve and point in a GH defination, but there are several errors coming

can you share us a sample for inputing a curve and point from server side in javascript.

Please share a sample to draw a curve and a point and input it.

Gh curve input example: rhino-developer-samples/compute/js/SampleGHCurveInput at 7 · mcneel/rhino-developer-samples · GitHub

Points: rhino-developer-samples/compute/js/SampleGHDelaunayMesh at 7 · mcneel/rhino-developer-samples · GitHub