trench/tools/reorganize_textures.py

82 lines
2.3 KiB
Python
Raw Permalink Normal View History

2026-04-22 10:19:57 -04:00
#!/usr/bin/env python3
"""Reorganize textures/td_sliced/ into textures/tb/<family>/<variant>.png
Already-lowercase precondition: run lowercase_textures.py first.
For each <family>_<variant>.png in the source folder, moves:
- the .png itself
- the .png.import sibling (with source_file= rewritten)
- the .tres sibling (if any) with its path= field rewritten
Files that don't match the family_variant pattern are left in place.
"""
import re
from pathlib import Path
ROOT = Path.home() / "godot_projects/trench/textures"
SRC = ROOT / "td_sliced"
DST_BASE = ROOT / "tb"
# family_variant.png OR family_variant_tag.png
# variant must start with a digit (so we don't accidentally split a multi-word
# family name on its first underscore)
NAME_RE = re.compile(r"^([a-z]+)_(\d+[a-z](?:_[a-z]+)?)\.png$")
moved_groups = 0
moved_files = 0
skipped = []
png_files = sorted(SRC.glob("*.png"))
for png in png_files:
m = NAME_RE.match(png.name)
if not m:
skipped.append(png.name)
continue
family, variant = m.group(1), m.group(2)
new_dir = DST_BASE / family
new_dir.mkdir(parents=True, exist_ok=True)
new_png = new_dir / f"{variant}.png"
new_imp = new_dir / f"{variant}.png.import"
new_tres = new_dir / f"{variant}.tres"
if new_png.exists():
print(f"COLLISION skip: {png.name} -> {new_png}")
continue
# Move .png
png.rename(new_png)
# Move + rewrite .png.import
old_imp = png.with_suffix(".png.import")
if old_imp.exists():
text = old_imp.read_text()
text = text.replace(
f"res://textures/td_sliced/{png.name}",
f"res://textures/tb/{family}/{variant}.png",
)
old_imp.unlink()
new_imp.write_text(text)
# Move + rewrite sibling .tres material if it exists
old_tres = png.with_suffix(".tres")
if old_tres.exists():
text = old_tres.read_text()
text = text.replace(
f"res://textures/td_sliced/{png.name}",
f"res://textures/tb/{family}/{variant}.png",
)
old_tres.unlink()
new_tres.write_text(text)
moved_groups += 1
moved_files += 1 + int(old_imp.exists() is False) * 0 # always counted via group
print(f"\nmoved {moved_groups} groups")
print(f"skipped (non-matching) {len(skipped)} files:")
for s in skipped:
print(f" {s}")