Fix Interchange export and extract styling

This commit is contained in:
lucio committed 2026-08-09 01:24:21 -07:00
1 parent 7c9e0ba5e1
commit b079009d74
6 files changed
+237 -19

No files matched your search

+76 -1
View File
@@ -29,6 +29,7 @@ import sys
import time
import importlib
from io import BytesIO
from PIL import Image
from psd_tools import PSDImage
@@ -125,6 +126,69 @@ def hide(psd, names: list[str], map_id: str) -> None:
print(f"[{map_id}] WARNING: excludeLayers entry {missing!r} matched nothing")
def find_layer(psd, raw_path: str, map_id: str):
node = psd
for name in raw_path.split("/"):
layer = next((child for child in node if child.name == name), None)
if layer is None:
raise SystemExit(
f"[{map_id}] layer path {raw_path!r} matched nothing. "
"Run: python tools/psd_tree.py <psd> --depth 6"
)
node = layer
return node
def embedded_underlays(psd, paths: list[str], map_id: str):
"""Expand layered PSB smart objects that psd-tools flattens incorrectly.
Interchange's exterior is a four-layer PSB. Re-compositing the outer PSD
uses an incomplete cached rendering and drops much of the west side. The
internal composite has alpha bounds that exactly match the outer smart
object's bbox, so render it explicitly as the bottom map-art layer and
keep the outer fixes, boundaries, interiors and inset above it.
"""
result = []
for path in paths:
layer = find_layer(psd, path, map_id)
if layer.kind != "smartobject" or layer.smart_object is None:
raise SystemExit(f"[{map_id}] underlay {path!r} is not a smart object")
if layer.smart_object.filetype not in {"8bps", "8bpb"}:
raise SystemExit(f"[{map_id}] underlay {path!r} is not an embedded PSD/PSB")
embedded = PSDImage.open(BytesIO(layer.smart_object.data))
image = embedded.composite(viewport=embedded.viewbox, force=True).convert("RGBA")
alpha_bbox = image.getchannel("A").getbbox()
if alpha_bbox is None:
raise SystemExit(f"[{map_id}] underlay {path!r} rendered empty")
image = image.crop(alpha_bbox)
left, top, right, bottom = layer.bbox
expected = (right - left, bottom - top)
if image.size != expected:
raise SystemExit(
f"[{map_id}] underlay {path!r} rendered {image.size}, "
f"expected outer bbox {expected}"
)
layer.visible = False
view_left, view_top, _, _ = psd.viewbox
result.append((image, (left - view_left, top - view_top)))
print(f"[{map_id}] expanded embedded underlay {path!r}")
return result
def alpha_composite_clipped(canvas: Image.Image, source: Image.Image,
position: tuple[int, int]) -> None:
x, y = position
left, top = max(0, x), max(0, y)
right = min(canvas.width, x + source.width)
bottom = min(canvas.height, y + source.height)
if right <= left or bottom <= top:
return
crop = source.crop((left - x, top - y, right - x, bottom - y))
canvas.alpha_composite(crop, (left, top))
def export(m: dict) -> None:
psd_path = ROOT / m["psd"]
if not psd_path.exists():
@@ -146,10 +210,21 @@ def export(m: dict) -> None:
if m.get("excludeLayers"):
hide(psd, m["excludeLayers"], m["id"])
underlays = embedded_underlays(
psd, m.get("baseUnderlaySmartObjects", []), m["id"],
)
# Keep alpha: these maps are composited onto the overall base map, so
# anything outside the map art should show the terrain underneath rather
# than an opaque white or black rectangle.
img = psd.composite(viewport=psd.viewbox, force=True).convert("RGBA")
remainder = psd.composite(viewport=psd.viewbox, force=True).convert("RGBA")
if underlays:
img = Image.new("RGBA", remainder.size, (0, 0, 0, 0))
for underlay, position in underlays:
alpha_composite_clipped(img, underlay, position)
img.alpha_composite(remainder)
else:
img = remainder
img.save(out, "PNG", optimize=False, compress_level=1)
print(f"[{m['id']}] wrote {out.relative_to(ROOT)} "
f"{img.size[0]}x{img.size[1]} in {time.time() - t0:.0f}s\n")