#!/usr/bin/env python3 """Dumps a puppet .mdl file's MDLS bone array: parent index, bind-pose matrix determinant (to spot a mirrored/negative-scale bone), and translation - without needing a debug build. Mirrors the parsing in CImage.cpp's parsePuppetBones()/readPuppetMeshData(): MDLS header is 9 bytes, then nextSectionOffset(u32) + boneCount(u32), then per bone: null-terminated name + type(u32) + parent(i32) + matrixBytes(u32) + [16 floats if matrixBytes==64] + null-terminated extra string (empty for most rigs, jiggle/physics JSON for some). The 16 floats are the file's row-major matrix - reshaping them row-major (not glm's column-major convention) gives the actual authored matrix back, which is what this script does. With MDLS version 2+ and its flag byte set, a second matrix per bone follows: the rest pose the animation starts from (the records above are the bind pose the vertices and inverse bind matrices are in, wallpaper64.exe 2.8.42 sub_1401FBAE0/sub_1401FDF90). Those are printed next to the record's translation. The MDLV header (version, format, materials, vertex/index byte counts per mesh) is printed first. Usage: mdl_bones.py mdl_bones.py --scan [workshop dir] every .mdl inside the installed scene.pkg files whose rest pose differs from the bind pose (default dir: the sandbox workshop library) Needs: pip install numpy """ import glob import os import struct import sys import numpy as np def read_cstr(data, offset): end = data.index(b'\x00', offset) return data[offset:end].decode('latin1', errors='replace'), end + 1 def print_meshes(data): tag, offset = read_cstr(data, 0) if not tag.startswith('MDLV'): return version = int(tag[4:]) default_format, materials_per_mesh, mesh_count = struct.unpack_from('= 17: offset += 24 vertex_format = default_format if version >= 15: vertex_format = struct.unpack_from('= 21: print(' (version 21+ trailing per-mesh records not walked, only the first mesh is reliable)') break def read_bones(data, mdls_offset): """Returns (version, [(name, parent, matrix or None, extra)], [rest matrices] or None).""" version = int(data[mdls_offset + 4:mdls_offset + 8]) offset = mdls_offset + 13 bone_count = struct.unpack_from(' 4096: raise ValueError(f"bone {i} has an implausible matrix byte count ({matrix_bytes}), layout desynced") offset += matrix_bytes extra, offset = read_cstr(data, offset) bones.append((name, parent, matrix, extra)) if version < 2: return version, bones, None extra_count = struct.unpack_from(' 0.01: print(pkg.split(os.sep)[-2], name, f'rest pose differs from bind pose by up to {difference:.1f}') def main(path): data = open(path, 'rb').read() print_meshes(data) marker_size = 9 mdls_offset = data.find(b'MDLS', marker_size) if mdls_offset < 0: raise SystemExit("no MDLS section found - not a puppet mesh, or a layout this script doesn't handle") print('MDLS at', mdls_offset, 'file size', len(data)) offset = mdls_offset + 9 # skip the MDLS0001-style 9-byte magic next_section_offset = struct.unpack_from('= 2 and sys.argv[1] == '--scan': scan(sys.argv[2] if len(sys.argv) > 2 else '/workspace/SteamLibrary/steamapps/workshop/content/431960') elif len(sys.argv) == 2: main(sys.argv[1]) else: print(__doc__) raise SystemExit(1)