Blender

Behind the scenes, Coldtype uses the Skia library to rasterize two-dimensional vectors. But what if we want to rasterize three-dimensional graphics? One very fun option is to use Blender.

Because Blender has an incredible Python API, it’s not too difficult to use it programmatically — i.e. to write a normal Coldtype script, mark a few things (specific to Blender), and then let Blender & Coldtype take care of the translation to three dimensions. Here’s an example:

from coldtype import *
from coldtype.blender import *

fnt = Font.Find("SwearCilatiVariable")

@b3d_animation(timeline=60)
def varfont(f):
    return (Glyphwise("Vari", lambda g:
        Style(fnt, 325,
            opsz=f.adj(-g.i*5).e("seio", 1, rng=(0.98, 0)),
            wght=f.adj(-g.i*15).e("seio", 1, rng=(0.98, 0))
            ))
        .align(f.a.r)
        .pmap(lambda i, p: p
            .ch(b3d(lambda bp: bp
                .extrude(f.adj(-i*5)
                    .e("ceio", 1, rng=(0.015, 3)))))))

But before you can begin, you’ll need to install Coldtype using the python bundled with Blender.

Installing

Installing packages in Blender requires locating the bundled python binary included with Blender itself. The path to this binary varies based on the version of Blender you have installed, but should look something like this:

/Applications/Blender.app/Contents/Resources/2.93/python/bin/python3.9

The 2.93 and the 3.9 might be different depending on your installation.

On my computer, I’ve aliased that path to something I call b3d_python, so there’s a line like this in my ~/.bash_profile:

alias b3d_python='/Applications/Blender.app/Contents/Resources/2.93/python/bin/python3.9'

I also have a line to address the Blender executable itself:

alias b3d='/Applications/Blender.app/Contents/MacOS/blender'

This is useful because when you’re running Python scripts in Blender on Mac, you need to start Blender from the command-line, in order to see errors and logs generated by the Python. If you start Python from the GUI Application in your Applications folder, you’ll be unable to see anything printed or thrown from your script.

Once you have all that out of the way, you can pip install things using that executable, like so:

b3d_python -m pip install "coldtype"

Running Code in Blender

To get a Blender window to show up, all you need to do is use the @b3d_animation decorator in place of the standard @animation decorator, and add -bw 1 to the command-line invocation.

So, to use an example from the Coldtype repo, you could run:

coldtype examples/blender/varfont.py -bw 1

This should launch both a standard Coldtype window (with a 2D Skia render) and a Blender GUI window, which should automatically render the same thing as the 2D window, except in 3D. Put another way: you do not need to open Blender yourself, since Coldtype launches it as a background process (necessary to connect the live-code-reloading part of Coldtype to Blender). To quit both Coldtype and Blender, just hit ctrl-c in the terminal.

What’s different in Blender is that the contents of the scene aren’t re-created from scratch every time you render; instead, you annotate specific elements in your returned result, then those annotated results are displayed in Blender, as persistent objects. This means you can use Blender in a hybrid fashion, creating objects using the GUI, saving the file, and then re-saving your Coldtype source file for automatic updates in Blender itself. (It also means you might need to periodically delete some things that Coldtype creates in Blender, if you no longer want those things to exist and the Coldtype program no longer references them.)

Here’s a short video demonstrating what’s being described above (all of which is now part of the released public Coldtype (despite what the tweet says)):


Caveats

If you’ve attempted to install Coldtype into Blender but are seeing an error about Python.h, you’ll need to download the Python 3.9.2 source tarball, unzip it, then manually copy the files in the python Include folder into the blender embedded python /Applications/Blender.app/Contents/Resources/2.93/python/include/python3.9/ folder. (This requires Blender 2.93 or 2.93.2)