""" restore_atlas_stex_v2.py - Restore atlas .stex files with CORRECT Godot-imported dimensions. Atlas textures must match Godot's import dimensions (respecting size_limit) because the AtlasTexture coordinates in .tscn files refer to those exact dimensions. """ import os import struct import io import re from PIL import Image def find_atlas_texture_refs(project_dir): """Scan all .tscn files and return set of res:// source texture paths.""" atlas_paths = set() for root, dirs, files in os.walk(project_dir): parts = root.replace('\\', '/').split('/') skip = False for p in parts: if p.startswith('.') or p in ('Tools',): skip = True break if skip: continue for f in files: if not f.endswith('.tscn'): continue filepath = os.path.join(root, f) with open(filepath, 'r', encoding='utf-8', errors='replace') as fp: try: content = fp.read() except: continue ext_resources = {} for match in re.finditer(r'\[ext_resource path="([^"]+)" type="[^"]*" id=(\d+)\]', content): ext_resources[match.group(2)] = match.group(1) for match in re.finditer(r'atlas = ExtResource\( (\d+) \)', content): res_id = match.group(1) if res_id in ext_resources: atlas_paths.add(ext_resources[res_id]) return atlas_paths def read_import_meta(meta_path): """Parse .png.import metadata, return dict of params.""" params = {'size_limit': 0, 'lossy_quality': 0.7} with open(meta_path, 'r', encoding='utf-8') as f: for line in f: m = re.match(r'^size_limit=(\d+)', line) if m: params['size_limit'] = int(m.group(1)) m = re.match(r'^compress/lossy_quality=([\d.]+)', line) if m: params['lossy_quality'] = float(m.group(1)) return params def compute_godot_dimensions(src_w, src_h, size_limit): """Compute the dimensions Godot would import at given size_limit.""" if size_limit <= 0: return src_w, src_h max_dim = max(src_w, src_h) if max_dim <= size_limit: return src_w, src_h scale = size_limit / max_dim new_w = max(1, int(src_w * scale)) new_h = max(1, int(src_h * scale)) return new_w, new_h def write_stex28(filepath, width, height, flags, mipmaps, data_type, data_size, image_data): """Write Godot 3.x .stex with 28-byte header.""" with open(filepath, "wb") as f: f.write(b"GDST") f.write(struct.pack(" {target_w}x{target_h} (src={src_w}x{src_h}, size_limit={size_limit})") try: if target_w != src_w or target_h != src_h: img = src_img.resize((target_w, target_h), Image.LANCZOS) else: img = src_img buf = io.BytesIO() img.save(buf, format="WEBP", lossless=False, quality=int(quality * 100)) buf.seek(0) webp_data = buf.read() write_stex28(stex_path, target_w, target_h, flags, mipmaps, data_type, len(webp_data), webp_data) restored += 1 print(f" OK ({len(webp_data)} bytes)") except Exception as e: errors += 1 print(f" ERROR: {e}") print(f"\nResults:") print(f" Restored: {restored}") print(f" Already correct: {already_ok}") print(f" Errors: {errors}") if __name__ == "__main__": main()