r/flipflip • u/NopeNotMeNo • Jan 28 '21
Community Stuff Spiral text python script
I've been playing around with the new caption scripting features (they're amazing btw) and wanted to create an effect of text spiraling into the center, so I've written a little python script I thought I'd share.
The effect: https://files.catbox.moe/q9d6t0.mp4
The script can be called like python spiral.py <path to file of phrases> with optional parameters to set the scale, number of turns and direction of the spiral. The flipflip caption code to generate it is then copied to your clipboard.
obligatory feel free to use anywhere and credit me if you like etc etc.
Code:
import sysimport numpy as npimport math# import matplotlib.pyplot as pltimport pyperclipdef normalize(arr):std = (arr - min(arr))/(max(arr) - min(arr))return (std - 0.5) * 2# constants of the spiral equation, from wikipedia:# Changing the parameter a moves the centerpoint of the spiral outward from the origin (positive a toward θ = 0 and negative a toward θ = π), while b controls the distance between loops.a = 0b = -2.4# first param is a file containing the phrases (one per line)f = open(sys.argv[1])words = f.readlines()# second param is optional scale factorscale = float(sys.argv[2]) if len(sys.argv) >= 3 else 75# third param is optional number of turns of spiralturns = float(sys.argv[3]) if len(sys.argv) >= 4 else 2# fourth param is optional, toggle direction of spiralreverse = (len(sys.argv) >= 5)# make a linspace with values for each wordT = np.linspace(0, 2 * turns * math.pi, len(words))# calculate x and y positions of each wordX = [(a + b * t) * math.cos(t) for t in T]Y = [(a + b * t) * math.sin(t) for t in T]# scaleX = [int(x) for x in normalize(X) * scale]Y = [int(y) for y in normalize(Y) * scale]# default is spiral inwardsif not reverse:X = reversed(X)Y = reversed(Y)# print(list(zip(X,Y)))# plt.plot(X,Y)# plt.show()# convert to flipflip caption formatoutput = "\n".join([f"setBlinkX {x}\nsetBlinkY {y}\nblink {word}\n" for x,y,word in zip(X,Y, words)])print(output)# dump to clipboardpyperclip.copy(output)
2
u/GoddessServant0 The Dev Jan 29 '21
This is awesome! Great work!