Introduction to Python in Grasshopper

Hi there,

I am a young architect (traditional “building” architect, not software architect) and I have been using Rhino and Grasshopper for about 8 years now. I honestly love this software and I would love to expand my knowledge. I also began to study Python recently and I would like to start implementing some code into my workflow.

So, I am starting this topic with the idea of receiving some advice and some clarification about stuff things that doesn’t make sense to me right now. I would appreciate it if someone can explain the questions I have in the simplest possible words, since I am new to this. Also, I wanted to say that I did some reaserch already and I read through this forum and through the RhinoDev website, and still have many doubts.

My idea would be to use Python within Grasshopper to create custom components (and eventually, in the future, plugins). I will list my main questions below:

1 - What is RhinoCommon? Is it a library containing many methods and classes written in C# that are useful when developing in Rhino?

2 - What is RhinoScriptSynthax? Is it the “way” one can access the RhinoCommon library from inside a GHPython component? Or is it something completely different?

3 - What is the difference between using “import Rhino”, “import RhinoScriptSynthax” or “import ScriptContext” inside an GHPython component? Because so far I understand you can do many similar things with all of these 3, for example, creating a line.

4 - How does IronPython work inside Rhino and Grasshopper? Is it right to say that basically, IronPython “receives” orders written in Python synthax and “translates” them internally to C# in order to use the RhinoCommon library?

Once again, I know that I can search on the internet for most of these answers and find a result, but so far I couldn’t really understand them and I am posting them here in order to receive an explanation with simpler and understandable terms.

Thanks in advance,

.

1 Like

Exactly, it’s a heavly OOP-based scripting API written in C# (.Net) that can be used in RhinoPython and GHPython and that rhinoscriptsyntax is syntactic sugar on top of.

It’s a Python module or library that wraps RhinoCommon API functionality to make it easier digestible for beginners, at least in theory. It’s meant to be used in Rhino for scripting, not in GHPython, which often causes confusion. In contrast to the API, It’s rather based on functional programming, which is usually more straight forward to understand as an organisational strategy in coding, than object-oriented programming.

import Rhino imports the majority of the RhinoCommon API, whereas import rhinoscriptsyntax loads the same named Pythonic module, which again is syntactic sugar on top of the API. In Grasshopper, you should really use API. It’s pretty well documented, too. Both are.
scriptcontext is an additional, smaller Python module that wraps further, more specific API functionality and adds some features, like the sticky dictionary. I believe it’s primarily meant to be used in Grasshopper.

IronPython is a Python version derived from C#, whereas usually Python is programmed in C(++). Python usually works with an interpreter that, well, interprets the code. I don’t think that there’s much translation going on here, in contrast to compiled code which usually gets translated back to machine code or assembly.
In terms of the API, there’s probably a translation layer between Python and the C# API.

1 Like

Hi diff,

Thank you for your answer. I came up with a few questions while reading them:

1- Why are you saying RhinoCommon is an API? As far as I know an API is a different thing, maybe I am mistaken. Would you mind explaining what an API is for you?

2- What do you mean by “IronPython is a Python version derived from C#, whereas usually Python is programmed in C(++)”? Isn’t Python a languaje on its own? What do C++ or C# have to do here? And why does Rhino/Grasshopper use IronPython instead of “real” Python?

I am sorry for my basic knowledge on these topics, but I would really appreciate some advice and I really want to know more about these things.

Thank you very much again.

P.S.: Would you mind taking a look at this? I couldn’t come up with a solution and maybe you can help me:

It’s officially called the “RhinoCommon API” (cf. docs). You might be mistaken in thinking that it’s a different thing. An API is an application programming interface which you can use here through Python, C#, VB, etc. to steer Rhino.

Python is a high-level, interpreted language of it’s own, but the standard implementation referred to as CPython is written in C; to be precise C11 for Python 3.11. IronPython is another implementation based on the .NET Common Language Runtime (C#), and currently stuck at version 2.7. All facts no printer!

C as well as C++ are low-level, compiled languages, which are generally much faster than for instance Python (or even C#) but there’s also less syntactical or functional sugar, which means you have to type more, memorize way more syntax, and for instance familiarize yourself with low-level language features and necessities, like managing memory and so fourth.

Parts of Rhino are written in C#, so I guess the implementation of IronPython was probably the easiest choice. Rhino 8 is said to get CPython support though.

I have, but I don’t know if this is even possible. I’m more familiar with “creative coding”. UI stuff is not my jam.

2 Likes

The .NET CLR supports a variety of languages: C#, Visual Basic, F#, etc. The supported languages all generate, instead of compiled machine code like C, C++, Fortran, etc., a standard “intermediate code” which is then fed as input data to the .NET executable to run. The .NET ecosystem also includes an extensive library of common routines used by programmers writing in the supported languages.

C# seems to be the second favorite for Rhino plugin developers with Python being the first. Visual Basic is also used by developers moving from Rhinoscript because Rhinoscript is based on Microsoft’s VisualBasicScript (VBScript).