1.1 Processing to Rhino with OSC

This basic example sends the X & Y coordinates of the mouse to Rhino and uses them as the center point of a circle.

You will need the Ghowl grasshopper plugin for the above definition: gHowl_r50

Here is the Grasshopper definition that receives OSC messages from processing: osc_point

import netP5.*;
import oscP5.*;

OscP5 oscP5;
OscMessage myMessage;
NetAddress myBroadcastLocation; 

Here are the two libraries you need. You also need to initialize the OscP% object and that variable that will store the port you will send your signal from: “myBroadcastLocation.” You also need to declare a variable that will hold the message you are sending: “myMessgae.”

void setup(){
  size(800, 800);
  
  oscP5 = new OscP5(this,7880);
  myBroadcastLocation = new NetAddress("localhost",1200);
}

In the setup function, you will need to create a new OscP5 object and store it in the variable “oscP% you initialized. The port number is the one your processing sketch will be listening too. IT is not necessary for this sketch because we are not receiving anything, but you have to add it. Note: this port needs ot be different than the port you are sending from. The port you send from is stored in “myBroadcastLocation.” This includes your IP address which is your address on a network. In this case, you are sending it to your computer so you can use “localhost” or 127.0.0.1 both of which mean communicate with your local device. You could send data to someone else on a shared network if you have their IP address.

void draw(){
  background(0);
   
  int px = mouseX;
  int py = mouseY;
  
  fill(0,255,255);
  ellipse(px,py,20,20);

In the draw function, we are getting the x & y position of the mouse and storing them in variables. We then use these coordinates to draw an ellipse.

  myMessage = new OscMessage("/point");
  myMessage.add(px);
  myMessage.add(py);
  oscP5.send(myMessage,myBroadcastLocation);

}

Here is where we send the message. We reinitialize the message in the first line. That will clear it from the previous run of the draw function. If we don’t do this the values we add will be cumulative. The two values of the x & Y coordinates are added using “myMessage.add(value).” Then we send the message.
Full code for sketch below:

import netP5.*;
import oscP5.*;

OscP5 oscP5;
OscMessage myMessage;
NetAddress myBroadcastLocation; 

void setup(){
  size(800, 800);
  
  oscP5 = new OscP5(this,7880);
   myBroadcastLocation = new NetAddress("localhost",1200);
}

void draw(){
  background(0);
   
  int px = mouseX;
  int py = mouseY;
  
  fill(0,255,255);
  ellipse(px,py,20,20);

  myMessage = new OscMessage("/point");
  myMessage.add(px);
  myMessage.add(py);
  oscP5.send(myMessage,myBroadcastLocation);

}