Rachel Max
Rachel Max

Reputation: 43

playing PDE files in a loop?

I'm still relatively new to Processing. We are thinking of showing our Processing work in a loop for a small exhibition. Is there a way to play multiple PDEs in a loop? I know I can export as frames and then assemble them as a longer loop-able Quicktime file, but I'm wondering if there's any way to play and loop the files themselves?

Also, for interactive PDEs, what's the best way to present them? We are thinking of having a couple of computers with PDEs running in Processing but it would be nice to have a file run for 20 minutes and then open another file for 20 minutes.

Thanks in advance!

Upvotes: 3

Views: 194

Answers (1)

George Profenza
George Profenza

Reputation: 51857

You should be able to put together a shell/batch script that uses the processing-java command line executable.

You should be able to set that up via the Tools > Install "processing-java"

Install processing-java menu item

If you're not confortable with bash/batch scripting you could even write a Processing sketch that launches Processing sketches

whoa!

Here's a very rough take on it using selectFolder() and exec():

final int SKETCH_RUN_TIME = 10000;//10 seconds for each sketch, feel free to change
ArrayList<File> sketches = new ArrayList<File>();

int sketchIndex = 0;

void setup(){
  selectFolder("Select a folder containing Processing sketches:", "folderSelected");
}

void draw(){

}
void nextSketch(){
  //run sketch
  String sketchPath = sketches.get(sketchIndex).getAbsolutePath();
  println("launching",sketchPath);
  Process sketch = exec("/usr/local/bin/processing-java",
       "--sketch="+sketchPath,
       "--present");
  //increment sketch index for next time around (checking the index is still valid, otherwise go back to 0)
  sketchIndex++;
  if(sketchIndex >= sketches.size()){
    sketchIndex = 0;
  }
  //delay is deprecated so you shouldn't use this a lot, but as a proof concept this will do
  delay(SKETCH_RUN_TIME);
  nextSketch();
}

void folderSelected(File selection) {
  if (selection == null) {
    println("No folder ? Ok, maybe another time. Bye! :)");
    exit();
  } else {
    File[] files = selection.listFiles();
    //filter just Processing sketches
    for(int i = 0; i < files.length; i++){
      if(files[i].isDirectory()){
        String folderName = files[i].getName();
        File[] sketchFiles = files[i].listFiles();
        boolean isValidSketch = false;
        //search for a .pde file with the same folder name to check if it's a valid sketch
        for(File f : sketchFiles){
          if(f.getName().equals(folderName+".pde")){
            isValidSketch = true;
            break;
          }
        }
        if(isValidSketch) {
          sketches.add(files[i]);
        }else{
          System.out.println(files[i]+" is not a valid Processing sketch");
        }
      }
    }
    println("sketches found:",sketches);
    nextSketch();
  }
}

The code is commented so hopefully it should be easy to read and follow. You will be prompted to select a folder containing sketches to run.

Note 1: I've used the processing-java path on my machine (/usr/local/bin/processing-java). If you're on Windows this may be different and you need to change this.

Note 2: The processing-java command launches another Process which makes it tricky to close the previous sketch before running the next one. As a workaround for you you could call exit() on each sketch after the amount of time you need.

Note 3: The code will not recursively traverse the selected folder containing sketches, so only the first level sketches will be executed, anything deeper should be ignored.

Upvotes: 3

Related Questions