vehicle-generator/scripts/cages.ts
CRT d8f3a6e3df Vehicle Generator 0.1.0 (alpha)
Procedural, game-ready vehicles for three.js: 13 body styles, blending, seeded
random vehicles, three LODs, colliders, 13 parametric mods, paint and livery,
a rig that drives, and GLB export that a game can load and drive. Ships with a
studio (browser or Tauri desktop app) and a Play mode.
2026-10-11 00:22:23 +00:00

76 lines
3.7 KiB
TypeScript

/**
* Rebuilds the authored body cages with Blender, headless.
*
* bun run cages:build every archetype in assets/src
* bun run cages:build sedan one
* bun run cages:build sedan --render renders/cages
* bun run cages:watch rebuild on every save under assets/src
*
* Building a cage is plain Python, so it runs on python3 in a fraction of a
* second; Blender is started only for `--render` (or when python3 is missing).
* BLENDER may point at the executable; otherwise the usual install paths are
* tried. The JSON lands in src/generator/cages; the runtime never needs Blender.
*/
import { spawnSync } from 'node:child_process'
import { copyFileSync, existsSync, readdirSync, readFileSync, rmSync, watch } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const candidates = [
process.env.BLENDER,
'C:/Program Files (x86)/Steam/steamapps/common/Blender/blender.exe',
'C:/Program Files/Blender Foundation/Blender/blender.exe',
'/Applications/Blender.app/Contents/MacOS/Blender',
`${process.env.HOME}/Library/Application Support/Steam/steamapps/common/Blender/Blender.app/Contents/MacOS/Blender`,
'/usr/bin/blender',
'blender',
].filter(Boolean) as string[]
const blender = candidates.find((c) => c === 'blender' || existsSync(c))!
const python = spawnSync('python3', ['--version']).status === 0 ? 'python3' : null
const args = process.argv.slice(2)
const renderAt = args.indexOf('--render')
const render = renderAt >= 0 ? ['--render', args[renderAt + 1]!] : []
const names = args.filter((a, i) => !a.startsWith('--') && i !== renderAt + 1)
const styles = names.length ? names : readdirSync('assets/src').filter((f) => f.endsWith('.py') && f !== 'cagelib.py').map((f) => f.slice(0, -3))
function build(list: readonly string[]): boolean {
for (const style of list) {
const t0 = performance.now()
// Written beside, then moved over the checked-in JSON only when it changed,
// so an unchanged save does not reload the studio.
const out = `src/generator/cages/${style}.json`
const fresh = join(tmpdir(), `vehicle-generator-cage-${style}.json`)
const script = [`assets/src/${style}.py`, '--', '--out', fresh, ...render]
const run = render.length || !python ? spawnSync(blender, ['-b', '--factory-startup', '--python', ...script], { encoding: 'utf8' }) : spawnSync(python, script, { encoding: 'utf8' })
const lines = `${run.stdout}\n${run.stderr}`.split('\n').filter((l) => /^cage:|renders in|Error|Traceback|line \d+/.test(l)).map((l) => l.replace(fresh, out))
console.log(`${lines.join('\n')}${watching ? ` (${Math.round(performance.now() - t0)} ms)` : ''}`)
if (run.status !== 0) return false
if (!existsSync(out) || readFileSync(out, 'utf8') !== readFileSync(fresh, 'utf8')) copyFileSync(fresh, out)
rmSync(fresh)
}
return true
}
const watching = args.includes('--watch')
if (!watching) {
if (!build(styles)) process.exit(1)
} else {
// A save to one archetype rebuilds it; a save to the shared library rebuilds
// them all. Vite picks up the new JSON and the studio reloads in place.
build(styles)
const all = (): string[] => readdirSync('assets/src').filter((f) => f.endsWith('.py') && f !== 'cagelib.py').map((f) => f.slice(0, -3))
let pending = new Set<string>()
let timer: ReturnType<typeof setTimeout> | null = null
watch('assets/src', (_event, file) => {
if (!file || !file.endsWith('.py')) return
for (const style of file === 'cagelib.py' ? all() : [file.slice(0, -3)]) pending.add(style)
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
const list = [...pending].filter((s) => existsSync(`assets/src/${s}.py`))
pending = new Set()
build(list)
}, 150)
})
console.log('watching assets/src')
}