Processing to grasshopper with oscP5

202 Views Asked by At

I’m trying to see if I can link a sketch made in processing to grasshopper using oscP5. The sketch is a sphere of points outlined in the first half of this Coding Train video:

https://www.youtube.com/watch?v=RkuBWEkBrZA

The code before I’ve started to link it with oscP5 appears to be working fine:

import peasy.*;

PeasyCam cam;

PVector[][] globe;
int total = 20;

void setup() {
  size(600, 600, P3D);
  cam = new PeasyCam(this, 500);

  globe = new PVector[total+1][total+1];
}

void draw() {
  background(0);
  fill(255);
  lights();

  float r = 200;

  for (int i = 0; i < total+1; i++) {
    float lat = map(i, 0, total, 0, PI);
    for (int j = 0; j < total+1; j++) {
      float lon = map(j, 0, total, 0, TWO_PI);
      float x = r * sin(lat) * cos(lon);
      float y = r * sin(lat) * sin(lon);
      float z = r * cos(lat);   
      globe[i][j] = new PVector(x, y, z);        
    }
  }
  noFill();   
  for (int i = 0; i < total; i++) {
    beginShape(TRIANGLE_STRIP); 
    for (int j = 0; j < total+1; j++) {
      PVector v1 = globe[i][j];
      stroke(255);
      strokeWeight(2);
      vertex(v1.x, v1.y, v1.z);   

      PVector v2 = globe[i+1][j];
      vertex(v2.x, v2.y, v2.z); 
    }
    endShape();
  }
}

However when I try to implement oscP5 its not quite working. My grasshopper file is recieving the skech fine however the points aren’t correct so I must have a problem with what data I’m sending with but I cant seem to figure out what I should be sending instead. The code takes the values of a stated radius, longitude and latitude and converts it into an x, y, z co-ordinate and I was trying to get oscP5 to send those x, y, z coordinates.

Does anyone have any ideas? The code so far is below.

//import necessary libraries
import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

//import camera
import peasy.*;

PeasyCam cam;

// message to send
String message;

PVector[][] sphere;
int total = 20;

float lat;
float lon;

void setup() {
  size(600, 600, P3D);
  cam = new PeasyCam(this, 500);

  sphere = new PVector[total+1][total+1];

//send message from this port
  oscP5 = new OscP5(this,12000);      
//send message to this port
  myRemoteLocation = new NetAddress("127.0.0.1",12001);
}

void draw() {
  background(0);
  fill(255);
  lights();

  float r = 200;

  for (int i = 0; i < total+1; i++) {
    float lat = map(i, 0, total, 0, PI);
    for (int j = 0; j < total+1; j++) {
      float lon = map(j, 0, total, 0, TWO_PI);
      float x = r * sin(lat) * cos(lon);
      float y = r * sin(lat) * sin(lon);
      float z = r * cos(lat);   
      sphere[i][j] = new PVector(x, y, z);        
    }
  }
  noFill();    
  for (int i = 0; i < total; i++) {
    beginShape(TRIANGLE_STRIP); 
    for (int j = 0; j < total+1; j++) {
      PVector v1 = sphere[i][j];
      stroke(255);
      strokeWeight(2);
      vertex(v1.x, v1.y, v1.z);   

      PVector v2 = sphere[i+1][j];
      vertex(v2.x, v2.y, v2.z); 
    }
    endShape();
  }

// osc message
  OscMessage myMessage = new OscMessage("/hello world");


for (int j = 0; j < total+1; j++) {
//message to send 
message  = "x = " + String.valueOf(r * sin(lat) * cos(lon)) + "; " + 
           "y = " + String.valueOf(r * sin(lat) * sin(lon))+ "; " +
           "z = " + String.valueOf(r * cos(lat))+ "; " ;

    myMessage.add(String.valueOf(r * sin(lat) * cos(lon)));
    myMessage.add(String.valueOf(r * sin(lat) * sin(lon))); 
    myMessage.add(String.valueOf(r * cos(lat))); 
  }

//print message
  println(message);
//send message
  oscP5.send(myMessage, myRemoteLocation);
}

I believe the error is in this section of the code

for (int j = 0; j < total+1; j++) {
//message to send 
message  = "x = " + String.valueOf(r * sin(lat) * cos(lon)) + "; " + 
           "y = " + String.valueOf(r * sin(lat) * sin(lon))+ "; " +
           "z = " + String.valueOf(r * cos(lat))+ "; " ;

    myMessage.add(String.valueOf(r * sin(lat) * cos(lon)));
    myMessage.add(String.valueOf(r * sin(lat) * sin(lon))); 
    myMessage.add(String.valueOf(r * cos(lat))); 
  }

Any help would be amazing.

1

There are 1 best solutions below

0
On

The values of the global variables float lat; are never float lon; set, because in the nested for loops, 2 new local variables (in the loop block scopes) are declared:

for (int i = 0; i < total+1; i++) {
float lat = map(i, 0, total, 0, PI);
    for (int j = 0; j < total+1; j++) {
        float lon = map(j, 0, total, 0, TWO_PI);

        // [...]
    }
}

Note, float lat = ... declares a new variable, lat = ... would be an assignment to the existing variable lat.

But using the global variables instead of setting new ones, won't change anything, because when the message is sent, then the variables lat and lon would have the value of the last iteration of the nested loops.

You have to implement 2 new loops and you have to recalculate the values for lat and lon. The print statement at after the loop makes not any sense, because it will print the last coordinates only.

The code should look somehow like this:

// osc message
OscMessage myMessage = new OscMessage("/hello world");

for (int i = 0; i < total+1; i++) {
    float lat = map(i, 0, total, 0, PI);
    for (int j = 0; j < total+1; j++) {
        float lon = map(j, 0, total, 0, TWO_PI);

        float x = r * sin(lat) * cos(lon);
        float y = r * sin(lat) * sin(lon);
        float z = r * cos(lat);
        println("(", i, ",", j, ") : x =" , x, "y = ", y, "z = ", z);

        myMessage.add(String.valueOf(x));
        myMessage.add(String.valueOf(y)); 
        myMessage.add(String.valueOf(z)); 
    }
}

//send message
oscP5.send(myMessage, myRemoteLocation);