-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebp_to_godot.py
More file actions
154 lines (110 loc) · 3.54 KB
/
webp_to_godot.py
File metadata and controls
154 lines (110 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import math
import re
from pathlib import Path
from collections import defaultdict
from PIL import Image, ImageSequence
# ----------------------------
# Setup
# ----------------------------
input_folder = ".\\inputs\\"
output_folder = "characters/" + input("Output folder: ").strip() or "output"
INPUT_DIR = Path(input_folder)
OUTPUT_DIR = Path(output_folder)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
# ----------------------------
# Naming: (a)idle -> idle_a
# ----------------------------
def split_name(stem: str):
m = re.match(r"^\((a|b)\)(.*)$", stem, re.IGNORECASE)
if m:
tag = m.group(1).lower()
base = m.group(2)
else:
tag = "pre"
base = stem
return clean(base), tag
def clean(name: str):
name = re.sub(r"[^\w]+", "_", name)
return re.sub(r"_+", "_", name).strip("_") or "anim"
# ----------------------------
# WebP extraction (preserve timing)
# ----------------------------
def extract_frames(path: Path):
img = Image.open(path)
frames = []
durations = []
for frame in ImageSequence.Iterator(img):
frames.append(frame.copy().convert("RGBA"))
durations.append(frame.info.get("duration", img.info.get("duration", 100)) / 1000.0)
return frames, durations
# ----------------------------
# Sprite sheet layout
# ----------------------------
def auto_grid(n):
cols = math.ceil(math.sqrt(n))
rows = math.ceil(n / cols)
return cols, rows
def build_sheet(frames):
cols, rows = auto_grid(len(frames))
w, h = frames[0].size
sheet = Image.new("RGBA", (cols * w, rows * h))
for i, f in enumerate(frames):
sheet.paste(f, ((i % cols) * w, (i // cols) * h))
return sheet, cols, rows
# ----------------------------
# Animation .tres (Sprite2D only)
# ----------------------------
def write_animation(name, texture_path, cols, rows, durations):
times = []
values = []
t = 0.0
for i, d in enumerate(durations):
times.append(round(t, 6))
values.append(i)
t += d
content = f'''[gd_resource type="Animation" format=3]
[ext_resource type="Texture2D" path="res://{texture_path.as_posix()}" id="1"]
[resource]
resource_name = "{name}"
length = {round(sum(durations), 6)}
loop_mode = 1
tracks/0/type = "value"
tracks/0/path = NodePath("Sprite2D:texture")
tracks/0/keys = {{"times":[0.0],"values":[ExtResource("1")]}}
tracks/1/type = "value"
tracks/1/path = NodePath("Sprite2D:hframes")
tracks/1/keys = {{"times":[0.0],"values":[{cols}]}}
tracks/2/type = "value"
tracks/2/path = NodePath("Sprite2D:vframes")
tracks/2/keys = {{"times":[0.0],"values":[{rows}]}}
tracks/3/type = "value"
tracks/3/path = NodePath("Sprite2D:frame")
tracks/3/keys = {{
"times": {times},
"values": {values},
"update": 1
}}
'''
out = OUTPUT_DIR / f"{name}.tres"
out.write_text(content, encoding="utf-8")
# ----------------------------
# Main
# ----------------------------
groups = defaultdict(list)
for f in INPUT_DIR.iterdir():
if f.suffix.lower() != ".webp":
continue
base, tag = split_name(f.stem)
groups[clean(base)].append((tag, f))
for base, items in groups.items():
print(f"\nProcessing: {base}")
for tag, file in items:
name = f"{base}_{tag}"
frames, durations = extract_frames(file)
if not frames:
continue
sheet, cols, rows = build_sheet(frames)
png_path = OUTPUT_DIR / f"{name}.png"
sheet.save(png_path)
write_animation(name, png_path, cols, rows, durations)
print(f" {name}: {len(frames)} frames ({cols}x{rows})")