Waiting for an external process to complete

edited December 2013 in Share Your Work

Here is an example where I create a set of povray files to render a processing "sketch" as a povray "scene". I start the povray program from the processing sketch (line 83) and wait for it to complete (line 85), before displaying the raytraced image in the sketch window. I just thought someone else might find this approach useful. Also I redirect the povray output to the processing console see line 81. See my modified supercad library here. Works with processing-2.1.0.

import java.util.logging.Level;
import java.util.logging.Logger;
import supercad2.Mode;
import supercad2.Status;
import supercad2.*;


Mode cad;
Status status;

void setup() {
  size(800, 600, P3D);
  status = Status.START;
}

void draw() {  
  if (status == Status.TRACED) {         
    PImage img = loadImage(System.getProperty("user.home") + File.separator + "output.png");
    background(img);
  } 
  else {
    if (status == Status.RECORD) {
      beginRaw("supercad2." + cad.className, "output." + cad.ext);
      status = Status.RECORDING;
    }

    fTest();

    if (status == Status.RECORDING) {
      endRaw();
      status = Status.RECORDED;
    }
  }
}

void configureLights() {
  lights();
  ambientLight(100, 100, 100);
  directionalLight(5, 0, 0, -1, -1, -1); // colored directional
  directionalLight(0, 0, 5, 1, -1, -1);  // lights essential
  directionalLight(5, 5, 0, 1, 1, 1);  // for 3D illusion
}

PVector midPoint(PVector a, PVector b) {
  PVector result = PVector.add(a, b);
  result.div(2);
  return result;
}

void fTest() {  // encapsulate initial processing sketch in a function
  translate(width / 2, height / 2, -width / 3);
  background(0);
  configureLights();
  rotateY(QUARTER_PI);
  fill(255, 0, 0);
  translate(0, -60, 0);      
  box(120);
  translate(0, -120, 0);
  box(120);
  translate(120, 0, 0);
  box(120);
  translate(-120, -120, 0);
  box(120);
  translate(0, -120, 0);
  box(120);
  translate(120, 0, 0);
  box(120);
}

void keyPressed() {
  switch (key) {
  case 'p':
    cad = Mode.POVRAY2;
    status = Status.RECORD;
    break;
  case 't':         // start povray
    if (status == Status.RECORDED) {
      String[] ini = {"/usr/bin/povray", sketchPath("output.ini")};
        ProcessBuilder pb = new ProcessBuilder(ini);
      Process povray;
      pb.inheritIO();
      try {
        povray = pb.start();
        status = Status.TRACING;
        if (status == Status.TRACING && povray.waitFor() == 0) {
          status = Status.TRACED;
        }
      } 
      catch (IOException  ex) {
        Logger.getLogger(ftest.class.getName()).log(Level.SEVERE, null, ex);
      }
      catch (InterruptedException ex) {
        Logger.getLogger(ftest.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    break;
  }
}
Sign In or Register to comment.