Skip to content

Instantly share code, notes, and snippets.

@arrowtype
Last active July 28, 2022 05:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arrowtype/b01187cac84a8c2a8bdb9c670e6dc4a6 to your computer and use it in GitHub Desktop.
Save arrowtype/b01187cac84a8c2a8bdb9c670e6dc4a6 to your computer and use it in GitHub Desktop.
This is a set of Python I use many times I wish to make an animation or multipage doc with Drawbot, but coded in my preferred editor (currently, VS Code)

Using Drawbot in an external editor

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 (currently, VS Code) rather than in the Drawbot app.

Usage

First, install DrawBot as a module:

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

Adapt as needed. Run in the command line with:

python3 <path>/remote-drawbot-script-template.py

Font

This uses Recursive VF 1.031, available as an OFL on GitHub (permalink).

"""
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 (currently, VS Code) rather than in the Drawbot app.
USAGE:
First, install DrawBot as a module:
pip install git+https://github.com/typemytype/drawbot
Adapt script as needed, then run from the command line with:
python3 <path>/remote-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
fps = 3
frameRate = 1/fps # only applicable to mp4 and gif; can be buggy
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