GhPython add-on compiler with example

(Giulio Piacentino) #1

Hi all,

I would like to introduce the new GhPython compiler feature we are developing for Rhino in Serengeti.
This is an early build, and it is the first time this is shown to the public, so some “rough edges” might still be there. Keep that into account.

You are encouraged to write comments below here. I am also posting a example below here.

Step by step example

A component that shows a textdot relative to screen position (“billboard”).

1 We begin with a customary GhPython component. Please note that these features are only available in Seregeti right now.
We can add and remove inputs by zooming in and out, and change the input names as usual.

2 We put together a small definition for testing the script. We can override the icon by drag-and-dropping 24x24 png’s.

3 We switch to component compilation mode. This mode allows to write a full class that represents the object that runs the script. This follows the Grasshopper SDK IGH_Component interface, but is technically not exactly it.

4 The component code is replaced with a longer version, similar to this one:

5 We need to type the full code. In this case:

from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import rhinoscriptsyntax as rs

class MyComponent(component):
    def __init__(self):
        self.x = ""
        self.L = Rhino.Geometry.Point2d(0,0)
    def RunScript(self, x, L):
        self.x = str(x)
        if L is not None:
            self.L = Rhino.Geometry.Point2d(L.X, 1000-L.Y)
    def DrawViewportWires(self, args):
            args.Display.DrawDot(self.L.X, self.L.Y, self.x, System.Drawing.Color.Black,
        except Exception, e:
            System.Windows.Forms.MessageBox.Show(str(e), "script error")
    def get_ClippingBox(self):
        return Rhino.Geometry.BoundingBox()

Our definition is ready.

We hit “Compile” and fill the form.

We just need to save the .GHPY to the default folder, and Grasshopper will load it automatically.

If you want, you can also export the compilable code and compile it yourself with this short code:

import clr
clr.CompileModules("result.ghpy", "",  "", ...)

Only one gotcha (automatically taken care of in the default “Compile…”): your modules need to have unique names, otherwise a non-unique module might shadow another one.

Please let me hear your thoughts and comments.


Giulio Piacentino
for Robert McNeel & Associates

Button and context menu options on component through GhPython
Displaying text with location based on points using Python Scripted Components
Optimizing displaypipeline performance
GHpython component development
(Giulio Piacentino) #2

Here, the definition. Please ensure you use the latest build if you are just “casually testing”. (7.9 KB)


Very interesting!
I can see two new categories (Edit and Tools) were added to the application menu toolbar of the ghpython component. I suppose indentation options, Find/Replace were added there? Do these two include debugger by any chance?

(Giulio Piacentino) #4

Not for now, but this is definitely on the wishlist.


Just got on the Serengeti forum and just have to say that this is nothing short of awesome!! Excellent work guys :slight_smile:


I have not been able to test Rhino 6 WIP.

Giulio, so basically the new compile feature works only if code is written in a form of a class?

(Giulio Piacentino) #7

Yes. This exposes the functionality of GH_Component to users of GhPython, so you are effectively inheriting from it when the code is compiled. For the dynamic preview, there is a class that reproduces the behavior of GH_Component, but can be scripted “live”.


Giulio Piacentino
for Robert McNeel & Associates


This is awesome!

I’m trying to complile the sample file you provided and am getting this error.

Windows 7 64bit
Rhino 6.0 (11/18/2014)
Grasshopper 1.0.0004

(Giulio Piacentino) #9

I wish I could reproduce. Could you save the file exactly as it is, and send it to me?
This way I can debug it.



Giulio Piacentino
for Robert McNeel & Associates


Hi Giulio I downloaded your sample file, made sure that I was in Component Compilation mode, and then hit Compile. I did not edit the code.

I was able to edit details in the GhPython Compilation Details window but when I hit Compile I get the error I posted.