Files
2026-11-17 20:19:55 +03:00

302 lines
18 KiB
Python

# Clean pixel-design ornaments (style of ornament_showcase.py) -> Blockbench .bbmodel
# Each ornament = template shape (digits = colour slots, % = candy stripes) + overlays (details, raised for relief).
# Usage: python gen_designs.py <out_dir>
import sys, os, math, re
from collections import deque
from gen_ornaments import Model, sh, hx, cap_hook, png
PAL = {k: hx(v) for k, v in dict(
R='e02a2a', r='a01424', W='f4f4f8', w='cfe2ff', G='ffc21a', g='c98400', N='2fb53a', n='177a28',
B='2a6cf0', b='1a2a7a', L='80d4ff', C='45f0ff', P='9040f0', p='4a1aa0', M='ff50d8', K='1e1e2a',
o='8a4a1e', d='5a2e10', O='ff8a1e', S='ffd2a8', Y='fff25a', T='c8844a', s='ff9ad0', E='ff2a4a',
V='c0c8dc', v='8890a8', Q='20c0b0').items()}
ACC = set('GCEYM') # gold / gems / glow pop out
def _circ(n=15, r=7.3, f=lambda d: '1'):
c = (n - 1) / 2
return [''.join(f(math.hypot(x - c, y - c)) if math.hypot(x - c, y - c) <= r else '.' for x in range(n)) for y in range(n)]
def _flake(n=15):
c = (n - 1) / 2; segs = []
for k in range(6):
a = math.pi / 2 + k * math.pi / 3; ex, ey = 7 * math.cos(a), 7 * math.sin(a); segs.append((0, 0, ex, ey))
for t, ln in ((3.4, 2.4), (5.4, 1.6)):
bx, by = t * math.cos(a), t * math.sin(a)
for s in (-1, 1):
b = a + s * math.pi / 3; segs.append((bx, by, bx + ln * math.cos(b), by + ln * math.sin(b)))
def sd(px, py, ax, ay, bx, by):
dx, dy = bx - ax, by - ay; t = max(0, min(1, ((px - ax) * dx + (py - ay) * dy) / (dx * dx + dy * dy)))
return math.hypot(px - ax - t * dx, py - ay - t * dy)
out = []
for y in range(n):
row = ''
for x in range(n):
px, py = x - c, c - y
row += '2' if math.hypot(px, py) <= 1.5 else ('1' if min(sd(px, py, *s) for s in segs) <= 0.62 else '.')
out.append(row)
return out
def _star4(n=15):
c = (n - 1) / 2
return [''.join('1' if (abs(x - c) <= 1 and abs(y - c) <= 7) or (abs(y - c) <= 1 and abs(x - c) <= 7)
or abs(x - c) + abs(y - c) <= 4.5 else '.' for x in range(n)) for y in range(n)]
T = lambda s: s.strip().split()
TPL = {
'BALL': _circ(), 'RING': _circ(f=lambda d: '1' if d > 3.4 else '.'),
'DONUT': _circ(f=lambda d: '2' if d > 6.3 else ('1' if d > 2.6 else '.')),
'MEDAL': _circ(f=lambda d: '1' if d > 6.0 else '2'), 'FLAKE': _flake(), 'STAR4': _star4(),
'GIFT': T('''..222...222.. .22.22.22.22. ..222222222.. 1111112111111 1111112111111 1111112111111
2222222222222 1111112111111 1111112111111 1111112111111 1111112111111'''),
'BELL': T('''.....111..... ....11111.... ...1111111... ...1111111... ..111111111.. ..111111111..
..111111111.. .11111111111. 1111111111111 2222222222222 .....333..... ......3......'''),
'STAR': T('''.......1....... ......111...... ......111...... .....11111..... 111111111111111
.1111111111111. ..11111111111.. ...111111111... ....1111111.... ....1111111.... ...111111111...
...1111.1111... ..1111...1111.. ..11.......11..'''),
'HEART': T('''..111.....111.. .11111...11111. 111111111111111 111111111111111 111111111111111
.1111111111111. ..11111111111.. ...111111111... ....1111111.... .....11111..... ......111......
.......1.......'''),
'GLOBE': T('''....LLLLLLL.... ..LL1111111LL.. .L11111111111L. .L11111111111L. L1111111111111L
L1111111111111L L1111111111111L L1111111111111L .L11111111111L. .LWWWWWWWWWWWL. ..LLWWWWWWWLL..
..22222222222.. .2333333333332. .2222222222222.'''),
'STOCK': T('''111111111... 111111111... .2222222.... .2222222.... .2222222.... .2222222....
.22222222... .222222222.. .2222222222. .22222223333 ..222223333. ...222.333..'''),
'MITTEN': T('''...22222.... ..2222222... .222222222.. .222222222.22 .22222222222. .22222222222.
.2222222222. .222222222.. .222222222.. ..1111111... ..1111111...'''),
'BOW': T('''111.......111 11111...11111 1111112111111 1111122211111 1111112111111 11111.1.11111
....11.11.... ...11...11... ..11.....11.. ..1.......1..'''),
'TREE': T('''......1...... .....111..... ....11111.... ...1111111... .....111..... ....11111....
...1111111... ..111111111.. ....11111.... ...1111111... ..111111111.. .11111111111.
1111111111111 .....222..... .....222.....'''),
'CANE': T('''..%%%%%... .%%%%%%%.. %%.....%%. %%.....%%. %%.....%%. .......%%. .......%%.
.......%%. .......%%. .......%%. .......%%. .......%%. .......%%. .......%%.'''),
'CANEX': T('''.%%%.....%%%. %%.%%...%%.%% %...%...%...% ....%%.%%.... .....%%%..... ....%%.%%....
...%%...%%... ..%%.....%%.. .%%.......%%. %%.........%%'''),
'DROP': T('''.....1..... ....111.... ....111.... ...11111... ...11111... ..1111111.. ..1111111..
.111111111. .111111111. 11111111111 11111111111 11111111111 .111111111. ..1111111.. ...11111...'''),
'LANTERN': T('''...33333... ..3333333.. .333333333. .221111122. .221111122. .221111122. .221111122.
.221111122. .221111122. .222222222. ..2222222.. ...22222...'''),
'HOUSE': T('''.......3....... ......333...... .....33333..... ....3333333.... ...333333333...
..33333333333.. .3333333333333. 333333333333333 ..11111111111.. ..1YY11111YY1.. ..1YY11111YY1..
..11111221111.. ..11111221111.. ..11111221111..'''),
'SNOWMAN': T('''....11111.... ....11111.... ..111111111.. ....WWWWW.... ...WKWWWKW... ...WWWOWWW...
...WWWWWWW... ..222222222.. ...WWWW22W... ..WWWWKWWWW.. .WWWWWWWWWWW. .WWWWWKWWWWW.
.WWWWWWWWWWW. ..WWWWKWWWW.. ...WWWWWWW...'''),
'PENGUIN': T('''....KKKKK.... ...KKKKKKK... ..KKWWWWWKK.. ..KWKWWWKWK.. ..KWWWOWWWK.. ..KWWOOOWWK..
.KK1111111KK. .KKWWWWWWWKK. KKKWWWWWWWKKK KK.WWWWWWW.KK ...WWWWWWW... ...KWWWWWK... ...OOO.OOO...'''),
'PERSON': T('''.....111..... ....11111.... ...1111111... ..222222222.. ...SSSSSSS... ...SKSSSKS...
...SSSSSSS... ...SSsSsSS... .33333333333. S33333333333S S33333333333S .KKKKKGKKKKK.
..333333333.. ..444...444.. ..444...444.. .5555...5555.'''),
'ANGEL': T('''.....YYYYY..... ......GGG...... WW...GSSSG...WW WWW..GSSSG..WWW WWWW..SSS..WWWW
WWWWW11111WWWWW .WWWW11111WWWW. ..WW1111111WW.. ....1111111.... ...111111111... ...111111111...
..11111111111.. ..11111111111.. .1111111111111.'''),
'REIN': T('''..3...3......... ..33.33......... ...333.......... ..1111.......... .11K11..........
611111.......... ..1111.......... ..44444......... ...41111111111.. ...412211111112.
...41111111111.. ....11222222111. ....11.1..1.11.. ....55.5..5.55..'''),
'CASTLE': T('''1.1....1....1.1 111...111...111 111..11111..111 121..12121..121 111111111111111
111112111211111 111111111111111 111111333111111 111113333311111 111113333311111 444444444444444'''),
'TRAIN': T('''..33............ ..33.....2222222 .1111....2YY2YY2 .1111....2YY2YY2 1111111112222222
1111111112222222 1111111112222222 4444444444444444 .KK..KK....KKK.. .KK..KK....KKK..'''),
'SLEIGH': T('''.........33.44.. .........33.44.. 11.....11111111. 111...111111111. .111111111111111
..11111111111111 ...111111111111. ....2...2...2... 2..222222222222. .2222222222222..'''),
'MUG': T('''..3.33.3...... .333333333.... .111111111.... .11111111111.. .1111111111.1. .1111111111.1.
.1111111111.1. .11111111111.. .111111111.... ..1111111.....'''),
'CANDLE': T('''.....Y..... ....YOY.... ....YOY.... .....K..... ...11111... ...11111... ..1111111..
...11111... ...11111... ...11111... ...11111... ...11111... ..2222222.. .222222222.'''),
'PINECONE': T('''....111.... ...12121... ..1212121.. .121212121. .212121212. 12121212121 21212121212
12121212121 .212121212. .121212121. ..2121212.. ...12121... ....212.... .....1.....'''),
'ROSE': T('''...1111111... ..111222111.. .11122122111. .11221112211. .11212221211. .11122222111.
..111111111.. ...1111111... ...33.3.33... ..333.3.333.. .3333.3.3333. ......3...... ......3......'''),
'OWL': T('''.11.......11. .11111111111. 11WWWW1WWWW11 1WWKCW1WCKWW1 11WWWWOWWWW11 .1111OOO1111.
.22222222222. 1111111111111 111WWWWWWW111 111WWWWWWW111 111WWWWWWW111 .11WWWWWWW11.
..111111111.. ...OO...OO...'''),
'FACE': T('''3..3.....3..3 33.33...33.33 .3333...3333. ...33...33... ...1111111... ..111111111..
.11K11111K11. .11111111111. ..112222211.. ..122222221.. ...222E222... ....22222.... ...4444444...'''),
'BEAR': T('''.11.......11. .11111111111. ..111111111.. ..1K11111K1.. ..112222211.. ..1122K2211..
...1122211... .11111111111. 1111111111111 11.1111111.11 ...1111111... ..111...111.. .2222...2222.'''),
'CROWN': T('''1.....1.....1 11...111...11 111.11111.111 1111111111111 1E11C11E11C11 1111111111111
2222222222222'''),
'EGG': T('''....11111.... ...1111111... ..111111111.. ..111111111.. .11111111111. .11111111111.
1111111111111 1111111111111 1111111111111 1111111111111 1111111111111 .11111111111.
.11111111111. ..111111111.. ...1111111...'''),
'MOON': T('''....1111.... ..11111..... .1111....... .111........ 1111........ 111......... 111.........
1111.......1 11111.....11 .1111111111. ..11111111.. ....1111....'''),
'CHEST': T('''..1111111111.. .111111111111. 11111111111111 22222222222222 111111GGG11111 222222GKG22222
111111GGG11111 11111111111111 11111111111111 22222222222222'''),
'SACK': T('''.....2.2..... ....22222.... .....222..... ....11111.... ...1111111... ..111111111..
.11111111111. 1111111111111 1111111111111 1111111111111 1111111111111 .11111111111. ..111111111..'''),
'CUP': T('''1111111111111 1.111111111.1 1.111111111.1 .11111111111. ...1111111... ....11111....
.....111..... .....111..... ....11111.... ...2222222... ..222222222..'''),
'FISH': T('''.....111....... ...1111111...11 ..111111111.111 .1K111111111111 .11111111111111
..111111111.111 ...1111111...11 .....111.......'''),
'SKATE': T('''..11111...... ..11111...... ..11111...... ..111111..... ..11111111... ..111111111..
..1111111111. ..WWWWWWWWWW. ...2....2.... ..2222222222.'''),
'FIRE': T('''..Y...Y...Y.... ..W...W...W.... 222222222222222 111111111111111 11KKKKKKKKKKK11
11KKKKKKKKKKK11 11KKKKOYOKKKK11 11KKKOYYYOKKK11 11KKOYYYYYOKK11 11KK2222222KK11 222222222222222'''),
'DRUM': T('''.WWWWWWWWWWW. WWWWWWWWWWWWW 2222222222222 G11111G11111G 1G111G1G111G1 11G1G111G1G11
111G11111G111 1111111111111 2222222222222'''),
'HAT': T('''...........WW .......111.WW ......11111.. .....111111.. ....1111111.. ...11111111..
..1111111111. .11111111111. WWWWWWWWWWWWW WWWWWWWWWWWWW'''),
'ELFHAT': T('''............G ..........11. ........1111. ......11111.. .....111111.. ....1111111..
...11111111.. ..1111111111. 2222222222222 2G22G22G22G22'''),
'HOLLY': T('''.11.......11. 1111.....1111 11111...11111 .11112E21111. ..111EEE111.. ...1EEEEE1...
....EE.EE....'''),
'GINGER': T('''.....111..... ....11111.... ....1K1K1.... ....11111.... ....1WWW1.... ..111111111..
.11111E11111. 111111W111111 11.111E111.11 ...1111111... ...1111111... ..111...111.. ..111...111..
.1111...1111.'''),
'CARROT': T('''...N.N.N... ..NNNNNNN.. ...NNNNN... ...11111... ..1111111.. ..1111111.. ...11111...
...11111... ....111.... ....111.... .....1.....'''),
'SHARD': T('''.....1..... ....111.... ....121.... ...11211... ...11211... ..1112111.. ..1112111..
.111121111. .111121111. .111121111. ..1112111.. ..1112111.. ...11211... ....121.... .....1.....'''),
'BRANCH': T('''...........oo ..1.1.1..oo.. ...11111oo1.. ..1111ooo1111 .1111oo11111. 111oo1111.1..
.1oo1111..... oo..11.1.....'''),
'DRAGON': T('''..11............ .1111........... 11K11...2....... .11111..22...... ...111.2222.....
...1111222222... ....11111222222. ....111111.2222. ...11111111..... ..111111111..... ..11.1111.11....
..11..11...111.. .111..111....11.'''),
'WOLF': T('''.1.1........... .111........... 11K1........... 11111.........1 ..1111......111
..2222111111111 ...11111111111. ...11111111111. ...11......11.. ...11......11.. ..111.....111..'''),
'YETI': T('''...1111111... ..111111111.. .11222222211. .12K22222K21. .12222222221. .11222KK2211.
1111111111111 1111111111111 11.1111111.11 11.1111111.11 ...1111111... ..1111.1111.. ..222...222..'''),
'SPIRIT': T('''.....111..... ....11111.... ...1111111... ..112222211.. ..12C222C21.. ..122222221..
.11122222111. 1111111111111 1111112111111 .11111111111. ..111111111.. ...1111111... ....11111....
.....111..... ......1......'''),
'BIRD': T('''.......1....... ......111...... ......1G1...... 11...11111...11 111.1111111.111
111111111111111 .1111111111111. ..11111111111.. ...111111111... ....1111111.... .....11111.....
....1.111.1.... ...1..1.1..1... ......1.1......'''),
'COMET': T('''............1.. ...........111. ..........11111 ...........111. ..........2.1.. .........22....
........222.... .......3222.... ......3322..... .....3332...... ....333........ ...333.........
..33........... .3.............'''),
}
def O(s, ox=None, oy=None): return (s, ox, oy)
def grid_of(tpl, subs, ovs):
if subs.startswith('%'): subs = subs[1:]; rows = [r.replace('1', '%') for r in TPL[tpl]]
else: rows = TPL[tpl]
g = {}
for y, row in enumerate(rows):
for x, ch in enumerate(row):
if ch == '.': continue
if ch == '%': ch = subs[0] if ((x - y) // 2) % 2 == 0 else subs[1]
elif ch.isdigit(): ch = subs[int(ch) - 1]
g[(x, y)] = (ch, 0)
H, W = len(rows), max(map(len, rows))
global HOOKX
HOOKX = None
for k, (s, ox, oy) in enumerate(ovs):
if s == '@': HOOKX = ox; continue # explicit hang column
free = s.startswith('+'); s = s.lstrip('+'); R = s.split('/')
ox = (W - max(map(len, R))) // 2 if ox is None else ox
oy = (H - len(R)) // 2 if oy is None else oy
put = cut = 0
for y, row in enumerate(R):
for x, ch in enumerate(row):
p = (x + ox, y + oy)
if ch == '.': continue
if not free and p not in g: cut += 1; continue
put += 1
if ch == '_': g.pop(p, None)
else: g[p] = (ch, 1)
if cut > max(1, (put + cut) * 0.25): WARN.append(f'overlay#{k} clipped {cut}/{put + cut}')
return g
WARN, MARK, HOOKX = [], [], None
def comps(S):
seen, out = set(), []
for p in S:
if p in seen: continue
c, q = [p], deque([p]); seen.add(p)
while q:
a = q.popleft()
for dx, dy in N4:
n = (a[0] + dx, a[1] + dy)
if n in S and n not in seen: seen.add(n); c.append(n); q.append(n)
out.append(c)
return sorted(out, key=len, reverse=True)
def fix_floating(g):
"""Glue diagonal-only pieces to the body with a bridge pixel; drop pieces that float free."""
while True:
cs = comps(set(g))
if len(cs) == 1: return
main, done = set(cs[0]), False
for c in cs[1:]:
for (x, y) in c:
for dx, dy in ((1, 1), (1, -1), (-1, 1), (-1, -1)):
if (x + dx, y + dy) in main:
g[(x + dx, y)] = g[(x, y)]; done = True; break
if done: break
if done: break
WARN.append(f'dropped floating {len(c)}px at {c[0]}')
for p in c: del g[p]
done = True; break
N4 = ((1, 0), (-1, 0), (0, 1), (0, -1))
def build(g, name, fn):
m = Model(); S = set(g)
dist, q = {}, deque()
for p in S:
if any((p[0] + a, p[1] + b) not in S for a, b in N4): dist[p] = 0; q.append(p)
while q:
p = q.popleft()
for a, b in N4:
n = (p[0] + a, p[1] + b)
if n in S and n not in dist: dist[n] = dist[p] + 1; q.append(n)
ys = [y for _, y in g]; top, ymax = min(ys), max(ys)
xs = [x for x, _ in g]; span = max(max(xs) - min(xs), ymax - top) + 1; my = (top + ymax) / 2
# hang point: the column nearest the shape's centre of mass that has the highest surface there
gx = sum(xs) / len(xs)
cols = {}
for x, y in g: cols[x] = min(cols.get(x, 99), y)
cx = HOOKX if HOOKX in cols else min(cols, key=lambda x: (abs(x - gx) > 1.5, cols[x], abs(x - gx)))
MARK[:] = [(cx, cols[cx])]
ctop = cols[cx]
if ctop - top > 3: WARN.append(f'hook sits low (col top {ctop} vs shape top {top})')
tx = [x for x in (cx - 1, cx, cx + 1) if cols.get(x) == ctop]
def look(p):
ch, raised = g[p]
h = 1 + 0.5 * min(dist[p], 2) + 0.5 * raised + (0.5 if ch in ACC else 0)
t = ((cx - p[0]) + (my - p[1])) / span
c = sh(PAL[ch], 1 + 0.35 * t + (0.04 if (p[0] + p[1]) % 2 else -0.04))
return tuple(round(v / 8) * 8 for v in c), h
for y in range(top, ymax + 1):
row = sorted(x for x, yy in g if yy == y); i = 0
while i < len(row):
c, h = look((row[i], y)); j = i + 1
while j < len(row) and row[j] == row[j - 1] + 1 and look((row[j], y)) == (c, h): j += 1
m.box(row[i] - cx, ymax - y, -h, row[j - 1] + 1 - cx, ymax - y + 1, h, c, 'px')
i = j
cap_hook(m, ymax - ctop + 1, 4 if len(tx) >= 3 else 2)
m.save(fn, name)
return len(m.els)
def rarity(n): return 'common' if n <= 50 else 'uncommon' if n <= 100 else 'rare' if n <= 150 else 'epic' if n <= 200 else 'legendary'
if __name__ == '__main__':
from designs import D
out = sys.argv[1]; thumbs = []
rs = '|'.join({rarity(n) for n in D})
for f in [os.path.join(dp, f) for dp, _, fs in os.walk(out) for f in fs]:
if re.search(r'\d{3}_(%s)\.bbmodel$' % rs, f): os.remove(f) # old auto-traced of redone rarities
for num in sorted(D):
name, tpl, subs, *ovs = D[num]
WARN.clear()
g = grid_of(tpl, subs, ovs); fix_floating(g); d = os.path.join(out, rarity(num)); os.makedirs(d, exist_ok=True)
nm = f'{num:03d}_{name}'; n = build(g, nm, os.path.join(d, nm + '.bbmodel'))
hx_, ht = MARK[0]; g = dict(g)
for k in (1, 2, 3): g[(hx_, ht - k)] = ('g' if k < 3 else 'G', 0) # hook marker in preview
thumbs.append(g)
if WARN: print(nm, '|', '; '.join(WARN))
TW, TH, Sc, PR = 22, 22, 3, 16
rows = (len(thumbs) + PR - 1) // PR; Wp, Hp = PR * TW * Sc, rows * TH * Sc; img = [(40, 20, 90)] * (Wp * Hp)
for i, g in enumerate(thumbs):
mx, my = min(x for x, _ in g), min(y for _, y in g)
for (x, y), (ch, r) in g.items():
X, Y = x - mx + 1, y - my + 1
if X < TW and Y < TH:
for a in range(Sc):
for b in range(Sc): img[((i // PR) * TH * Sc + Y * Sc + a) * Wp + (i % PR) * TW * Sc + X * Sc + b] = PAL[ch]
open(os.path.join(out, 'preview.png'), 'wb').write(png(img, Wp, Hp))