Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Created April 22, 2026 03:58
Show Gist options
  • Select an option

  • Save derrickturk/4eaab1583f25e14b0f345744378c120b to your computer and use it in GitHub Desktop.

Select an option

Save derrickturk/4eaab1583f25e14b0f345744378c120b to your computer and use it in GitHub Desktop.
Convert directories full of LDraw .dat files to a single .mpd file
from pathlib import Path
from queue import Queue
import sys
def stream_files(q, basedir, mpd):
first = True
seen = set()
while q:
datpath = basedir / q.pop()
if not datpath.exists():
print(f'warning: {datpath.name} not found', file=sys.stderr)
continue
with open(datpath, 'r') as dat:
if not first:
mpd.write('\n')
mpd.write(f'0 FILE {datpath.name}\n')
for line in dat:
match line.split():
case '1', *rest:
refdat = rest[-1]
if refdat not in seen:
q.append(Path(refdat))
seen.add(refdat)
case _:
pass
mpd.write(line)
first = False
def main(argv):
if len(argv) != 2:
print(f'Usage: {argv[0]} main-dat-file', file=sys.stderr)
return 2
datpath = Path(argv[1])
mpdpath = datpath.with_suffix('.mpd')
basedir = datpath.parent
with open(mpdpath, 'w') as mpd:
stream_files([datpath.relative_to(basedir)], basedir, mpd)
if __name__ == '__main__':
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment