Intro to vpype and vsketch

I've been doing creative coding for just under a year now. One heavy hitter in this domain is Processing, which has been around for ages. It's a good choice for beginners - owing to its age, there are tons of resources for new learners (Coding Train is amazing) and a large community. The syntax is also easy to understand, and the core library is full of features. But more recently I've been experimenting with vpype and vsketch. In this post I want to outline what vpype and vsketch are, how they can be used, and the advantages and disadvantages of using vpype and vsketch versus Processing.

What are vpype and vsketch?

Where Processing is both a programming language (based on Java) as well as a general purpose sandbox for visual art (IMO), vpype aims to specifically help with generating or modifying artwork that will be physically rendered with a plotter. It is written in Python and can be extended with plugins (more on this below). Generally speaking, vpype is designed to be used as a command line interface, but it can also be used programmatically from other Python scripts.

In fact, this is exactly what vsketch does. vsketch builds on top of vpype to provide a very powerful toolkit for rapid prototyping and testing of creative coding sketches. It includes built-in support for transformations, primitives, and layers. It's not quite a drop-in replacement for Processing's code editor and viewer but I certainly felt right at home.

Why use vpype and vsketch?

vpype and vsketch have both advantages and disadvantages versus Processing. Let's start with their benefits:

  1. vpype provides powerful tools for layout, optimization, and layer manipulation. If you've generated plotter artwork using Processing, you're probably familiar with the following frustrations:

    • Getting sizes correct. I plot on a variety of different paper sizes - 7x10 inches, 8.5x11 inches, 9x12 inches, and others. In Processing, you have to figure out what the dimensions are in pixels. vpype accepts a variety of different units and also supports easy scaling and cropping. To create a 7x10 inch plot, I can just set the page size to "7x10in" - no need to convert to pixels.
    • Separating plots into layers. This is a real pain in the ass for me - my plots often have thousands of individual strokes and different strokes in varying colors. Processing's SVG implementation provides no native support for layers. While there are plugins for Inkscape that can separate layers based on properties (e.g., stroke or fill style) it can be excruciatingly slow for large files (in fact, I got so frustrated that I wrote my own Python script to do this before discovering vpype).

      vpype has fantastic support for layers. Not only can you explicitly (and seamlessly) switch between different layers while generating lines, but many (all?) plugins support the option to perform operations on an explicitly delimited list of layers. So, for example, if you only want to use the occult plugin on a few layers and not others, you have this option.

    • Long plot times due to excessive numbers of points in primitives. This is especially noticeable when you plot lots of really small primitives, such as circles. vpype includes a linesimplify plugin that reduces the number of points in lines (with adjustable tolerance limits).

  2. If you already know and love Python, no need to learn another language. If you're looking for something like Processing but you don't want to learn another language, vsketch is great. I was able to prototype plots just as rapidly as in Processing - for example, these randomized flower(ish) designs took me all of about an hour, and that was before I had really familiarized myself with vsketch's API, so there was a lot of flipping back and forth between code and documentation:

    Random Flower 1 Random Flower 2 Random Flower 3

  3. What vpype and vsketch don't provide by default, you can implement as a plugin. For example, take another common problem with Processing's SVG output: Occlusion! Because Processing's SVG library doesn't support layers, occlusion isn't really possible (or at least it is far from trivial).

    But vpype has an occult plugin which does just this for you. By default it allows you to occlude lines for objects in the same layer, and I submitted a PR yesterday to allow occlusion between different layers as well. See how each set of petals in the examples above cover the lines of "lower" petals? That is the occlude plugin at work.

    Or suppose you want to arbitrarily shorten all lines by a certain percentage? There's no plugin for this yet (AFAIK), but I'm almost finished writing my own plugin to do this. In my opinion, the best feature of vpype and vsketch is the ease with which the default functionality can be extended. I looked into writing my own libraries for Processing and it seemed pretty daunting. By contrast, the setup for plugins in vpype seemed much simpler - I was able to get up and running with a plugin in vpype very quickly.

  4. Supporting parameterization in the interface is way easier than in Processing. In Processing you have to use a GUI library like ControlP5 to implement the interface and it requires a lot of code. vsketch includes a Param class that allows you to quickly create parameters with a few lines of code. Once you create an instance of Param, the vsketch viewer includes that parameter in a configuration section. This means that you don't have to flip back and forth between your code and the viewer. You can just add a param and then dynamically update the value directly in the viewer to see the effect of different configurations. Also, by default, vsketch's viewer always includes a random seed parameter.

  5. Live reload in the vsketch viewer. If you do need to make a change to the code, no need to restart the viewer! The viewer updates in real time to immediately show the effect of your changes. One caveat: If your code has a bug, the viewer stops updating and you have to restart it.

What are vpype and vsketch not good for?

There are a few good reasons you might not want to use vpype and vsketch:

  1. No 3D support. All operations in vpype and vsketch are strictly 2-dimensional. I use 3 dimensions all. the. time. in my plots - you can get some really interesting geometries, and it's especially useful for creating animated visualizations. That having been said, this isn't as much of a showstopper as it might seem on first glance - if you're willing to get your hands dirty. Under the hood, vpype and vsketch use the Shapely library to handle geometry. In theory, you could generate 3D geometries using Shapely and then use something like this to flatten the geometry to 2 dimensions. (For the record, I have not tried this yet - but I plan to experiment!)
  2. The API for vsketch is not extensive. vsketch's API does provide support for the most critical operations. But if you compare vsketch's API against Processing's API... Yeah, Processing provides significantly more flexibility. Translating this into level of effort, this might mean that you spend more time rolling your own solutions.
  3. No stroke/fill colors! In vsketch, stroke and fill are really just a way to specify which layer the stroke and fill belong on - you can't explicitly set RGB or RGBA values! And this makes perfect sense if you think about the use case for vpype and vsketch. The primary use case for vpype and vsketch is plotting. Setting stroke and fill styles in a Processing sketch makes sense because you might only care about the digital output. But if you're trying to plot the result, this doesn't make sense at all - you really only care about the physical output and when each layer gets plotted. If I'm plotting two sets of lines with different colors of pens, the stroke and fill in the SVG has no effect on the plot itself, since the plot color only changes if I physically change out the pen.

How to Get Started

So you like what you're hearing: How do you get started? I suggest starting with vsketch. vpype is primarily useful from the command line - if you want to pass in one or more files, combine them in some way, optimize existing SVGs, etc., then it is very useful for that. But if you want to write new creative coding sketches from scratch, start with vsketch.


Comments !