top of page
Search
  • Writer's pictureWill

Move from casual GhPy to .NET Grasshopper dev.

Since the last post I have moved onto developing compiled component for Grasshopper for the office. The process didn't start too easily. Python was my language of choice. It's still to a large extent a good prototyping language. However in the context of making bespoke components in Grasshopper, nothing beats the stability and speed of compiled ones written with C#.


The first order of business was definitely following through the workshop recorded on Long Nguyen's YouTube channel. He's done this a couple of times and each varies a little bit in contents. But both are very informative and thorough for someone who's not trained in the software developer trade.


After I got the pattern down, which believe it or not wasn't the hardest part once I had a list of components to write, the more intricate part of the experience was to learn the subtle, less obvious capabilities that came with coding in C#. I'm going to try to enumerate a few, not in any particular order below.


The Intellisense that comes from an IDE is definitely a life saver. With the understanding of computational geometry, it is relatively easy to guess what needs to be done most of the time after suggested code is displayed. There is the popular template for Visual Studio and that is what I use. VS automates a lot of the coding. For example, each method call will suggest what input arguments should be supplied. When a certain enum value is needed, VS even suggests where that enum is in the libraries.


C# is typed, meaning that if there is a mismatch VS will scream at you (visually of course with squiggly lines/lints). I can't assign an integer to a Brep variable. This is a huge difference from python but what seems to be extra rigor on the coder's end proves to be a good rule book by which Intellisense "proof-reads" your codes. Having certainty in object types also gives the coder insight into properties and methods associated with them. It's an awareness that is hard to foster by programming exclusively in python.


Specifically for Grasshopper component development, VS Intellisense can also suggest many of the methods and properties under GH_Component class. This way it's easier to modify the behavior of the component itself. If I were to override some of the methods on a component, GhPy definitely would not have enough hints. Not only would I have to dig though the API reference but also trying to guess how a C# script is re-written in python. When I was not so knowledgeable in C#, that translation often led nowhere. A few common overrides are appending new menu items to the right click drop down. In the dev VS template, once I call override, the code autocompletes the basic structure and it becomes obvious that some of the argument inputs were from the Windows.Forms namespace. In python I would have no idea and therefore couldn't even start looking for API references.


And perhaps best of all, compiled script is lightening fast compared to JIT compiler of GhPy. GhPy had a way of compiling to a .ghpy file which can be loaded into Grasshopper just the same way .gha files are. I thought that would be my golden ticket to the world of bespoke Grasshopper tools. However, after making .gha libraries for a while, it's unbearable to watch how long it takes Grasshopper to load .ghpy libraries. Did I mention multi-threading? Because VS is pretty much made by the guys who created the whole .NET sphere, there are some parallel task libraries built into .NET and they are fully Intellisense compatible. I know python has library that handles multi-thread as well but for IronPython, which is based on pyhton 2, the options are looking a little limited compared to what I can do in VS.


There is one big downside of switch to developing in C#. The native code editor in Grasshopper for C# is actually a little more cumbersome than the GhPy component. That means there isn't quite a place where I can type some code and run the script on the spot. With python, I can type a few lines and see what errors I might get. With C# dev, it usually means restarting Rhino entirely. I could test a few things out in the C# editor but its limited auto-completion makes it difficult to use, more difficult than GhPy which arguably has sub-par Intellisense.


A takeaway is that trying to code in C# for Grasshopper components teaches me value lessons about .NET, from which I can even build desktop windows applications. It's a good graduation for me, and probably necessary for anyone looking for more serious development experience than some circle drawing codes written in GhPy

Recent Posts

See All

How to pass all ARE exams

Now that I’ve finished all my ARE exams and registered as an architect, I can summarize a few pointers for anyone still in pursuit. These are things that I found to be noticeably helpful along my way

ARE 5 and prep materials

Here is an update on my progress on ARE 5 exams and my experiences with the preparation materials on the market. Ever since I passed my three managerial exams (PjM, PcM, CE), I've taken a long hiatus

GhPy can multi-thread too! But wait...

GhPy component is known to be a bit slow in its performance because of the JIT compiler it utilizes. However, GhPy is a powerful widget to test out scripting logic without the baggage associated with

bottom of page