top of page
  • Writer's pictureSilvia Vangelova

Representing and analyzing Grasshopper files as Graphs using NetworkX

How would you asses the performance of your grasshopper definitions? Sometimes its hard to inspect all components manually, especially in the case of complex models involving hundreds of components and operations.

Luckily, all grasshopper definitions are made up of one or multiple directed acyclic graphs.

Why? Because information in Grasshopper always flows from left to right and because Grasshopper does not allow for loops in the definitions.

What does this mean? We can order all definitions topologically, e.g. by the number of nodes each node is dependent on.

Why would we do that? By doing so we can see how much time it takes to reach a certain node.

Lets go step by step through the process of creating our graph.

1. Initial setup

We begin by importing the needed libraries. To install NetworkX in grasshopper go to your Scripts directory and run the following command there:

pip install networkx

If you don't know where your Scripts folder is, add a single python script component with this content:

Now, lets start building the entire script in a new python script component:

2. Construct graph

There are many types of objects in grasshopper files, each with different attributes. We need to go trough all of them in order to add them to our graph

Image source:

How do we get the process time of each object? By creating a get_time(object) function:

3. Analyze graph

We mentioned in the beginning that grasshopper files contain one or more graphs. Why is that? Sometimes grasshopper files consist of "isolated" clusters of components, that have no connections between them. When a graph has such "clusters" we say that its made of multiple connected components. In the case of directed graphs they are called weakly connected components. Here is how to find them using NetworkX:

Now, lets analyze how each graph is structured and store results.

5. Visualize results

Now that we have information about each nodes level, computation time, degree and name we can directly display them with Grasshopper.

What other ideas do you have about using Graphs to analyze Grasshopper files? I would be happy to discuss! :)

bottom of page