Start in GhPython -tips

It’s probably C# not C++. The Python version that Rhino implements is called IronPython i.e. a Python flavour for .NET that is written entirely in C#. You can therefore think of IronPython as a high level and dynamic wrapper around C# with Python syntax. This comes at the cost of performance, where especially large computations will be slower than directly written in C#. For more direct considerations about language differences, have a look at this article from old forum.

Generally speaking I use GHPython for most of my daily coding (generating/analysing geometry and solving problems fast, iteratively, and on the fly) and C# whenever I need more performant code. I personally find that GHPython provides less friction when scripting directly on the canvas than the C# component/language. Conversely, writing compiled C# code in Visual Studio when developing “real” Grasshopper plugins is very low friction. So if your goal is to write a plugin I’d recommend starting with C#. Although you can “compile” your GHPython scripts it doesn’t offer performance benefits (that I know of at least) and is really rather cumbersome.

I’m not sure what you mean by “basic component scripts”. But if you wanted to say inspect the code of the rhinoscriptsyntax Python module to see how it implements the RhinoCommon API, you can use the methods described in this thread. The source code of Grasshopper itself isn’t open, so you’d have to decompile it to inspect it. Which would be a breach of the license.

Absolutely great! The only caveat I can think of is that IronPython doesn’t support all Python modules (e.g. numpy). But being able to use all of .NET tends to make up for that (e.g. MathDotNet).

My primary GHPython tip is to start using RhinoCommon directly as soon as possible:

Understanding the different geometry types, how they relate to each, their properties, and methods is the most important aspect of learning how to code Rhino geometry (in my experience). And putting unnecessary and confusing layers of indirection around that is a detriment to learning (again, in my experience). That’s not to say that rhinoscriptsyntax or node-in-code doesn’t have it’s place.

Finally, learning how to Google coding questions is probably the most important skill you’ll need :wink:

Edit: To answer this question directly:

Yes, you should be able to implement most .NET APIs directly in GHPython. See for instance this thread, which documents how I implemented the Google Sheets .NET API. That said, you will likely have an easier time implementing .NET APIs in C#. As the documentation and examples likely will be written in this. You can also implement C++ directly in GHPython, like I did here implementing the ShapeOp library. And of course you can implement Python libraries, at least the ones written in pure Python. Python also has many standard libraries for interfacing with databases, internet protocols etc. that I find super useful in my daily work.

9 Likes