Tutorial: creating a Grasshopper component with the Python GHPY compiler

@piac, thank you for the post. Is there a solution to automate the whole process for several component or python files? I’m still not clear about the strcuture of the files to compile several files. For instance, I have 51 files for butterfly. What would be the best approach?

Hi @Mostapha!

First of all, this method does not replace any previous working method, so you can keep on using importing from folders, having all code in a single component, etc, for as long as you want.

You can compile as many files as you want into the library. There can be a Python script that forms the list for you, to then feed into CompileModules(). That is all you should need. Does it help?

Hi @piac, It does. I think my question is how to put the script together and if I can access the functionalities that you showed within the script. Currently we have a component for ladybug that automates the process of creating userobjects from a GHPython component from inside Grasshopper without having to deal with any form, and I was thinking to devlope a similar component to compile them. Can you share a code snippet on how to get this started?

OK; I see where you are aiming now. Let me have a look and (try to) write something useful.

Hi @piac, thanks a lot for the post! it is really useful.

Just wondering, do you think if it is possible to compile directly within Visual Studio?
I think I am missing the GhPython and ghpythonlib assemblies. Since it is shipped directly within Grasshopper in WIP, I am not really sure where to find those. Any ideas?



1 Like

Hi Giulio,

I’m going off on a tangent now, but can you elaborate on the possibilities to compile standalone IronPython scripts this way.
For a while now I’ve been pondering how I can gain speed and get some form of compiling to protect the code from prying eyes ( at least get some level of hiding the source), without delving into C# or the like.

Am I correct to assume that there might be a way to apply this type of compiling to rhinopython scripts as well?
Maybe copile modules as dll’s?


Hi @PaulPoinet

If you have exactly the same version of IronPython as the one that ships with Rhino, I think it should be possible.

  • GhPython is the Grasshopper .gha file, a .Net assembly. It is located at

%programfiles%\Rhino WIP\Plug-ins\Grasshopper\Components\

  • ghpythonlib is a Python package (a directory containing modules) that is located right beside the rhinoscriptsyntax. That is,

%appdata%\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib


Hi Willem

As it was stated earlier, it is possible to use clr.CompileModules() to compile any module. You will have to solve a series of technical hurdles that the GHPY solution provides you with, but it should be possible. The biggest issues are locating the .dll, and providing a way to differentiate assemblies for different IronPython language versions.

If you are considering _EditPythonScript scripts for this, there is the RhinoScript “compiler”. It is listed here. It takes all your code and puts it into a giant string, that will be later run by Rhino. This approach offers less protection and there is no need to “recompile” for different IronPython languages.

There isn’t a system that works like this one, for standalone scripts, AFAIK. Sorry.

1 Like

Thanks for the locations. I should have dig a bit more…
It works well from VS :slight_smile:

Yay :slight_smile: all working! Many thanks for setting this up for us Giulio!

Just as a matter of feedback. The workflow does it all for us, and since this is so accessible now, it would be nice to comment what the classes that assemble and the functionality that defines a component do. There may be some people that never compiled in C# or VB before that would like to understand what those are doing.

I will try to call PyDev modules now.

Yay again :slight_smile:

Hi @Mostapha

I made changes to the compiling SDK and the last set of addition (ability to call from code the same compiler tool that is used in the wizard) is in the Rhino WIP of last week – 6.0.17010.13211.

I am attaching here a definition that shows how that works.
Please let me know if this is useful.

compiling-sdk.gh (14.0 KB)


Hi @piac, This is very helpful. Thanks!

A word of advice, in case anyone else is having the problem I had earlier…

I was getting a trace error, something along the lines of:
Void Microsoft.Scripting.Utils.ExceptionUtils.RemoveData(System.Exception, System.Object)

Installing the latest IronPython fixed this issue

Hi @Matthew_Breau, in general there should be no need to install IronPython separately. You were doing that for other reasons, right?

I found that trying to switch from procedural mode to the GH Component SDK Mode would cause the error I mentioned above. Installing the latest IronPython eliminated the error.

There are general problems when using independent IronPython installs. They should be avoided, or the exactly matching version should be installed, when possible. This is a Rhino-wide limitation, not a specific one of Grasshopper, GhPython or the EditPythonScript editor.

Hi Giulio thank for this post. I have an issue after compiling. It display an error message “Illegal character in path. Try copying the source and compiling directly” (see pic). Copying from where to where exactly? Cheers F.

@Fabio_S, you can just click on “Copy Compilable Code” (Part 2 of the tutorial above).
Did you put a space in the module name? Or other special characters?

Thanks a lot Giulio… it was all because of a hyphen in my module name!?

1 Like