Python3 object created using rhino3dm: how to convert it to RhinoCommon Object?

I have created a Mesh object using Rhino3dm. When created, it gives a message as below:
{0}
0. <rhino3dm._rhino3dm.Mesh object at 0x00000153122CB8B0>

What is this code? How can I convert this object to a Rhino.Geometry Object (using RhinoCommon.dll)?

ps. doing all this in Python3

The short answer is that it is a RhinoCommon object!

RhinoCommon is a .NET wrapper library around the Rhino C++ SDK. RhinoCommon only runs inside Rhino. Rhino3dm is a set of wrapper libraries (.NET, Python and JavaScript) around the public parts of the SDK, a.k.a. openNURBS. Rhino3dm can run outside of Rhino.

Under the hood, your mesh object is an openNURBS mesh. You can use the rhino3dm python library to write this mesh to .3dm file, and then open it in Rhino, or serialise it to JSON and send it to a web server.

# sample: create model, add mesh and write to .3dm file
model = rhino3dm.File3dm()
model.AddMesh(mesh)
model.Write('example.3dm')

What is it that you want to do with this mesh object? Knowing a bit more about how you want to use rhino3dm will help us to provide some more relevant information. :slight_smile:

thanks for your response! I am rewriting some code that I had before in C# in Python3 within the CPython components in GH. I can easily import Rhino3dm in Python 3 and write 3dm files to open externally but cannot convert them into mesh objects digestible by grasshopper. Importing grasshoper in the CPython does not seem to be possible; so I was hoping I could use RhinoCommon to convert this Rhino3dm memory object into a Rhino object, i.e. without writing it into a separate 3dm file. Would that be possible?

Is there a reason you can’t use the built-in GhPython script components?

Hi, hope it’s okay that I piggyback on this thread, as I have exactly the same question.

We have a dll containing references to Rhino3dmIO, let’s call it ClientLib.dll. We want to be able to use this dll without a rhino process. It basically just reads and writes rhino data.

Now we have another dll that has references to RhinoCommon, let’s call it ServerLib.dll. This dll is used together with a Rhino process. ServerLib.dll also references ClientLib.dll to get access to client models/logic. In other words, it indirectly references Rhino3dmIO too.

The issue now is that rhino data produced in ServerLib.dll can’t be passed to ClientLib.dll, because it expects Rhino3dmIO objects. Therefore I’m looking for a way to convert RhinoCommon objects into Rhino3dmIO objects, so that the two libraries can talk to each other.

We have previously serialized the rhinocommon objects into json, and then deserialized it back into rhino3dm to achieve this, but we were hoping to find a cleaner approach with better performance, as both rhinocommon and rhino3dm are available in the process.

I hope this makes sense. Let me know if this can be achieved in some other way, or if I’m completely lost ^^

ping @stevebaer @will

Hi @emilpoulsen,

I ran into this when I wrote the Rockfish tool while sitting in your office a few years ago.

I think you might need a SerializationBinder. Here is an example:

– Dale

Hah, that’s awesome! Thanks alot @dale, really appreciate the quick response. I’ll give the serialization binder approach a go and let you know how it turned out :pray:

Thanks again @dale, your method with the SerializationBinder works really well. I was even able to extend it to work with the structs that don’t derive from the GeometryBase such as Point3d, Vector3d, Circle etc.

The only issue I ran into was the Matrix class, which doesn’t seem to be serializable:

Type ‘Rhino.Geometry.Matrix’ in Assembly ‘RhinoCommon, Version=6.31.20315.17001, Culture=neutral, PublicKeyToken=552281e97c755530’ is not marked as serializable.

Is this by design? If so, are there any workarounds?

1 Like

It wasn’t intentional, we just never got around to making Matrix support ISerializable. We can probably do this in the future, but for now you could just turn the Matrix into a rectangular array of doubles and then back into a Matrix.

Ok, good to know, thanks @stevebaer. Let us know when there is a version of rhino which has the ISerializable implementation on the Matrix type. Probably no stress from our side - I just tried out what you suggested and that seems to be working pretty well:

extern alias R3dmIo;
extern alias RCommon;
using R3 = R3dmIo::Rhino.Geometry;
using RC = RCommon::Rhino.Geometry;

/// <summary>
/// Converts a RhinoCommon Matrix to Rhino3dmIO Matrix
/// </summary>
/// <param name="rcommonMatrix">The RhinoCommon matrix</param>
/// <returns>The converted Rhino3dmIO matrix</returns>
public static R3.Matrix ConvertMatrixFromRCommonToR3dm(RC.Matrix rcommonMatrix)
{
    R3.Matrix r3dmMatrix = new R3.Matrix(rcommonMatrix.RowCount, rcommonMatrix.ColumnCount);
    for (int i = 0; i < rcommonMatrix.RowCount; i++)
    {
        for (int j = 0; j < rcommonMatrix.ColumnCount; j++)
        {
            r3dmMatrix[i, j] = rcommonMatrix[i, j];
        }
    }
    return r3dmMatrix;
}

Cheers
Emil

Hi @emilpoulsen,

I’ve logged it issue with Matrix.

https://mcneel.myjetbrains.com/youtrack/issue/RH-61803

– Dale

1 Like

That’s great, thanks @dale!