Of course! "Python Grasshopper" refers to the powerful integration between Python and the visual programming environment Grasshopper for Rhino. This connection allows you to use Python's extensive libraries and scripting capabilities to control and extend Grasshopper in ways that are difficult or impossible with the standard components.

Here’s a comprehensive guide covering everything you need to know, from the basics to advanced applications.
What is Grasshopper?
First, a quick refresher:
- Grasshopper is a visual programming plugin for Rhino, a popular 3D modeling software.
- Instead of typing code, you create a "definition" by dragging and dropping components from a palette and connecting them with wires.
- It's widely used in architecture, computational design, and generative art for creating complex, parametric forms.
The traditional way to add custom logic to Grasshopper is with C# or VB.NET components. The Python integration offers a fantastic alternative.
Why Use Python in Grasshopper?
You might be wondering, "Why not just use C#?" Here are the key advantages of using Python:

- Easier to Learn: Python's syntax is clean, readable, and often considered more intuitive for beginners than C#.
- Massive Ecosystem: You get instant access to thousands of powerful Python libraries directly within Grasshopper.
- Data Science:
NumPyfor numerical operations,Pandasfor data analysis,Scikit-learnfor machine learning. - Visualization:
Matplotlib,Plotlyto create charts and graphs. - Image Processing:
OpenCVto analyze and manipulate images. - Math & Geometry:
SciPyfor advanced scientific computing. - Web & APIs:
Requeststo pull data from websites or online services.
- Data Science:
- Rapid Prototyping: The interactive nature of Python (especially with Jupyter-style notebooks) makes it fantastic for quickly testing ideas and algorithms.
- Cross-Platform: Python scripts are generally more portable across different operating systems.
How to Set Up Python in Grasshopper
Getting started is straightforward.
Step 1: Install Python
You need a standard Python installation. It's highly recommended to use a version manager like Miniconda or Anaconda, as it makes managing different Python environments very easy.
- Download and install Miniconda from https://docs.conda.io/en/latest/miniconda.html.
- Open the Anaconda Prompt (or Terminal on Mac/Linux) and create a dedicated environment for Grasshopper:
conda create -n grasshopper_env python=3.10 conda activate grasshopper_env
- Install essential libraries into this environment:
conda install numpy pandas scikit-learn matplotlib
Step 2: Tell Grasshopper Where to Find Python
Grasshopper needs to know the path to your Python executable.
- Open Grasshopper in Rhino.
- In the top menu, go to File > Special Folder > Components Folder. This will open the folder where Grasshopper looks for components.
- Inside this folder, create a new folder named
IronPython. (Grasshopper's Python engine is called IronPython, but it's standard practice to name it this way). - Inside the
IronPythonfolder, create another folder namedLib. This is where you will point Grasshopper to find your installed libraries. - Now, go to File > Preferences....
- In the "Python" section on the left, click on Interpreters.
- Click the "Add..." button.
- Browse to your Python installation and select the
python.exefile. For a standard Miniconda installation, this will be something like:C:\Users\YourUser\miniconda3\envs\grasshopper_env\python.exe - Select the interpreter you just added and click "Set as Default".
- Click "Apply" and then "OK".
That's it! You are now ready to write Python code in Grasshopper.
The "Python Script" Component
The main entry point for Python in Grasshopper is the Python Script component. You can find it in the Params > Script tab.
This component has two main parts:
- Input Parameters (x): These are the data that flows into your script from other Grasshopper components (e.g., a list of points, a number, a curve).
- Output Parameters (o): These are the variables you create in your Python script that you want to send out of the script to be used by other Grasshopper components.
Basic Example: Manipulating Points
Let's create a simple script that moves a list of points up by 10 units in the Z-direction.
- Place a
Pointcomponent on the canvas. Right-click it and set "List Length" to 5 to create 5 points. - Place a
Python Scriptcomponent. - Connect the
Pointcomponent to thexinput of thePython Scriptcomponent. - Double-click the
Python Scriptcomponent to open the code editor.
You will see some boilerplate code:
# Required import to allow Grasshopper to pass data to the script
import clr
# Add references to the necessary Rhino and Grasshopper assemblies
clr.AddReference('RhinoCommon')
clr.AddReference('Grasshopper')
# Import the Rhino and Grasshopper namespaces
import Rhino
import Rhino.Geometry as rg
import Grasshopper as gh
# The 'x' input is a list of points
points = x
# The 'o' output is a list of new points
new_points = []
# --- YOUR PYTHON CODE GOES HERE ---
# Loop through each input point
for pt in points:
# Create a translation vector (0, 0, 10)
translation = rg.Vector3d(0, 0, 10)
# Create a new point by adding the translation vector to the original point
new_pt = pt + translation
# Add the new point to our output list
new_points.append(new_pt)
# Set the output of the script
# The name 'o' must match the output parameter name in the component
o = new_points
- Click "OK". You will see an
ooutput appear on the component. - Connect the
ooutput to aPointcomponent. You will see the original points and the new, translated points.
Working with Data Types (The Most Important Part)
This is the most crucial concept to understand. Data in Grasshopper is "tree-like" (a list of lists of lists...), but Python works with simple lists. The ghpythonlib library helps bridge this gap.
The ghpythonlib module is automatically available in the Python Script component and provides helper functions.
Helper Functions for Inputs:
-
ghpythonlib.tree_flatten(x, None): This is the most useful one. It takes a complex Grasshopper data tree and "flattens" it into a simple Python list. If you have multiple branches, it just combines them all.# Assuming x is a data tree with multiple branches flat_list = ghpythonlib.tree_flatten(x)
-
ghpythonlib.tree_to_list(x): This converts a data tree into a list of lists, where each inner list represents a branch of the tree. This is useful when you need to process data branch by branch.# This will give you [[branch1_item1, branch1_item2], [branch2_item1, ...]] list_of_branches = ghpythonlib.tree_to_list(x)
Helper Functions for Outputs:
ghpythonlib.tree_from_list(list, tree): This is the reverse. It takes a Python list and converts it back into a Grasshopper data tree, using the structure of an existing tree (tree) as a template. This is essential for maintaining data matching in Grasshopper.
Example: Dealing with Data Trees
Imagine you have two lists of points, one for the base of a column and one for the top. You want to create a line between each corresponding pair.
-
Use two
Pointcomponents. Set one to have 3 items and the other to have 3 items. -
Connect both to a
Python Scriptcomponent'sxinput. You'll seex(plural), meaning it's receiving multiple inputs. -
In the script:
import clr clr.AddReference('RhinoCommon') import Rhino.Geometry as rg import Grasshopper as gh import ghpythonlib as ghlib # Import the helper library # x is now a tuple of inputs: (input_list_1, input_list_2) base_points, top_points = x # We need to make sure they are both simple lists base_pts = ghlib.tree_flatten(base_points) top_pts = ghlib.tree_flatten(top_points) lines = [] for i in range(len(base_pts)): # Check if the points are valid before creating a line if base_pts[i] and top_pts[i]: line = rg.Line(base_pts[i], top_pts[i]) lines.append(line) # Output the list of lines o = lines -
Connect the
ooutput to aLinecomponent. It will correctly create 3 lines.
Advanced Example: Using NumPy to Find the Centroid of a Point Cloud
Let's use the NumPy library to calculate the centroid (average point) of a set of points.
-
Place a
Pointcomponent on the canvas and give it many points (e.g., 100). -
Place a
Python Scriptcomponent. -
Connect the points to the
xinput. -
In the script editor:
import clr clr.AddReference('RhinoCommon') import Rhino.Geometry as rg import Grasshopper as gh import ghpythonlib as ghlib import numpy as np # Import NumPy # Get the points and flatten them into a list points = ghlib.tree_flatten(x) # Check if we have any points if not points: o = None # Output nothing if the list is empty else: # Create a NumPy array from the points # We need to extract the X, Y, Z coordinates point_array = np.array([[pt.X, pt.Y, pt.Z] for pt in points]) # Calculate the mean (centroid) along the rows (axis=0) centroid_coords = np.mean(point_array, axis=0) # Create a Rhino Point3d from the calculated coordinates centroid_point = rg.Point3d(centroid_coords[0], centroid_coords[1], centroid_coords[2]) # Output the single centroid point o = centroid_point -
Connect the
ooutput to aPointcomponent. You will see a single point at the exact center of all your input points.
Debugging Tips
- Print Statements: The easiest way to debug is to use
print(). The output will appear in the Rhino command line or in the Grasshopper Console (View > Console).print("My input points are:", points) - The
ghenvobject: This object stores information about the component's environment.ghenv.Component.Message: Displays a message on the component icon in the Grasshopper canvas. This is great for giving feedback to the user.ghenv.Component.Message = "Calculated centroid successfully!"
- Component Error: If your script has an error, Grasshopper will show an error message on the component. Double-clicking it will often give you a traceback of the Python error, which is very helpful for finding the problem.
Summary
| Feature | Description |
|---|---|
| Core Component | Python Script from Params > Script. |
| Input Handling | Use ghpythonlib.tree_flatten(x) to simplify complex data trees into Python lists. |
| Output Handling | Use ghpythonlib.tree_from_list() to convert Python lists back into Grasshopper data trees. |
| Libraries | Access any Python library installed in your designated environment (NumPy, Pandas, etc.). |
| Debugging | Use print() for console output and ghenv.Component.Message for user feedback. |
The Python-Grasshopper connection opens up a world of possibilities, allowing you to leverage the full power of the Python ecosystem to create incredibly sophisticated and data-driven generative designs. Happy coding
