Random Points On Surface

From TOI-Pedia


Expected Time: 20 min
Possible result of this tutorial

A nice possible option in Grasshopper is to use a random generator to add randomness to your design. In this tutorial we will create a grid on a Rhino surface and then randomly pick a predefined number of grid points. I you have any trouble understanding this tutorial, it is advised to take a look back at Basic Principles Explained.

The Design

The Rhino surface

Before we start building the grasshopper model we need to create our Rhino Surface. This can be any kind of surface. In this tutorial we use a flat surface with two holes, created with the the function planar.

  • Create a surface in Rhino, for example with Rhino Commandline » PlanarSrf

Creating the basic grid

Set your surface on the parameter

The first step is to link the Rhino surface within our Grasshopper model. Right click on the surface parameter and set one surface. Next, click on the surface you just created.

  • Create a surface parameter Params » Geometry » Surface
  • Link the Rhino surface to the surface parameter RMB on Surface parameter » Set one surface

Create a basic grid with the divide surface module

To create the grid on the surface we will divide the surface.

  • Add a divide surface component to the script Surface » Util » Divide Surface

We want to be able to change the number of divisions in order to change the number of grid points on the surface.

  • Connect two number sliders to the U and V input of the divide surface module Params » Input » Number Slider

Cleaning up the list of points

Check the output of the points with a panel. Some values seem to be "Null".

When we create a panel and connect the output of our Divide Surface to it, we can see the values within the list of grid points.

  • Connect a panel to the Points output of the divide surface component Params » Special » Panel

What we notice immediately, are the list items with the value "NULL". These are points that could not be created because they are outside the surface. This is due to the way planar surfaces are created. A planar surface is basically a rectangular surface that has parts cut off to create the holes and the curved edges. When we divide the surface Grasshopper still tries to create points on the cut off pieces of the planar surface, but it will off course not succeed.

Remove the Null values with a clean tree node

We want grasshopper to pick a number of grid points randomly, so we don't want grasshopper to try and pick one of the grid points that could not be created. Therefore we get rid of the "NULL"-values.

  • Connect a Clean Tree component to the Points output of the divide surface component. Make sure you use the "tree" input of the Clean tree function. Sets » Tree » Clean Tree

Flatten the list

When we look at the list we can still see that the values are grouped in smaller separate lists. To be able to pick the points more easily we will turn these smaller lists into one big list. Note the difference between the original list with smaller groups and the new flattened list.

  • Flatten the created list of points Sets » Tree » Flatten Tree

Introducing the random generator

Add a Random component and a construct domain component to the canvas

To be able to use a random generator, first let's add a random component. The random component asks for a domain with a minimum value and a maximum value from which the function can choose. To find out between which values the random generator can choose, we need to retrieve the length of the list.

  • Add a random function to the canvas Set » Sequence » Random
  • Connect a construct domain to the domain input of the Random function Math » Domain » Construct Domain

Connect a panel with value 0 typed in it to the start of the domain

As computers start to count with zero, the start of the domain will be zero. In this case it might be the easiest to create a panel and type the value within the panel.

  • Add a panel with value 0 to the Domain Start Params » Special » Panel

Connect a list length to the script

The end of the domain will be equal to the amount of points in the list. However, there is one small issue. Computers start to count with index zero. That means the number of values is always 1 higher than the highest index in a list. Therefore we need to substract 1 from the list length to be able to get the correct domain.

  • Connect a list length to the output of the Clean Tree. After that connect the output of the length to the Domain end.Sets » List » List Length

Add an expression to the domain end input

Now add an expression to the input of the Domain End. This will probably be the first time you add an internal expression to your grasshopper script, so follow the steps carefully:

  • Right click on the Domain End input. Select expression. RMB on Domain End » Expression
  • Type x - 1 and click on OK. RMB on Domain End » select “Expression” and Type x-1 » OK

Connect two number sliders to the random function

The random generator still has two other inputs. The number of random values created (N) and the random seed (S). We will create number sliders for both inputs.

  • Add a numberslider to the N and S input Params » Input » Number Slider

The random seed might need a little more explanation. The random generator is a pseudo random generator. This means that for every seed you choose it creates random values, but it will always create the same random values for the same seed. If you do not like a random distribution, just try a different seed.

Set the output of the random number generator to integers

Finally, we have to set the output of the Random function to integer (round) values.

  • Right click on the Random node and select Integer Numbers. RMB on Random node » Integer Numbers

Now our random number generator is completed. The input is a number of values and a seed. The output is a selected amount of round numbers.

Retrieve specific points from the flattened list

Now we can select random points of our flattened point list, by adding a list item.

  • Connect the list input of a list item node to the flattened list. Set » List » List item
  • Connect the Random output to the list item index

The final script of this tutorial

We now have our final script! If you add some geometry to the points, a table or chair for example, you get a surface with randomly placed objects. Don't forget to set preview off for unnecessary nodes.

  • Add a final point parameter for good practice Params » Geometry » Point
  • Set preview off of unnecessary nodes Select items before final parameter » RMB on canvas » Preview off

Alternative Approach

Grasshopper has several alternative methods to populate a geometry or domain with random points. For example: Vector » Grid » Populate 2D, useful for populating rectangular shapes, Vector » Grid » Populate 3D, especially useful for populating boxes, and finally Vector » Grid » Populate Geometry, for populating 3D geometry.

For the assignment in this tutorial, it would have made way more sense to use the Populate Geometry node. However, considering that it is important to learn how the Random function works, we used the previous method. In the model you are building you may prefer to use the Populate Geometry function.

Alternative approach for populating geometry with points. In the case of this tutorial, the "populate geometry" would have been way more useful than the method described before.

Personal tools