Hi everyone, I am relatively new to Processing and am currently trying to send information from Grasshopper to Processing using Osc. From my understanding, Processing is receiving the informaiton however it is not doing what I am trying to achieve which is using the MD Slider in Grasshopper as a point attractor for my shapes in Processing (as seen on screenshot). I am attaching the whole code as I myself am not sure where it could have gone wrong.
// receive point attractor from grasshopper
// import communication library
import oscP5.*;
// instanciate library object
OscP5 oscP5;
// create a gobal variable for the position
float x;
float y;
boolean received = false;
Window [][] win;
Window [][] win2;
int winSize = 30;
color c1 = (255);
color c2 = color(255, 0, 0);
void setup()
{
size(400, 400);
rectMode(CENTER);
oscP5 = new OscP5(this, 12000);//receiving in this port
win = new Window[10][10];
win2 = new Window[10][10];
for (int i = 0; i < win.length; i++)
{
for (int j = 0; j < win.length; j++)
{
win2[i][j] = new Window(i*width/10, j*height/10, winSize, c2);
win[i][j] = new Window(i*width/10, j*height/10, winSize, c1);
}
}
}
void draw()
{
background(c1);
if (!received)
{
x = mouseX;
y = mouseY;
}
else
{
x = x;
y = y;
received = false;
}
pushMatrix();
translate(width/20, height/20);
for (int i = 0; i < win.length; i++)
{
for (int j = 0; j < win.length; j++)
{
win2[i][j].draw();
win[i][j].draw();
}
}
popMatrix();
}
void oscEvent(OscMessage theOscMessage)
{
// print the address pattern and the typetag of the received OscMessage
print("Received an osc message. ");
print("addrpattern: "+theOscMessage.addrPattern());
println(" typetag: "+theOscMessage.typetag());
println(" x = "+theOscMessage.get(0).stringValue());
println(" y = "+theOscMessage.get(1).stringValue());
x = float(theOscMessage.get(0).stringValue());
y = float(theOscMessage.get(1).stringValue());
received = true;
for (int i = 0; i < win.length; i++)
{
for (int j = 0; j < win.length; j++)
{
win[i][j].mouseMoved();
}
}
}
void mouseMoved()
{
// double array
for (int i = 0; i < win.length; i++)
{
for (int j = 0; j < win.length; j++)
{
win[i][j].mouseMoved();
}
}
}
It looks like there is a whole class missing in your code.
Some code below that will:
send mouse location (x, y) using OSC
receive data (x, y) using OSC
draw a line to x, y location in sketch
// https://discourse.processing.org/t/osc-messages-from-processing-to-supercollider/17441
// Receive point attractor from Grasshopper
// Simulated with mouse and OSC data sent locally over network
// Inspiration:
// https://discourse.processing.org/t/osc-messages-from-processing-to-supercollider/17441/2
// Modifications to code and embellishments by glv 2022-12-30
import oscP5.*;
import netP5.*;
OscP5 oscTx, oscRx;
// Global variables
float ox;
float oy;
PVector [][] grid;
PVector v2, v3;
NetAddress myRemoteLocation;
public void settings()
{
size(600, 600);
}
public void setup()
{
v2 = new PVector(0, 0);
v3 = new PVector(0, 0);
oscRx = new OscP5(this, 12000);//receiving in this port
oscTx = new OscP5(this, 12001);//receiving in this port
grid = new PVector [20][20];
// Initialize all PVectors in Grid
for (int y=0; y<20; y++)
{
for (int x=0; x<20; x++)
{
grid [y][x] = new PVector(x*30+15, y*30+15, 0);
}
}
myRemoteLocation = new NetAddress("127.0.0.1", 12000);
}
public void draw()
{
background(255);
strokeWeight(2);
stroke(255, 0, 0);
line(width/2, height/2, mouseX, mouseY);
stroke(0, 255, 0);
line(width/2, height/2, ox, oy);
// Draws a grid
for (int y=0; y<20; y++)
{
for (int x=0; x<20; x++)
{
v3 = grid[y][x].copy();
point(v3.x, v3.y);
}
}
}
public void oscEvent(OscMessage theOscMessage)
{
// print the address pattern and the typetag of the received OscMessage
print("Received an osc message. ");
print("addrpattern: "+theOscMessage.addrPattern());
println(" typetag: "+theOscMessage.typetag());
ox = PApplet.parseFloat(theOscMessage.get(0).stringValue());
println(" x = "+theOscMessage.get(0).stringValue());
oy = PApplet.parseFloat(theOscMessage.get(1).stringValue());
println(" y = "+theOscMessage.get(1).stringValue());
}
public void mouseDragged()
{
OscMessage myMessage0 = new OscMessage("/Hello World");
myMessage0.add(str(mouseX));
myMessage0.add(str(mouseY));
oscTx.send(myMessage0, myRemoteLocation);
}
I did not include the code to rotate lines to follow received data: