Processing Grasshopper communication

From TOI-Pedia


It this tutorial you will learn how to communicate between Grasshopper and Processing with the use of .txt files. Text files can be a simple medium of a universal method of storing arrays of data like coordinates, vectors or colors. This tutorial will start by explaining how to send a pointcloud from grasshopper to processing and then perform a simple operation with processing on this data. It's important that you installed the PeasyCam library for this tutorial. If you haven't used PeasyCam before, I suggest you first follow this tutorial: Processing 3D Navigation.

Start making the Processing Sketch

GH receive.jpg

First we need to set up the basic processing sketch for this tutorial. Give the sketch the name: GH_receive.pde. Go to the folder that is automatically generated by creating this file. You have to put a new folder inside this folder named: data. Here we will put the .txt files we will generate with the use of grasshopper. We leave the processing coding for later. First we continue with the grasshopper part.

Sending the pointcloud from Grasshopper

Next thing to do is starting Rhino and Grasshopper. We can very easily generate a 3D pointcloud with grasshopper with Vector » Grids » Populate_3D

GH pointcloud.jpg

If you use a panel after the 3D pointcloud you will see the grasshopper method of describing a point by it's x,y,z parameters. Something like:{2.486686, 1.10744, 0.934021}. We will get rid of these brackets and commas because processing can also distinquise the x y z coordinates by only a space division and it will distinquise all the individual coordinates by putting them on a new line each.

Decompose Pointcloud.jpg

We do this by first decomposing the points of the pointcloud in there individual x,y,z parameters: Vector » Point » Decompose

Now we will transform the grasshopper way of describing points from:

01 {2.486686, 1.10744, 0.934021}

02 {9.39928, 9.45462, 0.72356}

03 {1.174467, 9.279877, 0.51658}

04 {9.492078, 0.673603, 1.99918}


01 2.486686 1.10744 0.934021

02 9.39928 9.45462 0.72356

03 1.174467 9.279877 0.51658

04 9.492078 0.673603 1.99918


So we continue from the point we decomposed the points in there individual x,y,z parameters. We will use the component Concatenate strings to rewrite the individual point descriptions: Sets » Strings » Concatenate

Give the X output of the Decompose component as input A for the first Concatenate string component. Right click on B and then press set string. Press one time on your space bar and press enter. It will look if nothing happend but you will notice later that this space is appended to the X string.

Do the same thing for the Y output. For the Z output you do not need to append a space because it's the last in an array of the three coordinate parameters.

Concatenate multiple2.jpg

Your grasshopper definition should now look like the image at the right. Right click on the panel that is located the most at the right. Press then on the button stream contents. Save the content in the data folder of your GH_receive.pde sketch folder and give it the name: GH_PT.txt. The grasshopper part is finished for now. We will go back to processing to start writing some code and so something with this txt file.

Import the txt file in Processing

The first thing we need to write in this processing sketch is about importing the PeasyCam library:

import peasy.*;

Now we declare a PeasyCam object.

After the coordinates of the txt file are imported. We will put these points in a global PVector array. We will now define this Global PVector array and give the array a length of 100 for now:

PVector GHpoints[] = new PVector[100];

PeasyCam cam;

In the setup we first define the screen size, then initialize the PeasyCam object then set the minimum and maximum clipping distance of the camera.

void setup() {


cam = new PeasyCam(this, 100);





In the draw we define a import_GH() function and a function we name spiderWeb()

void draw(){

Make a white background:


Call the import_GH function:


Call the spiderWeb function:


End with a curly bracket.


We will now continue with the import_GH function. Start with:

void import_GH(){

First we load the txt data and then turn this loaded data in a local String array.

String GH_PT[] = loadStrings("GH_PT.txt");

We make a for loop for generating all the points from the String GH_PT[] array:

for (int x = GH_PT.length-1; x >= 0; x--){

We split the lines like 9.492078 0.673603 1.99918 in a little array with the split function and transform the string data in a local float array named point:

float point[] = float(split(GH_PT[x], ' '));

We define a temporal local PVector named Temp_PT

PVector Temp_PT = new PVector(point[0], point[1], point[2]);

We add the Temp_PT to the global array named PVector GHpoints[]

GHpoints[x] = Temp_PT;

End the function with two curly brackets.


The text file is now loaded. Important fact is that the length of the array is now set to 100. If you increase the amount of points in grasshopper higher then 100 you have to adjust the script.

Making a spiderWeb

Now it's time to start doing something with this imported data. We will use a simple loop function that will draw lines between points with a certain variable.

We start by writing:


Make a loop with the length same as the amount of points in

for (int i=0; i<GHpoints.length-1; i++){for (int b=0; b<GHpoints.length-1; b++){

if( jumpersB[i] != null){if( jumpersB[b] != null){

PVector locA = jumpersB[i]; PVector locB = jumpersB[b];

float distance = PVector.dist(locA, locB);

if ((distance > 0) && (distance < 3)) {

stroke(100, 110);


line(locA.x, locA.y, locA.z, locB.x, locB.y, locB.z);

End the function with six curly brackets.


Press play in processing.

The result should look like the image at the right.


Context cloud.jpg

A little assignment if you think you understand the logic of the tutorial. Try to import a context of points like the image at the right.

Personal tools