Rhinocommon Array[mesh]

Hi all,

I am trying to write a mesh to a .obj file using Rhinocommon from Python. Specifically I am using:

write_options = Rhino.FileIO.FileWriteOptions()
Rhino.FileIO.FileObj.Write(folder_path, mesh, write_options)

However, I get an error stating that an Array[Mesh] is expected instead of a Mesh. Could anyone explain how to transform a single Rhino.Geometry.Mesh() into an array of meshes? Unfortunately, I did not see it as an available class in Rhino.Geometry.Collections.

Error: Runtime error (ArgumentTypeException): expected Array[Mesh], got Mesh

Thank you for your time!

If just putting it into a list doesn’t work via the automagick, I would try a System.Array or System.Collections.ArrayList.

A lot of those .NET types under the hood are generic types, and e.g. you have to restrict their contents to specified types. That’s not a problem if you only have a single element, but you might need to first construct a concrete type using something like:

from System.Collections.Generic import List
TypeHintList = List[Grasshopper.Kernel.Parameters.IGH_TypeHint]

Thanks,

Do you know if that would also work if I need to run the code outside GHPython, e.g. Python 3 inside VS code?

list ([…]) will work. System is an IronPython only library, so probably not. But I don’t know what the CPython projects that can import Rhino have available.

The notation is worth learning, as coincidentally the .NET generic syntax is much the same as CPython 3 typing annotations. It’s also definitely possible to make that code work on both platforms, by building a dummy System.Array type on CPython >= 3.7 that behaves the same way, by subclassing list and implementing __class_getitem__ on it.

Thanks for the suggestions! Will take a look how to proceed. Perhaps I’ll just turn to an alternative meshing lib in CPython for saving meshes to obj.

Apparently it was easier to just dump my meshes into a json file. Also solves the problem, though not with obj.

Well done. If it works, it works. .json lets you take a peek inside the file to double check it looks right, before carrying out an expensive process too.

1 Like