1st March 2024

GHEC 06 : Grasshopper Script Best Practices

Today's focus :

  • Why we need best practices
  • Using containers to provide context
  • Using different wire styles to separate operations
  • Using scribbles to explain operations
  • Using groups and colours to classify operations
  • Cleaning Methodologies

Why we need best practices

As you might have experienced, Grasshopper scripts can get very messy very fast. This mess makes it nearly impossible to read and modify anything. There needs to be some best practices around scripting to limit and tame the mess.

It can sound neurotic to set rules on how to place components but believe me, the chaos that comes from messily placing components will scale exponentially. Take a look at the script below

Pasted image 20240123103458.png

And imagine that someone (your boss) tells you to read and modify it, which would you rather work with? My answer would be the organised one.

Sure, it takes more upfront effort to organise a script but this effort more than pays for itself in the future.

When we make scripts, we need to keep in mind anyone else (including future you) that might read our scripts. It needs to be organised and clean so that it's clear and understandable.

So, today, we will learn about techniques to keep your script readable and organised. These are important habits to have especially if you work in a team or are planning to scale/distribute your Grasshopper files.

Use Containers

In the last session, you saw me use containers to provide more context to the geometry. I made and named the geometry using containers. This should be a standard when it comes to Grasshopper scripts.

Using and naming containers are like leaving notes. They clarify the data you are working with at any given moment. Instead having "curve" everywhere in the script, it's much easier to understand "bottom chord" or "diagonal web".

Pasted image 20240122164709.png

To name your containers, right-click on any container and type in the name. I like to leave the container type in the front.

Rhino_niXxETzaJQ.gif

Note: you have to use the "draw full names" option in "display" to see the names. There is a plugin that you can install to see both the names and the icons.

Use Wire Styles

Once we have our containers, it's time to do something about those wires. If you have seen any of my solutions before, I tend to use faint wires whenever I am moving on from an operation in the script.

Different wire styles help visually separate the types of operations happening in the script.

Let's look back at the ‚Äčtruss script‚Äč we did in the last session. The script can be broken down into three operations:

  • Components that make the top and bottom chord
  • Components that make the vertical webs
  • Components that make the diagonal webs

Then we can separate these operations on the script.

Then, on all the containers, you can right-click and change how the wire style looks.

Wire Style Faint.gif

Note: there is a "keyboard shortcut" that you can use, when you right-click, hit "w" then the starting letter of the wire style you want. I.e. To get a hidden wire style, right-click and hit the keys "w" then "h"

Use Scribbles to explain operations

With our script visually separated, we can add more context to the script with scribbles. We want to be as clear as possible to our operations on the script. I use scribbles to explain parts of the script that I think won't make sense to others.

But make sure you aren't overexplaining, if the components you used are logical and make sense without explaining, then don't use a scribble. A lot of times, it depends on who I think will use the script, if it's a Grasshopper beginner, I'll explain more, if not, I'll let the components speak for itself.

Note: Again, there is a shortcut for this. In the search bar, type ~ followed by your text to instantly place a scribble

I normally wouldn't put scribbles on a small script like this because the components and containers speak for itself.

Use Groups and colours to classify operation types

If your script is large and complex, it's worth using groups to visually show the operations on the script.

Groups.gif

This is especially useful if your script has different type of operations. These could be operations in charge of data processing, visualisation, inputs, etc. If you noticed, with all my solutions, I have a legend at the top of what each group colour means.

If you want to make your own, you can use what I have done and tweak the colours to your liking. I maintain these colours throughout my scripts for consistency and just so that people instantly know the types of operations on the script.

Cleaning Methodologies

As for how you should clean, I like to follow the "clean as you go" approach. As I create the scripts, I will also create containers and use different wire styles.

But I also know people who create scripts messily because they find cleaning disruptive of their thought process. They do make sure to clean the script afterwards.

Today's Homework

With these best practices, I believe it's all about getting the reps in. So your homework is to tidy up a messy script that I made using the principles that we have learnt today.

Session 06 - Script to be cleaned

Final Thoughts

Scripts are inherently messy. We must do our best to contain that chaos or else it gets out of hand. I like to think that Grasshopper scripts should follow similar principles to traditional text code.

Code should read like a Newspaper, you should see the headers, then as you dive deeper, the specifics are laid out in a flow that makes it easy to understand - Adapted from Robert C Martin (Author of Clean Code)

Because we are giving steps not the results, we have to clearly communicate our thought process to others (including future you).