Running Rhino commands in Grasshopper Python

@Mr_Sinter,

check out the example below, with the attached DrillHole.gh file. Basically the script does the following:

  • Select a solid brep (eg, a sphere or box)
  • Get the Grasshopper plugin object
  • Open the DrillHole.gh file
  • Pass the id of the brep to an existing slot in gh file
  • Change any parameter in the gh file
  • Run the Grasshopper solver
  • Bake the resulting brep to rhino and select it
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DrillHoleWithGrasshopper():
    id = rs.GetObject("Select a closed brep", 16, True, False)
    if not id: return False
    
    Grasshopper = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
    if not Grasshopper: return False
    
    Grasshopper.OpenDocument("C:\DrillHole.gh")
    Grasshopper.DisableSolver()
    Grasshopper.AssignDataToParameter("input_brep", id)
    Grasshopper.AssignDataToParameter("circle_radius", 2.0)
    Grasshopper.RunSolver(True)
    
    objs = Grasshopper.BakeDataInObject("output_brep")
    if not objs: return False 
    rs.SelectObjects(objs)
    
    Grasshopper.CloseDocument()
    Grasshopper.HideEditor()
    
if __name__=="__main__":
    DrillHoleWithGrasshopper()

Do not forget to change the file path in the script above which loads the DrillHole.gh file into Grasshopper.

DrillHole.gh (8.4 KB)

c.

6 Likes