Skip to content

3D engine that AI agents drive

Read the scene file once instead of checking it exists first (#2158)

Nguyen Xuan An59abda0a626a
diff --git a/bench/scene-frames/camera-overrides.mjs b/bench/scene-frames/camera-overrides.mjs
index 670c3538..9b45f163 100644
--- a/bench/scene-frames/camera-overrides.mjs
+++ b/bench/scene-frames/camera-overrides.mjs
@@ -1,7 +1,7 @@
// Committed default cameras for the benchmark scenes that are generated locally from Blender demo files
// (scripts/blender-demo-to-glb.py), whose own camera renders a flat frame (#2158). camera-overrides.json maps a scene name
// to the eye and target that show the scene; the sweep applies it to scene.json before it renders.
-import { existsSync, readFileSync, writeFileSync } from 'node:fs';
+import { readFileSync, writeFileSync } from 'node:fs';
import { basename } from 'node:path';
const OVERRIDES_URL = new URL('./camera-overrides.json', import.meta.url);
@@ -24,11 +24,20 @@ export function applyCameraOverride(sceneName, description, overrides) {
return { ...description, eye: [...override.eye], target: [...override.target] };
}
+// Reads the file once instead of checking for it first, so nothing can swap it between the check and the read.
+function readSceneFile(scenePath, directory) {
+ try {
+ return readFileSync(scenePath, 'utf8');
+ } catch (error) {
+ if (error.code === 'ENOENT') throw new Error(`No scene.json in ${directory}. Pass a scene directory generated by scripts/blender-demo-to-glb.py.`);
+ throw error;
+ }
+}
+
// Rewrites <directory>/scene.json in place and returns true when the override changed it.
export function applyCameraOverrideToDirectory(directory, overrides = loadCameraOverrides()) {
const scenePath = `${directory}/scene.json`;
- if (!existsSync(scenePath)) throw new Error(`No scene.json in ${directory}. Pass a scene directory generated by scripts/blender-demo-to-glb.py.`);
- const description = JSON.parse(readFileSync(scenePath, 'utf8'));
+ const description = JSON.parse(readSceneFile(scenePath, directory));
const updated = applyCameraOverride(description.id ?? basename(directory), description, overrides);
if (updated === description) return false;
writeFileSync(scenePath, `${JSON.stringify(updated, null, 2)}\n`);