Skip to content

Instantly share code, notes, and snippets.

@hrehfeld
Last active March 7, 2021 23:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrehfeld/a3a9b33ede20f895b240ba60bcc5f429 to your computer and use it in GitHub Desktop.
Save hrehfeld/a3a9b33ede20f895b240ba60bcc5f429 to your computer and use it in GitHub Desktop.
Compile Quake .map to .bsp automatically whenever the file is modified
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.observers.polling import PollingObserver
import watchdog.events
from pathlib import Path
from subprocess import check_call
import subprocess
basename = Path('/mnt/games/quake_editing/ID1/maps/megad2lvl3-11')
watched_file = basename.with_suffix('.map')
output_file = basename.with_suffix('.bsp')
compiler_base = Path('/mnt/games/quake_editing/compilers/tyrutils-ericw-v0.15.9-Linux/bin')
commands = [
[compiler_base / 'qbsp', '-nopercent', watched_file, output_file]
, [compiler_base / 'vis'] + '-rate 1,1.0,0,1 -nosave -level 4'.split() + [output_file]
, [compiler_base / 'light'] + '-bounce -bouncescale 1 -gate 0.7'.split() + [output_file]
]
commands = [list(map(str, cmd)) for cmd in commands]
#trenchbroom save touches file twice
every_num_writes = 2
class Handler(watchdog.events.FileSystemEventHandler):
def __init__(self, every_num_writes=1):
watchdog.events.FileSystemEventHandler.__init__(self)
self.num_writes = 0
def on_modified(self, event):
if event.src_path == str(watched_file):
self.num_writes += 1
if self.num_writes < every_num_writes:
return
self.num_writes = 0
for cmd in commands:
print(' '.join(cmd))
try:
check_call(cmd, cwd=str(basename.parent))
pass
except subprocess.CalledProcessError as e:
print(e)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
event_handler = Handler()
observer = Observer()
path = watched_file.parent
print('watching %s', watched_file)
observer.schedule(event_handler, str(path))
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment