A Processing Boilerplate with Gcode Export

I’ve been using a standard boilerplate template in Processing that I wrote for myself a while back and I’ve been slowly extending it with more export features. Having just finished a Gcode-export feature, I figured I’d release it publicly in case others find it handy.

You can find the code on Github.

This boilerplate is aimed at generative artists working with static images in Processing. Some of its features include:

Usage

You will first need to install the Geomerative Processing library. This can be done from Sketch --> Import Library --> Add Library. Just search for Geomerative and click Install in the bottom left of the window.

The code folder contains a boilerplate folder with two files (boilerplate.pde and scaffold.pde). The folder can be duplicated for any new sketches you write. The scaffold.pde file contains all the configuration options at the top of the file (variables starting with the prefix CONFIG_ can be modified).

The boilerplate.pde file contains an example of how to write a sketch that works with the scaffolding code. Basically, you just implement a render() function in boilerplate.pde

If you’re starting a new sketch, just copy over the two files, and rename boilerplate.pde to a file name of your choice. If you’re using an existing sketch, you’ll have to re-arrange your code to have all your drawing logic in a single render() function that draws everything on screen.

Configuring Screen Size

In Processing, you would normally call size() to set the screen size. However, the scaffolding code does this for you using two parameters called CONFIG_WIDTH_PIXELS and CONFIG_HEIGHT_PIXELS. Just modify these in scaffold.pde to change your screen size.

Runtime Help

Some keyboard shortcuts are bound to specific actions and these can be viewed by pressing ? while running the sketch. Here is the output it gives:

Keyboard shortcuts:
  n: Generate a new seeded image
  l: Save low-resolution image
  h: Save high-resolution image
  p: Save PDF version
  s: Save SVG version
  g: Save Gcode for plotter
  G: Dump Gcode to serial output

Pressing n generates a new seeded image, while l, h, p, s, and g write files into your sketch folder. Files are consistently named in the format prefix-<seed>.<extension>. The prefix specifies the export type (can be lowres, highres, vector, or gcode). The seed specifies the number used as the seed for the random-number generator in case you need to regenerate an identical image in the future. Finally, extension is one of svg, pdf, png, or gcode.

High-Resolution PNG Output

To generate a high-resolution version of your image, just set the CONFIG_SCALE_FACTOR in scaffold.pde to an appropriate value. The CONFIG_SCALE_FACTOR value is used as the multiplier for the number of pixels. (e.g, a canvas of 500x500px with a scale factor of 10 gives a 5000x5000px image when h is pressed).

GCode Configuration

The scaffolding code contains basic code to output most shapes to standard Gcode for use with pen plotters.

NOTE: CURRENTLY, USING THE arc() CALL DOES NOT WORK DUE TO A BUG IN THE GEOMERATIVE LIBRARY.

The following configuration parameters exist to allow you to configure the Gcode generation to your needs: