[Help] How to write a code to save every x frames then reset? Substrate Code

I am new to processing and wanted to work with the Substrate code. When I try to insert saving functionality, it typically bounces back "Duplicate method draw() in type Substrate". I am trying to get the sketch to run, and because it iterates endlessly, save sequentially to a png image file every time X amount of frames pass. Here is what I'm trying to insert:

int x = 0; void draw() { background(204); if (x < 100) { line(x, 0, x, 100); x = x + 1; } else { noLoop(); } // Saves each frame as line-000001.png, line-000002.png, etc. saveFrame("line-######.png"); }

Into the original code. Additionally, If its possible to make the sketch stop after x amount of frames, run again, and save the new files such as a_line-######.png, b_line-######.png, c_line-######.png, etc. so that it could left to run and save randomized sketches that would be helpful.

Source

Reference Gif

Answers

  • // forum.processing.org/two/discussion/11439/
    // help-how-to-write-a-code-to-save-every-x-frames-then-reset-substrate-code
    
    static final int FRAMES = 10, FPS = 5;
    char label = 'a';
    
    void setup() {
      smooth(4);
      frameRate(FPS);
    }
    
    void draw() {
      background( (color) random(#000000) );
      saveFrame( dataPath(label + "_line-#####.jpg") );
    
      println(label, frameCount);
      if (frameCount % FRAMES == 0)  if (label++ == 'z')  noLoop();
    }
    
  • If its possible to make the sketch stop after x amount of frames, run again

    you just need to call begin() to reinitialise the sketch from time to time.

    i'd keep it simple like this (at bottom of draw()?):

    // reset every 10000th frame
    if (frameCount % 10000 == 0) {
      begin();
    }
    // save every 100th frame
    if (frameCount % 100 == 0) {
      saveFrame("line-########.jpg");
    }
    

    so your filename will be line-xxxxyyyy.jpg where xxxx is the iteration and yyyy is the frame within the iteration, 100 to 9900. (no idea if these numbers are suitable)

Sign In or Register to comment.