Hi, I’m trying to understand how to use RhinoCommon in Python, in this specific case, Brep.CreateSolid. I’ve got to the point that my code does not produce any errors, but the output is empty? Ive been trying both in GH Python and in RhinoScript, and I can’t even figure how to proceed.
Attached is the example - one with couple surfaces, which produces nothing, one with some spheres defined in script, which somehow passes through the original spheres. Does the output of CreateSolid work differently than I think? Am I passing it the list of surfaces incorrectly?
Thank you for any advice.
import Rhino
import Grasshopper
import rhinoscriptsyntax as rs
from System import Array
srfs = [rs.coercebrep(tnurbs),rs.coercebrep(bnurbs),rs.coercebrep(cutsrf)]
listed = Array[Rhino.Geometry.Brep](srfs)
imp = Rhino.Geometry.Brep.CreateSolid(listed,Rhino.RhinoMath.UnsetValue)
CreateSolid.gh (11.1 KB)
Try posting a manually modelled Rhino file of what you’re expecting. But here’s a quick GhPython implementation of Brep.CreateSolid. It should be the same with the new script editor (in IronPython at least):
241004_CreateSolid.gh (8.6 KB)
That is the result that I would like to see, yes - the result of Create Solid in Rhino.
So it is a list/data type issue then. The reason I am trying to create the list inside the code is related to how I am gathering the objects to create the closed solid. In the end, it would be a RhinoPython script, I switched to GH for troubleshooting.
Would you please show me how to create the list inside the code, from the three individually input breps?
Sure thing, a standard Python tuple or list works with this method:
241004_CreateSolid.gh (10.0 KB)
Some RhinoCommon methods do require one to wrap data in e.g. typed .NET arrays, which is maybe what is causing the confusion. Also, using RhinoMath.UnsetValue as the tolerance probably would cause an issue.
Thank you, now it at least works in GH. I got to that point by starting with RhinoPython editor, and I tried to find solutions for why I wasn’t getting back results/ was getting errors.
But my goal is still RhinoPython, and I have issues still. My cleaned up code in Rhino is below, and for some reason, the CreateSolid command returns an empty Brep object. I know I am getting the three objects - tested by Brep.Duplicate them back into Rhino, and it does work in GH with the same three objects.
Would you please have any pointers on why I get back an empty Brep?
import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
tnurbs = rs.ObjectsByLayer("layer_name1")
bnurbs = rs.ObjectsByLayer("layer_name2")
cutsrf = rs.ObjectsByLayer("layer_name3")
srfs = (rs.coercebrep(tnurbs),rs.coercebrep(bnurbs),rs.coercebrep(cutsrf))
sc.doc.Objects.AddBrep(Rhino.Geometry.Brep.CreateSolid(srfs, 0.001))
sc.doc.Views.Redraw()
Maybe try inspecting what is returned from each of the coercebrep calls before adding them to the tuple, and check if the breps are valid.
The problem has to be with the code, all inputs are valid. I setup an example file with the cleanest possible geometry - see attached. I am still getting this error message:
Rhino.Geometry.Brep[] value cannot be converted to Rhino.Geometry.Brep in method System.Guid AddBrep(Rhino.Geometry.Brep)
Or no output when using CoerceBrep before the AddBrep
CreateSolidExample.3dm (107.5 KB)
I see you’re using Rhino 8. This might be another interop issue with the rhinoscriptsyntax module and CPython 3. Can you try checking which version of Python you’re executing the code in:
I don’t recall, but I think there is a method for changing the Rhino script editor to go back to using IronPython 2.7 again. That might be your next step if you’re running CPython 3.9.
Edit: Just for clarity, it’s not that CPython can’t run the CreateSolid method, but more likely an interop issue with the rs.coercebrep function and the Python.NET framework being used behind the scenes:
241007_CreateSolid.gh (7.1 KB)
Using Iron Python, I got much clearer error message - it returns an array, and the Coercebrep cant handle it.
Thank you very much for your help.