Compute AWS Problems

compute_url = "http://3.104.37.217:80/"
compute_rhino3d.Util.url = compute_url
compute_rhino3d.Util.apiKey = ''

headers = {
    "RhinoComputeKey": ""
}

class __Rhino3dmEncoder(json.JSONEncoder):
    def default(self, o):
        if hasattr(o, "Encode"):
            return o.Encode()
        return json.JSONEncoder.default(self, o)

    gh_admin = open(r"./gh_scripts/admin.ghx", mode="r",
                    encoding="utf-8-sig").read()
    gh_admin_bytes = gh_admin.encode("utf-8")
    gh_admin_encoded = base64.b64encode(gh_admin_bytes)
    gh_admin_decoded = gh_admin_encoded.decode("utf-8")

    all_points = []

    admin_curves = []
    suburb_names = []
    while True:
        admin_response = requests.get(adminboundaries_url, params=params)
        if admin_response.status_code == 200 and admin_response.json():
            admin_data = json.loads(admin_response.text)
            if "features" in admin_data:
                for feature in admin_data["features"]:
                    suburb_name = feature['attributes']['suburbname']
                    geometry = feature["geometry"]
                    for ring in geometry["rings"]:
                        points = []
                        for coord in ring:
                            point = rh.Point3d(coord[0], coord[1], 0)
                            points.append(point)
                            all_points.append(point)
                        polyline = rh.Polyline(points)
                        curve = polyline.ToNurbsCurve()
                        admin_curves.append(curve)
                        suburb_names.append(suburb_name)
                break  # exit the loop if data is found
            else:
                time.sleep(0)
        else:
            # wait for some time before retrying
            time.sleep(0)

    maximum_suburb_num = len(suburb_names) - 1

    suburb_name_to_index = {name: i for i,
                            name in enumerate(set(suburb_names))}
    suburb_indices = list(suburb_name_to_index.values())

    curves_to_include = admin_curves

    # serialize curves
    serialized_curves = []
    for curve in curves_to_include:
        serialized_curve = json.dumps(curve, cls=__Rhino3dmEncoder)
        serialized_curves.append(serialized_curve)

    # create list of dictionaries for each curve
    curves_list = []
    for i, curve in enumerate(serialized_curves):
        curves_list.append(
            {
                "ParamName": "Curves",
                "InnerTree": {
                    f"{{ {i}; }}": [
                        {
                            "type": "Rhino.Geometry.Curve",
                            "data": curve
                        }
                    ]
                }
            }
        )

    max_dict = {
        "ParamName": "Max",
        "InnerTree": {
            "{ 0; }": [
                {
                    "type": "System.Int32",
                    "data": maximum_suburb_num
                }
            ]
        }
    }

    suburb_dicts = []
    for i, suburb_name in enumerate(suburb_names):
        suburb_index = suburb_indices[i]
        suburb_dict = {
            "ParamName": "Numb",
            "InnerTree": {
                f"{{ {i};0 }}": [
                    {
                        "type": "System.Int32",
                        "data": suburb_index
                    }
                ]
            }
        }
        suburb_dicts.append(suburb_dict)

    # update geo_payload with list of curves, point, and string inputs
    geo_payload = {
        "algo": gh_admin_decoded,
        "pointer": None,
        "values": curves_list + [max_dict]
    }

    for i, curve in enumerate(curves_list):
        payload = copy.deepcopy(geo_payload)
        payload["values"].append(curve)
        payload["values"].append(suburb_dicts[i])
        payload["values"].append(max_dict)
        res = requests.post(compute_url + "grasshopper", json=payload, headers=headers)
        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'])
                        geo = rh.CommonObject.Decode(data)
                        att = rh.ObjectAttributes()
                        att.SetUserString("Suburb Name", str(suburb_names[i]))
                        att.LayerIndex = admin_layerIndex
                        model.Objects.AddMesh(geo, att)

model.Write('C:/Users/RivinduB/Desktop/output.3dm')

Hi, I’ve posted about this a couple of times but I wanted some help if possible. I’ve set up an AWS server with Rhino compute and it seems to be running fine. Get requests seems to at 200 but post requests return 500. When I run the same script locally with local host, it works perfectly. I have set the apikey (not in code for security), included in the headers for the post request as well. Res.content gets me this:


The problem is so odd because it works perfectly with local host but not with the AWS server. Any help would be useful thanks!

@AndyPayne
@fraguada
@stevebaer

Any help here would be appreciated @stevebaer @AndyPayne

It’s a hurdle that’s impacting delivery.
Cheers

1 Like

Sorry @bradshales a lot of our developers are out of the country this week and next. Have you tried sending a much simpler POST message to your VM? Maybe just a very simple script that you know should work? That might help figure out whether the fault lies in one of the other parts of your code or if it’s something in how the server is configured.

2 Likes

Will do thanks @AndyPayne
I think @Rivindu_Bandara attempted a few variations, but we will give that a go first.
Cheers

1 Like

Hi @AndyPayne,

Just ran a really simple script of extending a curve, it’s still the same error, I’m assuming something is not right with our VM instance. It also might be good to mention, the server works fine with hops within GH, when I plug in the URL and API key into GH preferences.

Thanks

Ok. I am out of town through the end of this week for a conference but perhaps we can setup a time to review this next week. Did you setup your VM
instance using the powershell script on the guide?

1 Like

Hi @AndyPayne,

We spun another instance of EC2 and will test it out and let you know how it goes. We have setup the EC2 instance following the guide. If there’s no luck, we can set up a time early next week for review.

Thanks

1 Like

Hi @AndyPayne,

The new instance of EC2 seems to be working better and now we are getting some results, I’m still having some trouble with GH plugins and installing them. I looked through this Add grasshopper plugins to Rhino Compute when hosting it up with IIS - Rhino Developer / compute.rhino3d - McNeel Forum, and it still doesn’t seem to work.

Whenever I run ladybug or some other scripts with 3rd party plugins I get this.

Thanks

Again, I apologize for the delay. Most plugins should automatically work with rhino.compute with two main caveats. The first is that a plugin will likely fail in compute if it utilizes a UI that requires some sort of input from a user. Remember, rhino.compute is a console application which runs Rhino and Grasshopper as a sub-process. It has no way to “click” a button or navigate a UI so if the plugin you want to run incorporates a user interface, then it will likely fail in rhino.compute. The second caveat is if a plugin makes wide use of the rhino document. Remember, rhino.compute runs in a headless environment. As such, there is no “document” that you can query for information. We have put a few utility methods to try to catch users who try to make a call to get access to the document tolerances (length and angle) and the document units. Hopefully, in those cases rhino.compute will receive a default value rather than crashing. But, if the plugin you’re trying to load has any other calls to a document, then it will likely fail as well.
I don’t know Ladybug very well so I don’t know how to advise you in that situation. Have you tried emailing Mostpha or posting a question in their forum?

1 Like

Thanks for the response, Andy. It seemed like it was just a string error, for some reason one of the inputs was not reading the string of a file path correctly. Everything else seems to work well so far. Thanks for your help.

1 Like