#!/usr/bin/env python3 """Export original Scooter resource data; do not assign attachments or playback. The two Scooter ASE morphs have 195 indexed positions, whereas scooter.omt's body has 98. Keep these samples unbound rather than inventing UVs or a base. """ from __future__ import annotations import argparse import hashlib import json from pathlib import Path from ase import export_ase, read_ase from entities import resolve_asset from models import export_omt_model from omt import Database, FormatError, read_shape from png import read_png SOURCES = { 'omt/scooter.omt': '8d4ffdafee5fd819c5220ee4f608c901a5988fe6b8b13316e15618f5eec75e81', 'ASE/jimstop.ASE': 'b294d090ac5c4ec449d845db6d837f2cbedf5f7b812f5b6ed2bb70f85246f1d0', 'ASE/jimscooter.ASE': 'b27c8cb80811eb70e391fc061dab99c7d5f0b4d884c109b34c673a1815508299', 'ASE/jimscooterstop.ASE': 'edcdcc879a28757994525f815c88cd6dd46dc788b69ba2f902323d6de48cb873', 'ASE/godscooter.ASE': 'c18f1414ec2b397e0c8f8320f63830ab88630ea05a354bb99af34ce7fea79bd1', 'ASE/godscooter2.ASE': 'fcf9ca5e8298efa7b4d551d9d09878c04f1f2647b6a30edfa2297617a7d63189', 'png/jimycarl.png': '55ed165aaffe177cb62558a08d13fc3b2210da6e353065b108849d46df9dacb6', } def _json(path: Path, value): path.write_text(json.dumps(value, separators=(',', ':'), allow_nan=False)+'\n', encoding='utf-8') def export_scooter(original: Path, output: Path) -> dict: original, output = original.resolve(), output.resolve() if output.is_relative_to(original) or original.is_relative_to(output): raise ValueError('Output must be separate from the original installation') # Existing descendants are checked before any helper writes into them. if output.exists() and any(p.is_symlink() for p in output.rglob('*')): raise ValueError('Generated Scooter paths must not contain symlinks') paths = {name: resolve_asset(original, name) for name in SOURCES} for name, path in paths.items(): if hashlib.sha256(path.read_bytes()).hexdigest() != SOURCES[name]: raise FormatError(f'Unexpected original Scooter source identity: {name}') database = Database(paths['omt/scooter.omt']) shapes = {} for identity, expected in ((0, ('goddard01', 98, 176)), (1, ('wheel', 32, 56))): entry = next(e for e in database.entries if e.type == '3DSh' and e.id == identity) shape = read_shape(database.payload(entry)) if (entry.name, len(shape['vertices']), len(shape['polygons'])) != expected: raise FormatError('Unexpected original Scooter shape metadata') shapes[identity] = shape animations = {name: read_ase(path) for name, path in paths.items() if name.startswith('ASE/')} for name, animation in animations.items(): expected_count = 195 if 'godscooter' in name else 426 if len(animation['objects']) != 1 or len(animation['objects'][0]['samples']) != 2: raise FormatError('Unexpected original Scooter animation objects/samples') if any(len(s['vertices']) != expected_count for s in animation['objects'][0]['samples']): raise FormatError('Unexpected original Scooter morph vertex count') texture_path = resolve_asset(original, 'png/jimycarl.png') texture = read_png(texture_path) rgba = texture.pop('rgba') texture.update(source=texture_path.name, sourceSha256=hashlib.sha256(texture_path.read_bytes()).hexdigest(), rgbaSha256=hashlib.sha256(rgba).hexdigest(), path='../jimycarl.rgba') manifest = dict(schema=1, sources=SOURCES.copy(), shapes=[], animations=[], unboundMorphs=[], bindingPolicy='Resource-only export; caller owns original attachment, transforms and timing') output.mkdir(parents=True, exist_ok=True) for identity in shapes: folder = f'shape_{identity}' details = export_omt_model(database, identity, output/folder) manifest['shapes'].append(dict(id=identity, path=folder+'/scene.json', **details)) (output/'jimycarl.rgba').write_bytes(rgba) for stem in ('jimscooter', 'jimscooterstop'): animation = export_ase(paths[f'ASE/{stem}.ASE'], output/stem, paths['ASE/jimstop.ASE']) animation['texture'] = texture _json(output/stem/'animation.json', animation) manifest['animations'].append(dict(source=stem+'.ASE', path=stem+'/animation.json')) for stem in ('godscooter', 'godscooter2'): animation = animations[f'ASE/{stem}.ASE'] filename = stem+'-unbound.json' _json(output/filename, animation) manifest['unboundMorphs'].append(dict(source=stem+'.ASE', path=filename, vertices=195, sourceTimes=[s['time'] for s in animation['objects'][0]['samples']], reason='No matching textured base recovered; OMT body 98 and existing Goddard base 222 differ')) _json(output/'scooter.json', manifest) return manifest def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('original', type=Path) parser.add_argument('--output', type=Path, required=True) args = parser.parse_args() export_scooter(args.original, args.output) print(f'Original Scooter resources exported: {args.output}/scooter.json') if __name__ == '__main__': main()