Grasshopper TriRemesh from Rhino.Python with ghpythonlib.components

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.

Am I missing something ?
If the function is not available, would it be possible to implement it ?

Thanks,
Aymeric

Looks like it’s available as ghc.Kangaroo2Component.TriRemesh(). I haven’t tried all the options but a simple test works fine here.

1 Like

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
1 Like

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.

  1. Importing
try: # this works for first launch
    import ghpythonlib.components as ghc
    ghc_loaded = True

except Exception as e:
    ghc_loaded = False
    print(e)
  1. Using
try:
    if ghc_loaded:
        self.InterMesh, dual, creases = ghc.Kangaroo2Component.TriRemesh(
        geometry = InitialPolySrf_Brep,
        sharp = True,
        length = self.meshing_value,
        iterations = 100
        )
    else:
        print("remeshing with Rhino.NodeInCode")
        self.InterMesh, dual, creases = Rhino.NodeInCode.Components.NodeInCodeFunctions.Kangaroo2Component_TriRemesh(
        geometry = InitialPolySrf_Brep,
        target = None,
        sharp = True,
        feature = None,
        length = self.meshing_value,
        iterations = 100
        )
except Exception as e:
    print(e)

Note that Rhino.NodeInCode.Components.NodeInCodeFunctions.Kangaroo2Component_TriRemesh requires exactly 6 parameters,
I added target = None, feature = None
but throws an error (TypeError(“Invoke() got an unexpected keyword argument ‘geometry’”,)

@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.

Here is an example that works for me:

import Rhino
import System
import scriptcontext as sc
import rhinoscriptsyntax as rs

surf_guid = rs.AddSphere([0.0, 0.0, 0.0], 10.0)
surf_obj = sc.doc.Objects.FindId(surf_guid)
surf = surf_obj.Geometry.Surfaces[0]

(
    interMesh,
    dual,
    creases,
) = Rhino.NodeInCode.Components.NodeInCodeFunctions.Kangaroo2Component_TriRemesh(
    surf, None, True, None, 2.0, 100
)

sc.doc.Objects.AddMesh(interMesh[0])
sc.doc.Objects.AddMesh(dual[0])
print(interMesh)
print(dual)
print(creases)

rs.Redraw()

Thank you very much, @pierrec,
your code solved “TypeError(“Invoke()…”, and I was so happy about it, but not for long :slight_smile:
now it throws another error:

Kangaroo2Component.TriRemesh: solver component. error: Solution exception: 
Object reference not set to an instance of an object.

:roll_eyes:
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()