Curve from Grasshopper to Processing via UDP

Hello Processing Community, I am a newbie to both Grasshopper and Processing and I'm trying to do a simple exercise, without any results. What I'm trying to do is to import in real time a list of curve vertices from Grasshopper using gHowl UDP sender and use this vertices to draw a curve in Processing. When I run the definition I get the error: Cannot find anything named "vert". I know that the solution will be very simple but in this moment I can't find it.

import processing.opengl.*;
import hypermedia.net.*;
import peasy.*; 
PeasyCam cam;   
UDP udp;  
Curve c;
void setup() {
  size(800, 800, OPENGL);
  udp = new UDP( this, 4000 );
  udp.log( true );     
  udp.listen( true ); 
  c = new Curve();  
  cam = new PeasyCam(this, 0, 0, 0, 1000);
  cam.setMinimumDistance(10);
  cam.setMaximumDistance(10000);
  cam.setDistance(300);
  frameRate(60);
}
void draw() {
  scale(10);
  background(255);
  noStroke();
  if ( mousePressed ) {
    stroke(0); 
    strokeWeight(5);
    c.drawVertices();
    fill(200, 200, 0); 
    noStroke();
    //if( frameCount % 40 == 0)
    stroke(0); 
    strokeWeight(1);
  } 
  void receive( byte[] data, String ip, int port ) { 
    int time = millis();
    //println("\n");
    String message = new String( data );
    println("UDP-message: "+message.length() + " chars");
    //println( "receive: \""+message+"\" from "+ip+" on port "+port );
    c.prepareData(message);
    println("                          time: "+(millis() - time) +" ms");
    //println(message);
  }
  public class Curve {
    ArrayList<Vertice> v = new ArrayList<Vertice>();
    public Curve() {
    } 
    public void prepareData(String udp_data) {
      println(udp_data);  
      ArrayList<Vertice> v_tmp = new ArrayList<Vertice>(); 
      String lines[] = split(udp_data, '\n');
      String tmpLine = "";
      for (int i = 0; i < lines.length; i++) {      
        if (  lines[i].startsWith("v") ) {
          tmpLine = lines[i].replace('v', ' ');
          String coords[] = tmpLine.split(",");
          v_tmp.add(new Vertice( float(coords[0]), float(coords[1]), float(coords[2])  ));
        } 
        if ( v_tmp.size() == 0) v_tmp = null;
        v = v_tmp;
      } 

      public void drawVertices() {
        if ( v == null) return;
        for ( Vertice vert : v ) {
          point(vert.x, vert.y, vert.z);
        }
      }

      public void drawCurves() {
        if ( v == null || v.size() == 0 ) return;
        for ( int i = 0; i < v.size(); i++ ) {
          Vertice vertice = v.get(i);
          int index1 = vert.x;
          int index2 = vert.y;
          int index3 = vert.z;
          int indexMax = v.size()-1;
          if ( index1 > indexMax || 
            index2 > indexMax ||
            index3 > indexMax ) continue;
          noFill();
          beginShape();
          curveVertex ( v.get(index1).x, v.get(index2).y, v.get(index3).z);
          curveVertex ( v.get(index1).x, v.get(index2).y, v.get(index3).z);
          curveVertex ( v.get(index1).x, v.get(index2).y, v.get(index3).z);
          curveVertex ( v.get(index1).x, v.get(index2).y, v.get(index3).z);
          curveVertex ( v.get(index1).x, v.get(index2).y, v.get(index3).z);
          curveVertex ( v.get(index1).x, v.get(index2).y, v.get(index3).z);
          endShape();
        }
      }
      class Vertice {
        float x, y, z;
        Vertice(float x, float y, float z) {
          this.x = x;
          this.y = y;
          this.z = z;
        }
      } // end class Vertice{}
    }

Grasshopper curve_UDP

Answers

  • it's always an idea to say WHERE the error says it is, not just WHAT.

    looking at the code i'm guessing line 68 etc

    on line 67 you've called it 'vertice' but on line 68 you refer to it as 'vert'.

  • also, ctrl-t in the pde editor will indent your code nicely.

  • @koogs, I'm really sorry for my bad post, error is at line 68 as you suggest..

  • I have edited your post to show line numbers

  • I have edited your post to show line numbers

    i already did that, hence me being able to mention line 68. now, somehow, you have line 68 as a blank line.

    anyway, line 72 needs to use 'vert' as the variable name to match the lines that follow.

  • Sorry, it was my fault.. I edited the code with ctrl-t. Thank you for your help!

  • edited June 2015

    Hey! I need your help for one more question. I modified the code and now I'm able to import one curve from Grasshopper to Processing, but I need to import more than just one. When I try to send more vertices I can read the data correctly in Processing but it's drawing only the last curve of the list. Where am I doing wrong?
    (P.s.: How do you format the code to show line numbers it in the right way in the post? Sorry for the stupid questions but I started studying recently..) Thanks in advance!

    public class Curve {  
    
      ArrayList<Vertice> v = new ArrayList<Vertice>();  
    
      public Curve() {
      } 
      //DATA EDITING
      public void prepareData(String udp_data) {
        println(udp_data); //print udp string (check data!)
    
        ArrayList<Vertice> v_tmp = new ArrayList<Vertice>(); 
    
        String lines[] = split(udp_data, '\n');  //split data string 
        String tmpLine = "";
        for (int i = 0; i < lines.length; i++) {  
    
          if (  lines[i].startsWith("v") ) {
            tmpLine = lines[i].replace('v', ' ');
            String coords[] = tmpLine.split(",");
            v_tmp.add(new Vertice( float(coords[0]), float(coords[1]), float(coords[2])  ));
          }
        } 
    
        if ( v_tmp.size() == 0) v_tmp = null;
        println("v_tmp.size =" + v_tmp.size() ); 
    
        v = v_tmp;  
        println("v.size =" + v.size());
      } 
    
      //DRAW CONTROL POINTS
      public void drawVertices() { 
        if ( v == null) return;
    
        for (int i = 0; i<v.size (); i++) {  
          Vertice vert = v.get(i);
          point (vert.x, vert.y, vert.z);
          //println(vert);
        }
      } 
    
      //DRAW CURVES 
      public void drawCurves() {  
        if ( v == null || v.size() == 0 ) return;
    
        beginShape();
        for (int i=0; i<v.size (); i++) {  
          //    Vertice vert = v.get(i);
          curveVertex ( v.get(i).x, v.get(i).y, v.get(i).z);
        }
        endShape();
      }
    } // end class Curve
    
    class Vertice {
      float x, y, z;
      Vertice(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
      }
    } // end class Vertice
    

    Untitled

Sign In or Register to comment.