Using Python in Grasshopper

In the previous section we saw how Grasshopper, despite being a great platform for algorithmic design, can also be limiting when trying to incorporate more complex programming elements. On the other hand, programming directly with computer code in a programming language like Python gives us much more control over our models and allows us to develop more complex and interesting design spaces but has a much higher learning curve and can be quite intimidating when first starting out.

So how do we choose one over the other? Luckily we don’t have to! In fact we can easily extend the basic functionality of Grasshopper by embedding Python code directly into special Grasshopper nodes that can then interact with other nodes in our model. This allows us to rely mostly on Grasshopper for basic elements, and use code only when necessary for more complex functionality.

Why Python?

As discussed previously, there are now hundreds of different programming languages to choose from — so why would we choose to work with Python?

Python is a very modern, general-purpose and high-level object-oriented programming language. In recent years Python has become extremely popular in a variety of fields outside of computer programming such as science, medicine, statistics, math, and machine learning. This popularity can be attributed to Python’s

  • relatively simple syntax, with a focus on simplicity, clarity, and readability which makes it less complicated to learn and write
  • extensibility through a large collection of external libraries
  • a huge support community of active users.

Unlike more complex languages such as C++, Python is not meant for full software development. Because of it’s emphasis on ease of use, it also tends to be less efficient and somewhat slower than these languages, but this is usually not an issue for applications outside of software development.

Lately, Python is also starting to be embedded as a scripting language within many different design software, including ArcGIS, QGIS, Rhino, Solidworks, and Autodesk Fusion. This is a big change from earlier years when every design tool had it’s own proprietary scripting language (Rhinoscript for Rhino, EKL for Catia, Actionscript for Illustrator), so you had to learn a whole new language each time you used a new tool. Integrating Python as a standard scripting language allows designers to learn a single language and use it to control a variety of design software. So if you are not a programmer but want to learn one programming language that will give you the most use in your design career, there is a strong argument that the language should be Python.

Python in Rhino and Grasshopper

Starting in version 5 Rhino has Python directly embedded alongside (and as an eventual replacement for) Rhinoscript. In addition to running any standard Python code, it can also communicate with the Rhino interface and work with geometry by using a set of special Python libraries provided by Rhino. Using these libraries designers can use Python to control almost every feature of Rhino, from the model geometry, to its views, cameras, and even the user interface.

You can also work with Python directly in Grasshopper through an external plugin called GHPython. This plugin gives you a ‘Python’ node in Grasshopper which allows you to embed code into your models. Code written in these nodes uses the same libraries to handle geometry and perform various modeling tasks, and is able to communicate with both the Grasshopper and Rhino environments.

The great thing about the GHPython node is that it allows you to mix and match between working with code and normal Grasshopper nodes. This way, there is no pressure to develop your entire model just through code starting with a blank text file, which can be very intimidating for people just starting out with scripting or computational design. Instead, you can develop most of the model using standard Grasshopper nodes, and only use Python nodes to do more complex tasks that are difficult or impossible in standard Grasshopper. In a way GHPython is like a ‘gateway drug’ into using code for computational design. You can start off by writing small, simple scripts for specific purposes, and gradually develop more and more complex code as you learn.

Installing the GHPython library

Unfortunately the GHPython library does not come pre-installed with Grasshopper and must be download and installed separately. Luckily this is very easy to do:

1. Go to and download the latest stable version. At the time of this writing was the latest version and has been tested to work with all the examples in the class.

2. The library is contained in a single file called ghpython.gha. Once this file is downloaded, right click on it and go to properties. If there is a button or a checkbox in the properties window that says ‘Unblock’, check it or click on it to disable the blocking so that Grasshopper can see the file.

3. To load the library into Grasshopper you need to put this file into a special ‘Components’ folder where all the libraries are kept. The easiest way to find this folder is to launch Grasshopper and from the menu go to File -> Special Folders -> Components Folder. Now copy and paste the ghpython.gha file into this folder.

4. To see the GHPython node you need to restart Grasshopper either by restarting Rhino or typing the GrasshopperUnloadPlugin command in Rhino to shut down Grasshopper and then restart it. If you just close the Grasshopper window it only hides it and does not actually shut it down.

Using the GHPython node

The GHPython library ads just one additional node to Grasshopper. You can find it under the Maths tab.

Let’s put one of these nodes onto the canvas and see how it works. You can see that the Python node has input and output ports just like any other node in Grasshopper, except it can have any number of inputs and outputs and you can call them whatever you want.

You can add more inputs and outputs by zooming in on the node until you see minus icons appear next to the input/output names and plus icons appear between them. You can use these icons to either add or remove inputs and outputs. You can also rename them by right clicking on the name and entering a new name in the text box at the top of the menu.

These inputs and outputs are automatically brought into the Python script where you can use them as variables. This allows the Python script to interact with the rest of your Grasshopper model by bringing data in through input variables and then outputting other data that was created within the script.

Double clicking on the center of the node brings up the script editing window where you can write the actual Python code. Let’s create a simple ‘hello world’ example to see how this works.

GHPython ‘hello world’

Change the name of one of the Python node’s inputs to ‘input’ and the name of one of the outputs to ‘output’. Plug a text panel into the input and type whatever you want into the panel. Attach another text panel into the output so we can see the results.

Now type this line of code into the script window:

output = "hello " + input

and click the ‘Test’ button at the bottom of the window. This button will execute the script and you should see the results above. Clicking OK will save changes to your script and close the editor window. Clicking Close will close the window without saving changes. This simple example brings in text from the Grasshopper model through its input node, joins this text to another piece of text, and then assigns the result to the output node, which can then be used in the Grasshopper model.

Integrating code into Grasshopper like this is a very powerful way to extend the basic functionalities of Grasshopper and will allow us to create interesting and complex design spaces using the full tools of computation. The next series of posts will go through how to work with the five fundamental elements of programming in Python and introduce you to some of the basic concepts of the language. Although we won’t be connecting the nodes to anything yet, you can follow along with the code examples by entering them directly into the Python node script editor. Once we establish some of the basics, the following section of posts will go over how we can use Python to work with geometry and interact with other Grasshopper nodes.

For more information about learning the Python programming language you can follow these guides:

For more specific information on working with Python in Rhino and Grasshopper you can consult these resources:

Like what you read? Give Danil Nagy a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.