I would like to use the wonderful TriRemesh function available in Grasshopper from a Rhino Python script.Following the guide here, it seems that TriRemesh function is not available through this method.
Thank you for your answer. Works fine to the extent of my needs.
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
import ghpythonlib.components as ghc
__commandname__ = "TriRemesh"
# RunCommand is the called when the user enters the command name in Rhino.
# The command name is defined by the filname minus "_cmd.py"
def RunCommand( is_interactive ):
go = Rhino.Input.Custom.GetObject()
go.SetCommandPrompt("Select a mesh")
go.EnablePreSelect(True, True)
go.GeometryFilter = Rhino.DocObjects.ObjectType.Mesh
go.Get()
if go.CommandResult()!=Rhino.Commands.Result.Success:
return 1 # 1==failure
objMesh = go.Object(0).Object()
mesh = objMesh.Geometry
try:
remeshed, dual, creases = ghc.Kangaroo2Component.TriRemesh(
geometry = mesh,
sharp = True,
length = 1000,
iterations = 1000
)
sc.doc.Objects.Delete(objMesh)
sc.doc.Objects.AddMesh(remeshed)
# sc.doc.Objects.AddMesh(dual)
# for crease in creases:
# sc.doc.Objects.AddLine(crease)
sc.doc.Views.Redraw()
except:
return 1 # 1==failure
return 0 # 0 == success
Hi,
There is a known issue (link) with loading a script containing âimport ghpythonlib.componentsâ more than once ==> Rhino throws an error (ImportError(âNo module named GhPython.Assembliesâ,)
My attempt to find a solution with Rhino.NodeInCode,. hope you can help me get this working.
Importing
try: # this works for first launch
import ghpythonlib.components as ghc
ghc_loaded = True
except Exception as e:
ghc_loaded = False
print(e)
@monkelisha It looks like keyword arguments are not allowed when you use the pInvoke functions in NodeInCode. Just pass the same arguments as regular positional arguments.
Thank you very much, @pierrec,
your code solved âTypeError(âInvoke()âŚâ, and I was so happy about it, but not for long
now it throws another error:
Kangaroo2Component.TriRemesh: solver component. error: Solution exception:
Object reference not set to an instance of an object.
I just totally copy/pasted your code with sphere, and this I got.
â after a couple of hours â
But this code works in Rhino 8 WIP !!
Im so happy⌠that will do for my experiments.
For those who may encounter such an error when reloading script with âimport ghpythonlib.componentsâ, thanks to Pierre Cuvilliers, here is a simple example of a working code:
import Rhino
import System
import scriptcontext as sc
import rhinoscriptsyntax as rs
try:
import ghpythonlib.components as ghc
ghc_first_load = True
except Exception as e:
ghc_first_load = False
print(e, 'but thanks to Elisha and Pierre, code still works well!')
surf_guid = rs.AddSphere([0.0, 0.0, 0.0], 10.0)
surf = sc.doc.Objects.FindGeometry(surf_guid)
if ghc_first_load:
TriMesh, dual, creases = ghc.Kangaroo2Component.TriRemesh(
geometry = surf,
sharp = True,
length = 2,
iterations = 100)
else:
(im, dual, creases) = Rhino.NodeInCode.Components.NodeInCodeFunctions.Kangaroo2Component_TriRemesh(
surf,
None, True, None,
2,
100)
TriMesh = im[0]
sc.doc.Objects.AddMesh(TriMesh)
rs.Redraw()