Clusters and Hops

From TOI-Pedia

Introduction

LEVEL: MASTER
Expected Time: 60 min
Large scripts can be hard to follow

When your knowledge about building Grasshopper scripts is increasing, it is easy to lose the control over your script because of its size. Grasshopper gives users several methods to make it easier to get an overview of what you made. Furthermore, some of these techniques can be used to publish, share and optimize your scripts. Generally speaking, there are three options:

  • Group your components;
  • Cluster components;
  • Hops.

Since we already discussed grouping several times, we will skip this part for now.

Clusters

Your own component!

Clustering makes it possible to combine several Grasshopper nodes into one. This cluster can be published to your own Grasshopper ribbon for future use. There are several advantages and disadvantages about using clusters.

Advantages:

  • Clustering can make your script look cleaner;
  • It is easier to share your script with other people;
  • Future reuse of your script is easier.

Disadvantages:

  • Sometimes it is hard to track problems in your script when using (nested) clusters;
  • Clusters cannot be computed parallel;
  • Creating a cluster using many inputs and outputs can be a bit unintuitive.


How to make a cluster?

The script for an easy staircase

In this tutorial we will use the Tutorial 10 - Stairs Easy script as example. Let’s assume you created this script and you want to use it for all the future buildings you are going to design.

Place all inputs to the left, all outputs to the right

First we open the script and place all the relevant inputs like Number Sliders and Parameters to the left of your canvas. All the outputs that are important are placed on the right. You can do this easily by selecting all the input, holding shift, and click on the small left arrow of the displayed box.

  • Put all inputs to the left, all outputs to the right


Rename the inputs and outputs

Now we need to make sure that the names of all inputs and outputs are correct. Right-click on parameters to change their names. Also change the Number Slider names. It can be useful to add the type of input to the name. For example, rename a curve input as “Left Stair Curve” instead of “Left Stair”.

  • Rename all your Parameters and Number Sliders RMB on node » Type name


Relay the inputs

Sometimes one Number Slider or Parameter is used in multiple places in your script. Make sure that the inputs and outputs are only connected to one wire. This can be done easily using a relay. Make sure you also rename the relays with the same name as the input.

  • Relay inputs and outputs that are used by multiple nodes Params » Util » Relay


Sort the inputs and outputs

Next you should make sure that the order, from top to bottom, is correct for the final cluster you are creating. Perhaps it makes more sense to add the Curve inputs first, and less relevant Number Sliders last. Grasshopper always check the order according to the place where the data inputs. Therefore it can be helpful to also add a relay to single wired inputs.

  • Sort your inputs and outputs


Cluster the script

Finally select all nodes, except for the inputs and outputs. Right-click on the canvas and select Cluster. You will now see that all nodes are combine to just one.

  • Cluster all nodes except for inputs and outputs Select all nodes except for inputs/outputs » RMB on canvas » Cluster


Open the cluster script

If you double-click on the cluster, a new canvas will be opened with the script. As you can see, the inputs and outputs are replaced by input and output components. These nodes can also be found in the toolbar, so you can use them directly.

  • Enter the cluster Double click on cluster


Close the cluster script

To go back to the main script, Click on the box in the top right and Return to Parent. You can also change the script if you wish and save it before going back to the main script.

  • Go back to the main script LMB on box icon » Discard&Close


Rename the cluster

Rename your cluster to a name that describes its function LMB cluster » Type name

Publish the cluster or add it to the ribbon

Change the properties of the cluster

The properties of the cluster like the icon, name and nickname can be changed by right-clicking on the component and selecting Properties…

  • Change the cluster properties RMB on cluster » Properties...


Assign a password

If you want to share the script with others, but you don’t want them to see how you built it, right-click on the cluster component and click on Assign Password…

  • If you wish, assign a password RMB on cluster » Assign password

If you are working/studying at a university, we usually do not assign passwords to scripts. It is expected that other people will reference your name when they are using your work. You can license your work by adding a panel with the details of the license. More information about licenses can be found at: https://creativecommons.org/licenses/

Create a User Object

Now we will add the cluster to the Ribbon. Select the cluster you want to add. Now create a User Object in the File menu.

  • Add the cluster to the ribbon Select the cluster » Menu » File » Create User Object


Change the User Object properties

In the available properties that show up, you can specify what category you want to add the stair cluster to. You can also type a new name. Finally click on OK.

  • Specify the (sub)category


Find your User Object in the ribbon

You will now see that a new tab is added to your ribbon. Under the subcategory you specified, you will see that your created cluster is now available for future use.

If you wish, delete the User Component

Clusters in your ribbon can be removed by right-clicking on the component and clicking on Delete.

  • If you wish, delete the cluster from the ribbon RMB on cluster in the ribbon » Delete


Find the User Object Folder

You can Publish your script on github, Food4Rhino by exporting your own folders with cluster. The clusters you created can be found in the User Object Folder.

Hops

Create your own hopable definition

Hops can be best compared by external modules in programming. First you create a script, which works similarly to a cluster. After that you add a hop which references to your created definition. There are several advantages and disadvantages about using clusters.

Advantages:

  • Hops can be used with external Python script, also including modules like SciPy and NumPy (read more...)
  • Hops can be computed parallel and therefore can be efficient in complex definitions;
  • Hops can cache solutions to use again;
  • Hops can use multiple processor cores in calculations;
  • Hops can be executed using the GrasshopperPlayer function in Rhino;
  • Hops can use plugins from Grasshopper, like ladybug, anemone etc. as long as you have installed them.

Disadvantages:

  • Hops are new in Rhino 7 and may have some bugs (April, 2021);
  • Hops may take more time to calculate initially;
  • Hops have less available input types. Vectors for example, are not available. Currently, McNeel advices to convert these datatypes to strings first;
  • Nested hops are currently not possible;
  • You cannot directly see a script when double-clicking on a hop, which makes it less traceable.


How to make a hopable definition?

Use Context Parameters

Instead of using normal input Parameters and Number Sliders, you will have to use contextual parameters in your script. Contextual Parameters can be found in the Util tab under Params.

  • Replace all inputs and outputs with contextual parameters Params » Util


Settings of Context Parameters

By right-click on a Contextual Parameter, you can define an allowed range, like how you would do in a Number Slider. Rename the inputs and outputs according to their function.

  • Rename and define the range of contextual parameters RMB on contextual parameter


Output the data

Outputting the correct data works a bit differently. First select a node that belongs to the outputs and group it. Next, rename the group to RH_OUT:[output name]. You can output multiple objects by adding multiple groups.

  • Output the correct data Select output nodes » RMB on canvas » Group » RMB on group » Rename to RH_OUT:[output name]


Context bake your script

By adding the Contextual Bake component at the end of your script, the output of the script will be baked automatically when it is finished calculating.

  • Add a contextual bake component at the end of your script Params » Util » Contextual Bake


Save your hopable definition
  • Save your definition GH Menu » Save document as


Install Hops

Install hops from the Package Manager

First we need to install the Hop module. Open the Package Manager in Rhino and search for Hops. Install the latest version available. You will have to restart Grasshopper to use the plugin.

  • Open the Package Manager Rhino » Tools » Package Manager
  • Type and install Hops
  • Restart Rhino and Grasshopper


Use Hops in Grasshopper

A hop component

Open an empty canvas in Grasshopper and add the Hops component to the canvas.

  • Open an empty canvas
  • Add a Hops component Params » Util » Hops


Use you hop to calculate the Pythagoras formula

Double-click on the Hops component and find the correct definition file you made in the previous step.

  • Select the definition to hop Double-click hop » Find and select the saved hopable definition


Hop Parallel computing options

By right-clicking on the node you will see multiple options:

  • Parallel Computing: Defines if the Hop is allowed to use multiple cores of your processor. This can be either faster or slower depending on the type of script;
  • Path…: Defines which path to use as definition;
  • Local Computes: The amount of solutions that already have been calculated. When you are inputting a range of numbers into the definitions, it sometimes make sense to change it to 6 pack. This allows Grasshopper to use multiple cores for different inputs and therefore increases the speed of the calculation. Important to note though, is that it is still not possible to use trees as input to be calculated parallel. Make sure you only use single lists. Simply said, every item in the list uses is handled as a separate operation in your processor;
  • Cache in Memory: Saves results temporarily in your computer memory;
  • Cache on Server: Saves results permanent for future use.

As you may notice, the speed of the solutions can increase drastically when you enable caching.

Use Hops in Rhino

GrasshopperPlayer command in Rhino for hops

It is also possible to use your Hop definition using the Rhino Command Line. First use the command GrasshopperPlayer and select the Hop definition.

  • Execute Rhino GrasshopperPlayer command Rhino » Commandline » GrasshopperPlayer
  • Select the hopable definition


Enter the correct values

Rhino now asks for data for the inputs of your definition. Enter the correct values.

  • Enter the asked values
  • Click on Enter


Result of the Pythagoras calculation

If the result of the script is not visible anymore after executing the definition, you probably forgot to add a Context Bake to the output of your script. You can also add a Context Print to send a message when the execution is completed.

Personal tools
Actions
Navigation
Tools