Nervous System OBJ Export

Hello! Wondering if anyone is knowledgeable around using nervoussystem library for obj export from Processing. I used it on one file and it worked - now trying it on a different file and it’s creating the obj, says there are __bites (usually 75-150 bites), but the file seems corrupt and won’t open in other applications. Thinking I’m putting the if (record) lines in incorrect spot, but I tried other places and can’t seem to find one that works.

Here’s my code below – sorry for the messiness as I’m a new coder. :smile:

Thanks in advance, kirstee_baxt

import nervoussystem.obj.*;
import peasy.*;
Table myTable;
String table_fn="data/data-test.csv";
int numEntries;
float time, radius, day;
boolean diagp = true;
PShape[] myShapes;
PeasyCam cam;

//for export
boolean record = false;
String filename = "Data_TypeA_"+month()+day()+hour()+minute()+".obj";

void setup(){
  size(800, 800, P3D);
  myTable = loadTable(table_fn, "header");
  if (diagp) println("load file: "+table_fn);
  numEntries = myTable.getRowCount();
  make_shapes();
  cam = new PeasyCam(this,1000);
}

void make_shapes(){ 
  noFill();
  int startday = myTable.getRow(0).getInt("Day");
  int endday   = myTable.getRow(numEntries-1).getInt("Day");
  int days = endday - startday+1;
  if (diagp) println("start day "+startday+" endday "+endday+" days "+days);
  myShapes = new PShape[days];
  int k = 0;
  for ( int goday = startday; goday <= endday; goday++ ) {  // group shapes per day
    myShapes[k] = createShape();
    println("make shape "+k);
    myShapes[k].beginShape();
    for (int i = 0; i < numEntries; i++) {
      time = myTable.getRow(i).getFloat("Time Decimal");
      radius = myTable.getRow(i).getFloat("Productivity Level");
      day = myTable.getRow(i).getFloat("Day");
      //time is x, radius is y, day is z
      
      if ( day == goday ) {
      if (diagp) println("#:i "+i+" x", time+" time decimal ", radius +" productivity ", day +"day");
      float angle = map(time, 0, 1, 0, TWO_PI);
      float xCenter = width/20; 
      float yCenter = height/20;
  
      float xCoord = xCenter + cos(angle-(TWO_PI/4)) * (radius*3);
      float yCoord = yCenter + sin(angle-(TWO_PI/4)) * (radius*3);
      float zCoord = (day * 80); //multiplier is random for separation
      
      myShapes[k].vertex(xCoord, yCoord, zCoord);
      }
    }
      myShapes[k].endShape();
      k++; 
  }
}

void draw() {
//background(135); //removed because creates plane with obj export
if (record) {
    beginRecord("nervoussystem.obj.OBJExport", filename); 
  } 
for ( int k = 0 ; k < myShapes.length; k++ ) {
  shape(myShapes[k]);
  endShape();
}
if (record) {
    endRecord();
    record = false;
  }
}

void keyPressed() {
  if (key == 's') {
    record = true;
  }
}
1 Like

Just a remark : the endshape in draw seems unnecessary

2 Likes

I’m not familiar with it, no. Yes, endShape is something that you pair with beginShape (not needed when just using shape().

You said one file worked, another didn’t. What file – a csv file? Can you boil them down to simple examples (a few lines) of working and not working? Maybe share them. It may be bad data – or bad error handling (null values, zeros, extra columns etc)

1 Like

Two years ago I was having problems with it too so I wrote this (sorry for pasting an image, don’t know where the code is…)

1 Like

Transcript (not checked, may contain OCR errors):

private void exportMesh(PShape mesh) {
  StringBuilder verts = new StringBuilder();
  StringBuilder faces = new StringBuilder();
  final int vertsNum = mesh.getVertexCount();
  final PVector v = new PVector();
  for (int i=0; i<vertsNum; i+=3) {
    mesh.getVertex(i, v);
    verts.append("v " + v.x + " " + v.y + " " + v.z + "\n");
    mesh.getVertex( i+1, v);
    verts.append("v " + v.x + " " + v.y + " " + v.z + "\n");
    mesh.getVertex( i+2, v);
    verts.append("v " + v.x + " " + v.y + " " + v.z + "\n");
    faces.append("f " + (i+1) + " " + (i+2) + " " + (i+3) + "\n");
  }
  PrintWriter output = createWriter( "thing.obj");
  output.println("o Sphere\n");
  output.println(verts);
  output.println(faces);
  output.flush();
  output.close();
}   

What is that syntax “index:” and “filename:” doing…? I don’t think I’ve seen anything like that in Java before.

That’s the OCR in action :slight_smile: IntelliJ Idea shows you the names of the arguments. It’s not part of the code. Sorry about the confusion.

ah, got it. Okay, I think I’ve fixed the code above.