19th April 2024

A simpler way to work with relative items in Grasshopper

One of the most useful component patterns I have used in Grasshopper is finding relative items in data. This means that instead of using their indices or a pattern, it's about getting each item to interact with a neighbouring item.

Let's put this into practice, let's say I have a polyline.

Now, let's say you wanted to get the angles between the segments.

What you can do is create two lists, one without the first segment and the other without the last segment. Something that looks like this :

While that works, here is a solution that I think is more elegant.

In the second scenario, the shift list component gets rid of the first item of the list and the shortest list component gets rid of the last item on the list. Thus, creating the same effect as the first pattern.

I have been doing a lot of relative calculations lately and even though it's a simple change, I find the second pattern to be quicker and cleaner to use.

On that note, did you know that Grasshopper lets you avoid this scenario if you are just dealing with numbers?

Instead of measuring angles, let's say we now want the change in elevation between each point on the curve. You can either implement one of the patterns above or you can use the relative differences component.

This now greatly simplifies the process. Simplifying a 3-4 component process into a single component process.

Final Thoughts

Even though these patterns are not "game changers", they can make you quicker and help limit the mess in Grasshopper. They might be small but when implemented many times over in a single script, they make a huge difference to the readability of the script. I think it's always a good thing to find fewer components that can help you achieve the same thing.