# Difficulty using a Grasshopper slider as my point attractor in Processing (through Osc)

**URL:** https://discourse.processing.org/t/difficulty-using-a-grasshopper-slider-as-my-point-attractor-in-processing-through-osc/40366
**Category:** Libraries
**Created:** [December 30, 2022, 12:47am UTC](https://discourse.processing.org/t/difficulty-using-a-grasshopper-slider-as-my-point-attractor-in-processing-through-osc/40366 "2022-12-30T00:47:33Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![newbieprocessing](https://avatars.discourse-cdn.com/v4/letter/n/f475e1/32.png) [@newbieprocessing](https://discourse.processing.org/u/newbieprocessing)
#### Post date: [December 30, 2022, 12:47am UTC](https://discourse.processing.org/t/difficulty-using-a-grasshopper-slider-as-my-point-attractor-in-processing-through-osc/40366/1 "2022-12-30T00:47:33Z")

</div>

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.

Are there any steps which I might be missing?

Thank you

 ![osc](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/f/ef7b22f2d46af9993646fb2092efb0750fd5f8b9.png)

```auto
// 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();
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 31, 2022, 1:42am UTC](https://discourse.processing.org/t/difficulty-using-a-grasshopper-slider-as-my-point-attractor-in-processing-through-osc/40366/2 "2022-12-31T01:42:48Z")

</div>

Hello @newbieprocessing,

Cool stuff!

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

```auto
// 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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/b/bbde43909861c2013b381b6a8e0a2529c3713f1e.png)

I used PVectors because they were easier for me to implement for x, y data points.

Have fun!

Reference:

> **[Grasshopper 3D](https://en.wikipedia.org/wiki/Grasshopper_3D)**
>
> Grasshopper is a visual programming language and environment that runs within the Rhinoceros 3D computer-aided design (CAD) application. The program was created by David Rutten at Robert McNeel & Associates. Programs are created by dragging components onto a canvas. The outputs to these components are then connected to the inputs of subsequent components.
> Grasshopper is primarily used to build generative algorithms, such as for generative art. Many of Grasshopper's components create 3D geometry...

`:)`
