Voronoi: grasshopper nodes into PythonScript?

Hello all,

I would like to translate this grasshopper defintion below into gypthon (pythonScript for Grasshopper) but unsure how to go about it… Could someone please help me out?

the easiest (most newbie) way is to use node-in-code.

Whether it is practical or meaningful rather than making it all using RhinoCommon many would argue.

It is definitely good for prototyping.

One of the down sides currently is that if you run your scripts from Rhino (no GH running). It will load Grasshopper with all its plugins and you have to wait for the GH Splash to go away.

2 Likes

I think it’s the only way for voronoi atleast considering voronoi isnt in Rhinocommon. Haven’t explored node in code, but I think it won’t work for plugins (since the end parts are weaverbird)? Or does it?

1 Like

It does work with plugins :slight_smile:

image

2 Likes

Sorry, I meant if you wanted to use it elseware that computer would also need the plugin? Cool that works in general tho.

1 Like

Good point.

I only used it on my workstation and home pc. And have the same plugins installed on both.

Perhaps in the future Yak could be more advanced to take care the installations of plugins within node-in-code. :slight_smile:

2 Likes

One can implement the Grasshopper.Kernel.Geometry namespace directly in GHPython. Old (very old) example here (down a bit, direct link to the post doesn’t work):

GhPythonVoronoi_2 (1).gh (50.4 KB)

3 Likes

Hello All!
Base on your post I add a free curve to the inputs to proyect and split the voronoi.
I share the code here
Thanks!

# Inputs List Point3d points, Curve Boundary

import clr
clr.AddReference("Grasshopper")
import Grasshopper as gh
import Rhino as rc

def voronoi2D(nodePts, boundary):
    # Create a bounding box around the boundary curve
    bb = boundary.GetBoundingBox(True)
    d = bb.Diagonal
    dl = d.Length
    f = dl / 15
    bb.Inflate(f, f, f)
    bbCorners = bb.GetCorners()

    # Create a list of nodes from the points
    nodes = gh.Kernel.Geometry.Node2List()
    for p in nodePts:
        n = gh.Kernel.Geometry.Node2(p.X, p.Y)
        nodes.Append(n)
    
    # Create a list of outline nodes using the bounding box corners
    outline = gh.Kernel.Geometry.Node2List()
    for p in bbCorners:
        n = gh.Kernel.Geometry.Node2(p.X, p.Y)
        outline.Append(n)

    # Calculate the Delaunay triangulation
    delaunay = gh.Kernel.Geometry.Delaunay.Solver.Solve_Connectivity(nodes, 0.1, False)

    # Calculate the Voronoi diagram
    voronoi = gh.Kernel.Geometry.Voronoi.Solver.Solve_Connectivity(nodes, delaunay, outline)

    # Create a boundary surface from the boundary curve
    boundary_surface = rc.Geometry.Brep.CreatePlanarBreps(boundary)[0]

    # Project Voronoi polylines onto the boundary surface
    projected_polylines = []
    for c in voronoi:
        pl = c.ToPolyline()
        poly_curve = rc.Geometry.PolylineCurve(pl)
        
        # Project the polyline onto the boundary surface
        projection = rc.Geometry.Curve.ProjectToBrep(poly_curve, boundary_surface, rc.Geometry.Vector3d.ZAxis, 0.01)
        
        if projection:
            projected_polylines.append(projection[0])  # Take the first projection result

    # Split the boundary surface with the projected Voronoi curves
    split_surfaces = boundary_surface.Split(projected_polylines, 0.01)
    
    # Convert split surfaces to boundary curves
    trimmed_polylines = []
    if split_surfaces:
        for surface in split_surfaces:
            edges = surface.Edges
            for edge in edges:
                trimmed_polylines.append(edge.ToNurbsCurve())
    
    return trimmed_polylines, projected_polylines

# Inputs from Grasshopper
nodePts = points  # The input points for Voronoi generation
boundary = Boundary  # The boundary curve

# Generate the Voronoi diagram
voronioCells, projected_VoronioCells = voronoi2D(nodePts, boundary)

# Output to Grasshopper
V = voronioCells  # The Voronoi cells trimmed to the boundary
Projected_V = projected_VoronioCells  # Projected Voronoi cells for debugging


Python3Scipt_Voronoi-CurveBoundary.gh (17.2 KB)