My Processing Template Sketch
Processing makes it very easy to save your work in different formats. But writing the code to save movies, images and PDF files from the same sketch is boring. I’ve made a template sketch I can reuse so I don’t have to write this kind of code over and over again. The template consists of three files:
template.pdeMain template sketch.
config.pdeA configuration file with the directories to save images, PDF files, movies and the number of frames for the length of the movie.
functions.pdeSome functions to generate filenames for the PDF and video files.
If you’re familiar with Processing, you should be able to figure out what everything in this sketch is doing. I’ll only explain the basic idea behind it in this article.
Most of the magic happens in the
draw() method. The first block of code starts recording a new PDF or video file. The second part is where I do all the drawing. The third part finishes recording the PDF file, adds a frame to the video file and finishes the file after a certain amount of frames set in the config file, and saves images.
I’m using the
ANIMATION codec at
LOSSLESS quality, Full HD so I usually end up with big video files. This is handy if you need to do some post-production.
The configuration file has three String variables I use for the directories to which each type of file gets saved. I don’t like my files ending up in the same directory. The
maxFrames variable is used to set the length of the movies. I’m using 750 frames at 25 frames per second so I end up with 30 seconds of video.
bc. String IMAGE_DIRECTORY = "images/";
String PDF_DIRECTORY = "pdf/";
String VIDEO_DIRECTORY = "movies/";
int maxFrames = 750;
getSketchName()returns a String with the name of the current sketch. This function is called when I save images and when I need to generate a new filename for a PDF or video file.
newVideoFileName()returns a String with a new filename for a video file. The filename is made from the
VIDEO_DIRECTORY, the sketch name, a five digit random number and the
newPDFFileName()is basically the same as
newVideoFileName(), but it returns a string for a new PDF file.
- s Save the current frame as a PNG file.
- p Save the current frame as a PDF file.
- m Start recording a new video file.
Download my Processing Template sketch.