Skip to content

Instantly share code, notes, and snippets.

@bryanrieger
Created June 23, 2025 13:51
Show Gist options
  • Select an option

  • Save bryanrieger/1e103686b6ac651eec84f9dca6852cee to your computer and use it in GitHub Desktop.

Select an option

Save bryanrieger/1e103686b6ac651eec84f9dca6852cee to your computer and use it in GitHub Desktop.
Install Drawbot from GitHub

Install Drawbot from GitHub

This is a set of Python I use many times I wish to make an animation or multipage doc with Drawbot, but code in my preferred editor (ie: Nova) rather than in the Drawbot app.

First, install DrawBot as a module:

pip install git+https://github.com/typemytype/drawbot

Adapt script (below) as needed, then run from the command line with:

python <path>/drawbot-script-template.py

from drawBot import * # requires drawbot to be installed as module
import sys
import os

newDrawing() # required by drawbot module

# currentDir = sys.argv[0]
currentDir = os.path.dirname(os.path.abspath(__file__))
print(currentDir)

# ---------------------------------------------------------
# CONFIGURATION -------------------------------------------

docTitle = "drawbot-export" # update this for your output file name
save = True
outputDir = "exports"
autoOpen = True

fontFam = f"{currentDir}/Recursive_VF_1.031.ttf" # Update as needed. Easiest when font file is in same directory.

frames = 10
frameRate = 1/60 # only applicable to mp4
fileFormat = "pdf" # pdf, gif, or mp4

pageSize = 3.5 # inches
DPI = 72 # dots per inch

paddingInPts = 18

# ----------------------------------------------
# Helper functions

pixels = DPI*pageSize # do not edit
W, H = pixels, pixels # do not edit
padding = DPI*paddingInPts/72 # do not edit

# turn font size into usable value for given pageSize
def computeFontSizePoints(pts):
	return W * (pts / (pageSize * 72))

# a frequently-useful function
def interpolate(a, b, t):
	return(a + (b-a) * t)

# ----------------------------------------------
# THE ACTUAL ANIMATION

for frame in range(frames):
	newPage(W, H) # required for each new page/frame

	t = frame / frames
	x = interpolate(0, W*0.9, t)
	
	font(fontFam, computeFontSizePoints(24)) # set a font and font size
	# draw text
	text("hi", (x, H/2))

endDrawing() # advised by drawbot docs

if save:
	import datetime

	now = datetime.datetime.now().strftime("%Y_%m_%d-%H_%M") # -%H_%M_%S

	if not os.path.exists(f"{currentDir}/{outputDir}"):
		os.makedirs(f"{currentDir}/{outputDir}")

	path = f"{currentDir}/{outputDir}/{docTitle}-{now}.{fileFormat}"

	print("saved to ", path)

	saveImage(path)

	if autoOpen:
		os.system(f"open --background -a Preview {path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment