ghPython – Outside the Canvas
As a follow up to my post about the new features in ghPython, I wanted to point out another feature now available; running Grasshopper components outside of the Grasshopper canvas.
You will need to have installed ghPython and have used the component in grasshopper at least once. We will fix the “at least once” issue in a future release of ghPython.
- Start Rhino
- Run EditPythonScript and enter the following script
import rhinoscriptsyntax as rs import ghpythonlib.components as ghcomp import scriptcontext points = rs.GetPoints(True, True) if points: curves = ghcomp.Voronoi(points) for curve in curves: scriptcontext.doc.Objects.AddCurve(curve) for point in points: scriptcontext.doc.Objects.AddPoint(point) scriptcontext.doc.Views.Redraw()
- Run the python script
ghpythonlib (both components and parallel) modules are available in the standard python editor in Rhino. Behind the scenes things are running through Grasshopper code, but you don’t have to use a canvas to do your work.
This also lets you work in a slightly different way where you can get points in Rhino using rhinoscriptsyntax “get input” type functions and pass those points (or curves or breps) into the Grasshopper component code.
Great work Steve.
This is probably the best Christmas present.
One question: when combining the rhinoscriptsyntax functions and these new grasshopper component ones: passing guids is not possible, right?
For example using rs.GetObjects() instead of rs.GetPoints would not work?
These new grasshopper component functions require either object type data (Point3d, Polyline…) or data in the form of lists/tuples (point = [2,0,6])?
That is correct. The component functions would not know how to deal with guids. In those cases you would need to use the “coerce” functions in rhinoscriptsyntax (coercecurve, coercebrep, …) to get the geometry from the guids and then pass those on to the component functions.
Steve, I noticed that “scriptcontext.doc.Objects.” and after that “scriptcontext.doc.Views.Redraw()” is always called behind each rhinoscriptsyntax function which creates geometry. Seems that way geometry is added to Rhino.
What about baking geometry from Grasshopper? We do not need “scriptcontext.doc.Objects.” for that?
Is there some documentation explaining this?
I’m not sure I completely understand your question. It would probably be easier to discuss this at http://discourse.mcneel.com where I can bring other people into the discussion who may be more helpful.
hello steve ,
i have a problem …
ghpythonlib does load in grasshopper but it doesnt load in editpythonscript in rhino , whats shoukld i do !?
Hey Arash,
In case you couldn’t solve the problem: Are you sure ghpythonlib didn’t load? I thought the same thing because I got no auto-complete while writing the import statement, but after typing it in anyway the function calls worked.
Hi, Can we use the usual python XLSX libraries for taking points and automate the process of multiple curve generation?
Hi ,can we use the XLSX libraries of python to take points and parameters directly from the excel sheet to automate the generation of multiple curves?
I haven’t tried this myself, but that library looks like it may work.