sketch-a-day/sketch_180120a/dbn_generate_poly.py

86 wiersze
3.4 KiB
Python
Czysty Zwykły widok Historia

2018-01-20 01:48:03 +00:00
"""
2018-01-21 01:37:23 +00:00
s18020a - Alexandre B A Villares
2018-01-20 01:48:03 +00:00
https://abav.lugaralgum.com/sketch-a-day
This script generates code on console for dbn_letters.py
Converting some of Maeda's Design by Number
dbnletters.dbn code -> Processing
"""
2018-01-21 02:05:31 +00:00
2018-01-20 01:48:03 +00:00
def convert_dbn_source(file_path):
2018-01-20 20:11:57 +00:00
with open("dbn_polys.py", 'w') as out:
out.write('"""\n')
out.write("s18019 - Alexandre B A Villares\n")
out.write("https://abav.lugaralgum.com/sketch-a-day\n")
out.write("This code was generated by dbn_generata_poly.py\n")
out.write("Converting some of Maeda's Design by Number\n")
out.write('dbnletters.dbn code -> Processing\n"""\n')
out.write("dbn_letter = {} # Dict of functions\n")
out.write("\n")
2018-01-20 01:48:03 +00:00
with open(file_path, "r") as f:
dbn_source = f.readlines()
inside_block = False
command_name = ""
command_block = []
for ln in dbn_source:
if ln.count("command"):
command_name = ln[14:15]
elif ln.count("{"):
inside_block = True
elif ln.count("}"):
if command_name in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
def_dbn_letter(command_block, command_name)
command_block = [] # empty block
inside_block = False
elif inside_block:
command_block.append(ln.lstrip())
2018-01-20 20:11:57 +00:00
def def_dbn_letter(dbn_block, key_):
2018-01-20 01:48:03 +00:00
p_block = []
2018-01-20 20:11:57 +00:00
2018-01-20 01:48:03 +00:00
for dbn_line in dbn_block:
if dbn_line:
2018-01-21 01:37:23 +00:00
p_lines =(" " + dbn_line # all this to convert lines() to shapes
.replace("(", "")
.replace(")", "")
2018-01-20 01:48:03 +00:00
.replace("line ", "vertex(")
.replace(" ", ",", 1)
2018-01-21 01:37:23 +00:00
.replace(" ", "$", 1) # token to split line into 2 vertices
2018-01-20 01:48:03 +00:00
.replace(" ", ",")
.replace("$", ")\n vertex(", 1)
.replace("//", "#")
.strip()
+ ")")
2018-01-21 01:37:23 +00:00
p_block.append(p_lines.split("\n")[0])
2018-01-20 21:25:19 +00:00
p_block.append(p_lines.split("\n")[1])
2018-01-21 01:37:23 +00:00
# for ln in p_block:
# print ln.replace(" ","-")
2018-01-20 21:25:19 +00:00
2018-01-20 20:11:57 +00:00
with open("dbn_polys.py", 'a') as out:
out.write("# " + key_ + "\n")
2018-01-21 02:05:31 +00:00
out.write("def dbn_letter" + key_ + "(h, v, debug_poly=False):\n")
2018-01-20 20:11:57 +00:00
out.write(" pushMatrix()\n")
out.write(" scale(1, -1)\n")
2018-01-21 02:05:31 +00:00
out.write(" if debug_poly: stroke(random(256),200, 200)\n") # for debug
2018-01-20 20:11:57 +00:00
out.write(" beginShape()\n")
2018-01-21 01:37:23 +00:00
v_count = 0
for i, line_ in enumerate(p_block):
2018-01-21 02:05:31 +00:00
if line_ != p_block[i-1]: # and line_ != p_block[i-2]: # if repeated
2018-01-20 21:25:19 +00:00
out.write(line_ + "\n")
2018-01-21 01:37:23 +00:00
v_count += 1
else: out.write(" # " + line_.lstrip() + "\n")
if i % 2 and i < len(p_block)-2: # if on odd lines, next doesn't repeat
if line_ != p_block[i+1]:
#out.write(" #---\n")
out.write(" endShape()\n")
2018-01-21 02:05:31 +00:00
out.write(" if debug_poly: stroke(random(256),200, 200)\n") # for debug
2018-01-21 01:37:23 +00:00
out.write(" beginShape()\n")
2018-01-20 20:11:57 +00:00
out.write(" endShape()\n")
out.write(" popMatrix()\n")
out.write("dbn_letter['" + key_ + "'] = dbn_letter" + key_ + "\n")
out.write("dbn_letter[" + str(ord(key_) - 64)
+ "] = dbn_letter" + key_ + "\n")