# GhPython add-on compiler with example

**URL:** <https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307>\
**Category:** Scripting\
**Created:** [September 1, 2014, 4:24pm UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307 "2014-09-01T16:24:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![piac](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/piac/32/769366_2.png) [@piac](https://discourse.mcneel.com/u/piac)\
**Post date:** [September 1, 2014, 4:24pm UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/1 "2014-09-01T16:24:44Z")

</div>

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:

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

class MyComponent(component):
    
    def __init__ (self):
        super(MyComponent,self). __init__ ()
        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):
        try:
            args.Display.DrawDot(self.L.X, self.L.Y, self.x, System.Drawing.Color.Black,
                System.Drawing.Color.White)
        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:

```python
import clr
clr.CompileModules("result.ghpy", "myuniquemodule1.py", "myuniquemodule2.py", ...)

```

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

–  
Giulio Piacentino  
for Robert McNeel & Associates  
[giulio@mcneel.com](mailto:giulio@mcneel.com)

---

<div class="post-metadata">

**Author:** ![piac](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/piac/32/769366_2.png) [@piac](https://discourse.mcneel.com/u/piac)\
**Post date:** [September 1, 2014, 4:27pm UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/2 "2014-09-01T16:27:08Z")

</div>

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

[billboard.gh](https://sea3.discourse-cdn.com/mcneel/uploads/default/11825/682579986f6db3a2.gh) (7.9 KB)

---

<div class="post-metadata">

**Author:** ![djordje](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/djordje/32/747634_2.png) [@djordje](https://discourse.mcneel.com/u/djordje)\
**Post date:** [September 2, 2014, 9:50pm UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/3 "2014-09-02T21:50:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![piac](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/piac/32/769366_2.png) [@piac](https://discourse.mcneel.com/u/piac)\
**Post date:** [September 3, 2014, 7:06am UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/4 "2014-09-03T07:06:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [November 14, 2014, 2:58pm UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/5 "2014-11-14T14:58:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![djordje](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/djordje/32/747634_2.png) [@djordje](https://discourse.mcneel.com/u/djordje)\
**Post date:** [November 18, 2014, 3:53pm UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/6 "2014-11-18T15:53:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![piac](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/piac/32/769366_2.png) [@piac](https://discourse.mcneel.com/u/piac)\
**Post date:** [November 18, 2014, 7:47pm UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/7 "2014-11-18T19:47:50Z")

</div>

> [@djordje](#):
>
> the new compile feature works only if code is written in a form of a class?

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

Giulio Piacentino  
for Robert McNeel & Associates  
[giulio@mcneel.com](mailto:giulio@mcneel.com)

---

<div class="post-metadata">

**Author:** ![bringley](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/bringley/32/171399_2.png) [@bringley](https://discourse.mcneel.com/u/bringley)\
**Post date:** [November 20, 2014, 9:09pm UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/8 "2014-11-20T21:09:38Z")

</div>

This is awesome!

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

![](https://sea3.discourse-cdn.com/mcneel/uploads/default/13849/8e185de8dbc17920.jpg)

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

---

<div class="post-metadata">

**Author:** ![piac](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/piac/32/769366_2.png) [@piac](https://discourse.mcneel.com/u/piac)\
**Post date:** [November 20, 2014, 9:34pm UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/9 "2014-11-20T21:34:53Z")

</div>

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.

Thanks,

## Giulio

Giulio Piacentino  
for Robert McNeel & Associates  
[giulio@mcneel.com](mailto:giulio@mcneel.com)

---

<div class="post-metadata">

**Author:** ![bringley](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/bringley/32/171399_2.png) [@bringley](https://discourse.mcneel.com/u/bringley)\
**Post date:** [November 20, 2014, 9:51pm UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/10 "2014-11-20T21:51:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vijesh](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/vijesh/32/509577_2.png) [@vijesh](https://discourse.mcneel.com/u/vijesh)\
**Post date:** [December 1, 2023, 6:57am UTC](https://discourse.mcneel.com/t/ghpython-add-on-compiler-with-example/11307/11 "2023-12-01T06:57:52Z")

</div>

How to do it in Rhino 8?  
Vijesh
