7th March 2024

Using Metahopper to auto-colour groups

Hi there,

This week and maybe the next, I am refactoring an article that I wrote on strategies to organize your grasshopper script. The idea behind the article was to share tactics that you can use to keep your grasshopper tidy and organised.

After, pondering it more and getting feedback from others, I have decided to turn it into something more formal. Something along the lines of "Best Practices for Grasshopper scripting". I think these strategies should become the scripting standard.

In that article, I talked about colouring groups to classify different operations. Operations like inputs, geometry processing, visualisation, etc. Each operation should have a different coloured group on the canvas. These colours help readers immediately identify the operations that a script is performing.

I have a colour legend to keep these operation types consistent if you have seen any of the scripts I use.

Pasted image 20240305095827.png

Even with a colour legend, manually maintaining and applying the colours takes time and is impractical when creating scripts. To help with that, I use the metahopper plugin to colour the groups based on their names.

This helps me "automatically" colour the groups on my canvas and I don't have to worry about setting the right colour each time.

Let me show you how to make this workflow.

Metahopper auto colouring groups workflow

Metahopper has a component that gets all the groups and it's properties on the canvas.

Pasted image 20240307091955.png

Using this component, I do a text match of the group names against a list of expected group names.

Pasted image 20240307092104.png

The idea is that I am grouping all the groups based on a naming convention. I am sorting each group type into a branch. I.e., groups named "IN_" will go into branch {0}. From here, I want to clean my tree just to make sure I am working with clean data.

Pasted image 20240307092521.png

Now with this clean tree, I'll create a list of colours for each group type.

Pasted image 20240307092642.png

You may have noticed that I have more colours than I do group types, but that doesn't matter because I am using the branch number to apply the colours. That's why it was important to clean the tree beforehand. I.e. "IN_" group types will be coloured cyan.

Now, I can use another metahopper component, which is the set groups component, to set the colours of the groups on the canvas.

This is what the entire workflow looks like:

Pasted image 20240307092823.png

Now, when I add or modify any groups on the canvas, I just have to click on the "update" button and the workflow will colour my groups accordingly.

I always place this workflow at the top of my scripts but I'll remove them before sending the script to anyone else. This is so that I don't force anyone to get the metahopper plugin. This "auto" colouring can also be done with a C# or python script but this way allows anyone to use this workflow without knowing how to code.

You can find today's script ​here​.

Until the next one,

Braden Koh

P.S. If you are new to Grasshopper, check out my ​free 12-day email Grasshopper course​.