Loading rhino3dm.py into grasshopper

I’m trying to get a File3dm created in memory inside grasshopper, from a base64 string. There’s a File3dm.Decode method in rhino3dm, but I’m not sure how to use rhino3dm within grasshopper.

Can this be done?

Hi @Daniel_González_Rein,

If you are in Grasshopper, just use the RhinoCommon you already know.

– Dale

I’m not sure if I was looking at the same docs, because I was looking at https://developer.rhino3d.com/api/rhinocommon/rhino.fileio.file3dm. They look like they’re the same thing, but I wanted to mention it in case there are some differences.

I’ve tried a few things using that class, but I couldn’t get it working. Is there a working example of going from a base64 string to a File3dm object using python?

I forgot all the things I tried, but a few that I can recall are:

File3dm.FromByteArray(base64String)
File3dm.FromByteArray(bytes(base64String))
File3dm.FromByteArray(bytearray(base64String))
File3dm.FromByteArray([x for x in bytes(base64String)])
File3dm.FromByteArray([x for x in bytearray(base64String)])

None of them work, and the error they provide is some form of Expected Array[Bytes] (or something similar, I can’t remember the exact error and I can’t access my work computer at the moment).

I’ve also tried saving a file with the byte array (instead of the base64) and loading it in a similar fashion, but I got some other errors.

Looking at rhino3dm.py, there’s a method called Decode, but I’m not sure how to use rhino3dm.py within grasshopper. I’m also unsure if using Decode would work, since the docs say “[todo] add documentation”. I’m guessing it should work because I can use file3dm.encode() from rhino3dm.js in my node server, from which I call rhinocompute. But so far it’s just a guess and I haven’t been able to test it.

Hi @Daniel_González_Rein,

A Base64 string is not the same as a byte array.

Can you explain in detail what you are trying to do and why?

Thanks,

– Dale

Try calling .decode('base64') on your base64 encoded string and pass that off to the FromByteArray function

1 Like

@dale Sure. I’m working on a web app where users can upload 3dm models, I send those models to a node server for further processing, and then I send the updated models to a rhinocompute server.

The way I send the models from the client to node is as a base64 string, since I’m sending more information with the requests and the body is a JSON. One of the JSON fields is the model, which is the base64 string representation of the model.

When I call rhinocompute from the node server, I do the following to send the base64 string of the model:

const trees = []
const model = new RhinoCompute.Grasshopper.DataTree('base64Model')
const fileB64 = fileToBase64(file.toByteArray(options))
model.append([0], [fileB64])
trees.push(model)

const rhinoResult = await RhinoCompute.Grasshopper.evaluateDefinition(
  definition,
  trees
)

Then, on the grasshopper script, I grab the base64Model string, and I want to generate a File3dm object from it.

I know a base64 string is not the same as a byte array, but I’ve tried everything I could think of.

@stevebaer I’ll try that, thanks!