7.2 Realtime Rhino to Processing: Value List

The above definition sends a message of three dimensional coordinates to the sketch below. The list of coordinates has an undetermined length. The sketch uses the data type tag to get the length of the list. The data type tag is a string with a letter for each value type. s-stirng, f-float, i-int, etc. In this case all of the coordinates are sent as strings so the list will consist of “s”s or something like “ssssssss….” To get the length of the list you can get the length of this string as each s represents a coordinate. You can download the above definition here: Rhino_to_Processing_OSC_2.zip

import oscP5.*;
import netP5.*;

//declare and initialize a list of vectors called pts
ArrayList <PVector> pts = new ArrayList<PVector>();

OscP5 oscP5;

///camera varibles
int oldx = mouseX;
int oldy = mouseY;
float rotx = 0;
float roty = 0;
float zcam = 500;

void setup() {
  size(800, 800,P3D);
  
  //becuase we are reciving a large amount of data we have to set the OSC buffer size
  //as a default it is set to 1536 bytes. The maxiumum size is 64000 bytes
  //to set this we need to create an osc properties object
  //we will set the port address here and the buffer size to 12000
  OscProperties properties = new OscProperties();
  properties.setListeningPort(23000);
  properties.setDatagramSize(12000);
  
  //start oscP5, listening using th properties object
  oscP5 = new OscP5(this,properties); 
}

void draw(){
  background(0);
  cam();
  
  //draw points sent from RhinoGH
  for(int i = 0; i < pts.size(); i++){
    PVector p = pts.get(i);
    stroke(255);
    strokeWeight(3);
    point(p.x,p.y,p.z);
  }
}

//incoming osc message are forwarded to the oscEvent method.
//this function i s only triggered when a message is recieved
void oscEvent(OscMessage theOscMessage) {
  /* print the address pattern and the typetag of the received OscMessage */
  print("### received an osc message.");
  
  //the value type lists the value of each piece of data as a letter
  //in this case the type will be a string made of "sssssssss" as each type
  //of data is sent as a string. The number of s's is the length of the list
  String valuetype = theOscMessage.typetag();
  //get the length of the list by finding the length of the type string
  int listLength = valuetype.length();
  
  //reinintialize the point list
  pts = new ArrayList();
  //get the coordinates from the message and ad them to the point list
  for(int i = 0; i < listLength/3; i++){ 
    float xp = float(theOscMessage.get(i*3).stringValue()); 
    float yp = float(theOscMessage.get(i*3+1).stringValue())*-1; 
    float zp = float(theOscMessage.get(i*3+2).stringValue()); 
    pts.add(new PVector(xp,yp,zp)); 
  }
} 

///camera functions 
void cam() { 
  int newx = mouseX; 
  int newy = mouseY; 
  translate(width/2, 
  height/2,zcam); 
  rotateY(rotx); 
  rotateX(roty);
  if ((mousePressed == true) && mouseY > 180) {
    rotx = rotx + (oldx-newx)/50.0;
    roty = roty + (oldy-newy)/50.0;
  }
  oldx = newx;
  oldy = newy;
}

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  zcam = zcam - e*5;
}