# -*- coding: utf-8 -*- """Einmal-Werkzeug fuer CR 23: entpackt die Quaternius-CC0-Packs aus assets/_staging/ nach assets/models/, bringt alle Dateinamen auf snake_case und schreibt die relativen URIs in den glTF-Dateien mit. Texturen liegen pro Pack einmal unter /textures/; die Modelle liegen in thematischen Unterordnern und referenzieren sie relativ. """ import io, json, os, re, shutil, sys, zipfile STAGING = "assets/_staging" OUT = "assets/models" def to_snake(name): name = name.replace(" ", "_").replace("-", "_") name = re.sub(r"(?<=[a-z0-9])(?=[A-Z])", "_", name) name = re.sub(r"(?<=[A-Z])(?=[A-Z][a-z])", "_", name) name = re.sub(r"_+", "_", name) return name.lower().strip("_") def snake_file(filename): stem, ext = os.path.splitext(filename) return to_snake(stem) + ext.lower() class Pack: def __init__(self, zip_name): self.zip = zipfile.ZipFile(os.path.join(STAGING, zip_name)) self.names = self.zip.namelist() def find(self, suffix): return [n for n in self.names if n.endswith(suffix)] def in_dir(self, directory, ext): return sorted(n for n in self.names if os.path.dirname(n) == directory and n.lower().endswith(ext)) def resolve_uri(pack, source_dir, uri): """Findet die Quelldatei zu einer glTF-URI. Faengt den Quaternius- Namensfehler ab, bei dem Texturen als '_png.png' referenziert werden, aber als '.png' im Pack liegen.""" candidate = source_dir + "/" + uri if candidate in pack.names: return candidate, False stem, ext = os.path.splitext(uri) if stem.endswith("_png"): repaired = source_dir + "/" + stem[:-4] + ext if repaired in pack.names: return repaired, True return None, False def write_gltf(pack, source, target_dir, texture_dir_rel, texture_out_dir, stats): """Schreibt eine glTF samt .bin und Texturen um.""" source_dir = os.path.dirname(source) data = json.loads(pack.zip.read(source)) for buffer in data.get("buffers", []): uri = buffer.get("uri") if not uri: continue found, _ = resolve_uri(pack, source_dir, uri) if found is None: stats["missing"].append(source + " -> " + uri) continue new_name = snake_file(os.path.basename(uri)) os.makedirs(target_dir, exist_ok=True) with open(os.path.join(target_dir, new_name), "wb") as out: out.write(pack.zip.read(found)) buffer["uri"] = new_name for image in data.get("images", []): uri = image.get("uri") if not uri: continue found, was_repaired = resolve_uri(pack, source_dir, uri) if found is None: stats["missing"].append(source + " -> " + uri) continue if was_repaired: stats["repaired"].add(uri) new_name = snake_file(os.path.basename(found)) os.makedirs(texture_out_dir, exist_ok=True) texture_path = os.path.join(texture_out_dir, new_name) if not os.path.exists(texture_path): with open(texture_path, "wb") as out: out.write(pack.zip.read(found)) stats["textures"] += 1 image["uri"] = texture_dir_rel + new_name os.makedirs(target_dir, exist_ok=True) target = os.path.join(target_dir, snake_file(os.path.basename(source))) with io.open(target, "w", encoding="utf-8", newline="\n") as out: json.dump(data, out, ensure_ascii=False, indent="\t") stats["models"] += 1 return target def copy_raw(pack, source, target_dir, stats): os.makedirs(target_dir, exist_ok=True) target = os.path.join(target_dir, snake_file(os.path.basename(source))) with open(target, "wb") as out: out.write(pack.zip.read(source)) stats["models"] += 1 return target PROP_CATEGORIES = [ ("moebel", ("Bed_", "Bench", "Bookcase", "Cabinet", "Chair", "Nightstand", "Shelf", "Stool", "Table_Large", "Workbench", "Peg_Rack", "BookStand", "WeaponStand")), ("buecher", ("Book", "Scroll")), ("behaelter", ("Barrel", "Bag", "Bucket", "Cage", "Chest", "Crate", "FarmCrate", "Pouch", "Pot_")), ("licht", ("Candle", "Chandelier", "Lantern", "Torch")), ("kueche", ("Cauldron", "Chalice", "Mug", "Bottle", "SmallBottle", "Vase", "Potion", "Table_", "Carrot")), ("waffen", ("Axe", "Sword", "Pickaxe", "Shield", "Anvil", "Whetstone", "Dummy")), ] ENVIRONMENT_CATEGORIES = [ ("waende", ("Wall_",)), ("tueren", ("Door_", "DoorFrame_")), ("fenster", ("Window", "WindowShutters_")), ("boeden", ("Floor_",)), ("daecher", ("Roof_",)), ("treppen", ("Stair_", "Stairs_")), ("balkone", ("Balcony_",)), ("ecken", ("Corner_",)), ("vorspruenge", ("Overhang_",)), ("bauteile", ("Prop_", "HoleCover_")), ] ## Redundante Dach-Groessenvarianten: 4x4, 6x6 und 8x8 bleiben als ## Stellvertreter, der Rest der Matrix waere nur Ballast im Browser. ENVIRONMENT_SKIP = { "Roof_RoundTiles_4x6", "Roof_RoundTiles_4x8", "Roof_RoundTiles_6x4", "Roof_RoundTiles_6x8", "Roof_RoundTiles_6x10", "Roof_RoundTiles_6x12", "Roof_RoundTiles_6x14", "Roof_RoundTiles_8x10", "Roof_RoundTiles_8x12", "Roof_RoundTiles_8x14", } def categorize(stem, categories): for folder, prefixes in categories: for prefix in prefixes: if stem.startswith(prefix): return folder return "sonstiges" def new_stats(): return {"models": 0, "textures": 0, "missing": [], "repaired": set()} def report(label, stats): print(" %-26s %3d Modelle, %2d Texturen" % (label, stats["models"], stats["textures"])) for uri in sorted(stats["repaired"]): print(" URI repariert: %s" % uri) for miss in stats["missing"]: print(" FEHLT: %s" % miss) def main(): total = new_stats() # --- Universal Base Characters ------------------------------------- pack = Pack("Universal Base Characters[Standard].zip") stats = new_stats() base_dir = "Universal Base Characters[Standard]/Base Characters/Godot - UE" for model in pack.in_dir(base_dir, ".gltf"): write_gltf(pack, model, OUT + "/characters/basis", "../textures/", OUT + "/characters/textures", stats) hair_dir = ("Universal Base Characters[Standard]/Hairstyles/" "Rigged to Head Bone/glTF (Godot -Unreal)") for model in pack.in_dir(hair_dir, ".gltf"): write_gltf(pack, model, OUT + "/characters/haare", "../textures/", OUT + "/characters/textures", stats) report("characters", stats) # --- Universal Animation Library ------------------------------------ pack = Pack("Universal Animation Library[Standard].zip") stats = new_stats() for model in pack.find(".glb"): if "/Unreal-Godot/" in model: copy_raw(pack, model, OUT + "/characters/animations", stats) report("characters/animations", stats) # --- Modular Character Outfits - Fantasy ---------------------------- pack = Pack("Modular Character Outfits - Fantasy[Standard].zip") stats = new_stats() root = "Modular Character Outfits - Fantasy[Standard]/Exports/glTF (Godot-Unreal)" for model in pack.in_dir(root + "/Outfits", ".gltf"): write_gltf(pack, model, OUT + "/outfits/komplett", "../textures/", OUT + "/outfits/textures", stats) for model in pack.in_dir(root + "/Modular Parts", ".gltf"): write_gltf(pack, model, OUT + "/outfits/teile", "../textures/", OUT + "/outfits/textures", stats) report("outfits", stats) # --- Fantasy Props MegaKit ------------------------------------------ pack = Pack("Fantasy Props MegaKit[Standard].zip") stats = new_stats() for model in pack.in_dir("Exports/glTF", ".gltf"): stem = os.path.splitext(os.path.basename(model))[0] folder = categorize(stem, PROP_CATEGORIES) write_gltf(pack, model, OUT + "/props/" + folder, "../textures/", OUT + "/props/textures", stats) report("props", stats) # --- Medieval Village MegaKit --------------------------------------- pack = Pack("Medieval Village MegaKit[Standard].zip") stats = new_stats() village_dir = "Medieval Village MegaKit[Standard]/glTF" skipped = 0 for model in pack.in_dir(village_dir, ".gltf"): stem = os.path.splitext(os.path.basename(model))[0] if stem in ENVIRONMENT_SKIP: skipped += 1 continue folder = categorize(stem, ENVIRONMENT_CATEGORIES) write_gltf(pack, model, OUT + "/environment/" + folder, "../textures/", OUT + "/environment/textures", stats) report("environment", stats) print(" (%d redundante Dach-Groessenvarianten uebersprungen)" % skipped) for stats_done in []: pass if __name__ == "__main__": main()