@pascal-app/geometry-script 0.0.0-stage → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +4 -2
- package/bpy/runner.py +169 -0
- package/dist/blender-local.d.ts +16 -0
- package/dist/blender-local.d.ts.map +1 -0
- package/dist/blender-local.js +49 -0
- package/dist/blender.d.ts +30 -0
- package/dist/blender.d.ts.map +1 -0
- package/dist/blender.js +79 -0
- package/dist/compile.d.ts +34 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +783 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/sandbox-window.d.ts +2 -0
- package/dist/sandbox-window.d.ts.map +1 -0
- package/dist/sandbox-window.js +5 -0
- package/dist/sandbox-worker.d.ts +2 -0
- package/dist/sandbox-worker.d.ts.map +1 -0
- package/dist/sandbox-worker.js +52 -0
- package/dist/sandbox.d.ts +2 -0
- package/dist/sandbox.js +2 -0
- package/dist/transform.d.ts +10 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +73 -0
- package/package.json +66 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pascal Group Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @pascal-app/geometry-script
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Compiles AI-authored three.js geometry modules into a GLB artifact plus a Pascal manifest (parts, paint slots, lights, cutouts, surfaces, undersides, clips). Runs in a browser worker, Bun or Node.
|
|
4
|
+
|
|
5
|
+
See `wiki/architecture/authored-objects.md`.
|
package/bpy/runner.py
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"""Runs one Blender (bpy) geometry script and exports what it built as a GLB.
|
|
2
|
+
|
|
3
|
+
python runner.py <dir> # the `bpy` wheel
|
|
4
|
+
blender -b --factory-startup --python-exit-code 1 --python runner.py -- <dir>
|
|
5
|
+
|
|
6
|
+
Reads `<dir>/request.json` ({"code": str, "params": {id: value}}) and writes
|
|
7
|
+
`<dir>/result.json` plus, on success, `<dir>/model.glb`. The module declares
|
|
8
|
+
`PARAMS` (same shape as a three.js script's `params`), an optional `MOUNT` and
|
|
9
|
+
`build(params)`, which adds objects to the empty scene it is given. Everything in
|
|
10
|
+
the scene is exported; `@pascal-app/geometry-script/blender` reads the GLB into
|
|
11
|
+
the same manifest as a three.js build.
|
|
12
|
+
|
|
13
|
+
This file is the protocol, not the sandbox: the host runs it with no network, no
|
|
14
|
+
secrets and a deadline. Parameter resolution mirrors `resolveParams` in
|
|
15
|
+
compile.ts; the reader re-resolves and refuses a mismatch.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import contextlib
|
|
19
|
+
import io
|
|
20
|
+
import json
|
|
21
|
+
import math
|
|
22
|
+
import sys
|
|
23
|
+
import traceback
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
MOUNTS = ("floor", "wall", "wall-side", "ceiling")
|
|
27
|
+
LOG_LIMIT = 8_000
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def param_specs(raw):
|
|
31
|
+
if raw is None:
|
|
32
|
+
return []
|
|
33
|
+
if not isinstance(raw, dict):
|
|
34
|
+
raise ValueError(
|
|
35
|
+
"PARAMS must be a dict of { id: default | { default, min, max, step } }"
|
|
36
|
+
)
|
|
37
|
+
specs = []
|
|
38
|
+
for key, spec in raw.items():
|
|
39
|
+
described = isinstance(spec, dict)
|
|
40
|
+
value = spec.get("default", spec.get("value")) if described else spec
|
|
41
|
+
if isinstance(value, bool):
|
|
42
|
+
kind = "boolean"
|
|
43
|
+
elif isinstance(value, (int, float)):
|
|
44
|
+
if not math.isfinite(value):
|
|
45
|
+
raise ValueError(f'Param "{key}" default is not finite')
|
|
46
|
+
kind = "number"
|
|
47
|
+
elif isinstance(value, str):
|
|
48
|
+
kind = "string"
|
|
49
|
+
else:
|
|
50
|
+
raise ValueError(f'Param "{key}" needs a number, boolean or string default')
|
|
51
|
+
entry = {"id": str(key), "kind": kind, "default": value}
|
|
52
|
+
if described:
|
|
53
|
+
for field in ("label", "min", "max", "step", "unit", "options"):
|
|
54
|
+
if spec.get(field) is not None:
|
|
55
|
+
entry[field] = spec[field]
|
|
56
|
+
specs.append(entry)
|
|
57
|
+
return specs
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def same_kind(value, kind):
|
|
61
|
+
if kind == "boolean":
|
|
62
|
+
return isinstance(value, bool)
|
|
63
|
+
if kind == "number":
|
|
64
|
+
return isinstance(value, (int, float)) and not isinstance(value, bool)
|
|
65
|
+
return isinstance(value, str)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def resolve_params(specs, overrides):
|
|
69
|
+
values = {}
|
|
70
|
+
for spec in specs:
|
|
71
|
+
override = overrides.get(spec["id"])
|
|
72
|
+
value = override if same_kind(override, spec["kind"]) else spec["default"]
|
|
73
|
+
options = spec.get("options")
|
|
74
|
+
if isinstance(value, str) and options and value not in options:
|
|
75
|
+
value = spec["default"]
|
|
76
|
+
if spec["kind"] == "number":
|
|
77
|
+
if spec.get("min") is not None:
|
|
78
|
+
value = max(spec["min"], value)
|
|
79
|
+
if spec.get("max") is not None:
|
|
80
|
+
value = min(spec["max"], value)
|
|
81
|
+
values[spec["id"]] = value
|
|
82
|
+
return values
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def carry_viewport_colors(bpy):
|
|
86
|
+
# `diffuse_color` is only Blender's viewport colour; the exporter reads the
|
|
87
|
+
# Principled BSDF. A script that set just the former still means it.
|
|
88
|
+
for material in bpy.data.materials:
|
|
89
|
+
tree = material.node_tree
|
|
90
|
+
nodes = tree.nodes if tree else ()
|
|
91
|
+
bsdf = next((n for n in nodes if n.type == "BSDF_PRINCIPLED"), None)
|
|
92
|
+
if bsdf is None:
|
|
93
|
+
continue
|
|
94
|
+
base = bsdf.inputs["Base Color"]
|
|
95
|
+
untouched = all(abs(c - 0.8) < 1e-6 for c in base.default_value[:3])
|
|
96
|
+
viewport = tuple(material.diffuse_color)
|
|
97
|
+
if not base.is_linked and untouched and viewport[:3] != (0.8, 0.8, 0.8):
|
|
98
|
+
base.default_value = viewport
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def export_glb(bpy, path):
|
|
102
|
+
bpy.ops.export_scene.gltf(
|
|
103
|
+
filepath=str(path),
|
|
104
|
+
export_format="GLB",
|
|
105
|
+
use_selection=False,
|
|
106
|
+
use_visible=False,
|
|
107
|
+
export_apply=True,
|
|
108
|
+
export_yup=True,
|
|
109
|
+
export_extras=True,
|
|
110
|
+
export_lights=True,
|
|
111
|
+
export_cameras=False,
|
|
112
|
+
export_materials="EXPORT",
|
|
113
|
+
export_image_format="NONE",
|
|
114
|
+
export_animations=True,
|
|
115
|
+
export_animation_mode="ACTIONS",
|
|
116
|
+
export_draco_mesh_compression_enable=False,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def run(directory):
|
|
121
|
+
import bpy
|
|
122
|
+
|
|
123
|
+
request = json.loads((directory / "request.json").read_text("utf-8"))
|
|
124
|
+
bpy.ops.wm.read_factory_settings(use_empty=True)
|
|
125
|
+
module = {"__name__": "pascal_script"}
|
|
126
|
+
log = io.StringIO()
|
|
127
|
+
with contextlib.redirect_stdout(log):
|
|
128
|
+
exec(compile(request["code"], "script.py", "exec"), module)
|
|
129
|
+
build = module.get("build")
|
|
130
|
+
if not callable(build):
|
|
131
|
+
raise ValueError("The script must define build(params)")
|
|
132
|
+
mount = module.get("MOUNT", "floor")
|
|
133
|
+
if mount not in MOUNTS:
|
|
134
|
+
raise ValueError(f"MOUNT must be one of {', '.join(MOUNTS)}")
|
|
135
|
+
raw_params = module.get("PARAMS")
|
|
136
|
+
resolved = resolve_params(param_specs(raw_params), request.get("params") or {})
|
|
137
|
+
build(resolved)
|
|
138
|
+
if not bpy.context.scene.objects:
|
|
139
|
+
raise ValueError("build(params) added nothing to the scene")
|
|
140
|
+
carry_viewport_colors(bpy)
|
|
141
|
+
export_glb(bpy, directory / "model.glb")
|
|
142
|
+
return {
|
|
143
|
+
"ok": True,
|
|
144
|
+
"mount": mount,
|
|
145
|
+
"params": raw_params or {},
|
|
146
|
+
"resolved": resolved,
|
|
147
|
+
"blender": bpy.app.version_string,
|
|
148
|
+
"log": log.getvalue()[-LOG_LIMIT:],
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def main():
|
|
153
|
+
args = sys.argv[sys.argv.index("--") + 1 :] if "--" in sys.argv else sys.argv[1:]
|
|
154
|
+
directory = Path(args[0])
|
|
155
|
+
try:
|
|
156
|
+
result = run(directory)
|
|
157
|
+
except Exception as error:
|
|
158
|
+
frames = traceback.extract_tb(error.__traceback__)
|
|
159
|
+
line = next((f.lineno for f in reversed(frames) if f.filename == "script.py"), None)
|
|
160
|
+
result = {
|
|
161
|
+
"ok": False,
|
|
162
|
+
"error": f"{type(error).__name__}: {error}",
|
|
163
|
+
"line": line,
|
|
164
|
+
}
|
|
165
|
+
(directory / "result.json").write_text(json.dumps(result), "utf-8")
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
if __name__ == "__main__":
|
|
169
|
+
main()
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { GeometryScriptParamValue } from '@pascal-app/core';
|
|
2
|
+
import { type BlenderRun } from './blender.js';
|
|
3
|
+
/** The runner every Blender host executes; it ships beside `dist` and `src`. */
|
|
4
|
+
export declare const BLENDER_RUNNER_PATH: string;
|
|
5
|
+
/**
|
|
6
|
+
* Runs a bpy module with a local Blender binary (`blender -b`). No sandbox: the
|
|
7
|
+
* module runs with this process's user, so it is for a developer's own machine
|
|
8
|
+
* and local agents, the way the local three.js compile is opt-in.
|
|
9
|
+
*/
|
|
10
|
+
export declare function runBlenderLocally(input: {
|
|
11
|
+
blender: string;
|
|
12
|
+
code: string;
|
|
13
|
+
params?: Record<string, GeometryScriptParamValue>;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
}): Promise<BlenderRun>;
|
|
16
|
+
//# sourceMappingURL=blender-local.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blender-local.d.ts","sourceRoot":"","sources":["../src/blender-local.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAA;AAChE,OAAO,EAAE,KAAK,UAAU,EAAc,MAAM,WAAW,CAAA;AAEvD,gFAAgF;AAChF,eAAO,MAAM,mBAAmB,QAA8D,CAAA;AAE9F;;;;GAIG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE;IAC7C,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,wBAAwB,CAAC,CAAA;IACjD,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,GAAG,OAAO,CAAC,UAAU,CAAC,CA2CtB"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { blenderRun } from './blender.js';
|
|
7
|
+
/** The runner every Blender host executes; it ships beside `dist` and `src`. */
|
|
8
|
+
export const BLENDER_RUNNER_PATH = fileURLToPath(new URL('../bpy/runner.py', import.meta.url));
|
|
9
|
+
/**
|
|
10
|
+
* Runs a bpy module with a local Blender binary (`blender -b`). No sandbox: the
|
|
11
|
+
* module runs with this process's user, so it is for a developer's own machine
|
|
12
|
+
* and local agents, the way the local three.js compile is opt-in.
|
|
13
|
+
*/
|
|
14
|
+
export async function runBlenderLocally(input) {
|
|
15
|
+
const directory = await mkdtemp(join(tmpdir(), 'pascal-bpy-'));
|
|
16
|
+
try {
|
|
17
|
+
await writeFile(join(directory, 'request.json'), JSON.stringify({ code: input.code, params: input.params ?? {} }));
|
|
18
|
+
await new Promise((resolve, reject) => {
|
|
19
|
+
const child = spawn(input.blender, [
|
|
20
|
+
'-b',
|
|
21
|
+
'--factory-startup',
|
|
22
|
+
'--python-exit-code',
|
|
23
|
+
'1',
|
|
24
|
+
'--python',
|
|
25
|
+
BLENDER_RUNNER_PATH,
|
|
26
|
+
'--',
|
|
27
|
+
directory,
|
|
28
|
+
], { stdio: 'ignore', env: { PATH: process.env.PATH ?? '' } });
|
|
29
|
+
const timer = setTimeout(() => {
|
|
30
|
+
child.kill('SIGKILL');
|
|
31
|
+
reject(new Error(`The script ran longer than ${(input.timeoutMs ?? 60_000) / 1000} s`));
|
|
32
|
+
}, input.timeoutMs ?? 60_000);
|
|
33
|
+
child.on('error', (error) => {
|
|
34
|
+
clearTimeout(timer);
|
|
35
|
+
reject(error);
|
|
36
|
+
});
|
|
37
|
+
child.on('exit', () => {
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
resolve();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
const report = JSON.parse(await readFile(join(directory, 'result.json'), 'utf8').catch(() => '{}'));
|
|
43
|
+
const glb = report.ok ? new Uint8Array(await readFile(join(directory, 'model.glb'))) : null;
|
|
44
|
+
return blenderRun(report, glb);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
await rm(directory, { recursive: true, force: true });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { GeometryScriptParamValue } from '@pascal-app/core';
|
|
2
|
+
import { type GeometryScriptCompileOutput } from './compile.js';
|
|
3
|
+
/** What `bpy/runner.py` reports for a successful run, beside the GLB it exported. */
|
|
4
|
+
export type BlenderRun = {
|
|
5
|
+
glb: Uint8Array;
|
|
6
|
+
mount: unknown;
|
|
7
|
+
/** The module's `PARAMS`, as written. */
|
|
8
|
+
params: unknown;
|
|
9
|
+
/** The values `build(params)` ran with. */
|
|
10
|
+
resolved: Record<string, unknown>;
|
|
11
|
+
/** The Blender version that ran it, for the artifact's provenance. */
|
|
12
|
+
blender?: string;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The run from the runner's `result.json` and GLB, or the script's error
|
|
16
|
+
* (with its line) thrown as the build failure the agent reads.
|
|
17
|
+
*/
|
|
18
|
+
export declare function blenderRun(report: unknown, glb: Uint8Array | null): BlenderRun;
|
|
19
|
+
/**
|
|
20
|
+
* Reads what a Blender module built (the runner's GLB and report) into the same
|
|
21
|
+
* artifact and manifest as a three.js build. Runs no model code, so any host
|
|
22
|
+
* may call it; running the module itself is the Blender host's job, in its own
|
|
23
|
+
* sandbox. Params are resolved again here and must match what the run used.
|
|
24
|
+
*/
|
|
25
|
+
export declare function compileBlenderBuild(input: {
|
|
26
|
+
code: string;
|
|
27
|
+
params?: Record<string, GeometryScriptParamValue>;
|
|
28
|
+
run: BlenderRun;
|
|
29
|
+
}): Promise<GeometryScriptCompileOutput>;
|
|
30
|
+
//# sourceMappingURL=blender.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blender.d.ts","sourceRoot":"","sources":["../src/blender.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAuB,wBAAwB,EAAE,MAAM,kBAAkB,CAAA;AAGrF,OAAO,EAGL,KAAK,2BAA2B,EAIjC,MAAM,WAAW,CAAA;AAElB,qFAAqF;AACrF,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,UAAU,CAAA;IACf,KAAK,EAAE,OAAO,CAAA;IACd,yCAAyC;IACzC,MAAM,EAAE,OAAO,CAAA;IACf,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACjC,sEAAsE;IACtE,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,GAAG,UAAU,CAc9E;AAqCD;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,KAAK,EAAE;IAC/C,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,wBAAwB,CAAC,CAAA;IACjD,GAAG,EAAE,UAAU,CAAA;CAChB,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAgBvC"}
|
package/dist/blender.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
|
|
2
|
+
import { assertScriptSize, finishBuild, MOUNTS, readParamSpecs, resolveParams, } from './compile.js';
|
|
3
|
+
/**
|
|
4
|
+
* The run from the runner's `result.json` and GLB, or the script's error
|
|
5
|
+
* (with its line) thrown as the build failure the agent reads.
|
|
6
|
+
*/
|
|
7
|
+
export function blenderRun(report, glb) {
|
|
8
|
+
const result = (report ?? {});
|
|
9
|
+
if (result.ok !== true) {
|
|
10
|
+
const error = typeof result.error === 'string' ? result.error : 'Blender did not finish';
|
|
11
|
+
throw new Error(typeof result.line === 'number' ? `${error} (line ${result.line})` : error);
|
|
12
|
+
}
|
|
13
|
+
if (!glb)
|
|
14
|
+
throw new Error('Blender exported no GLB');
|
|
15
|
+
return {
|
|
16
|
+
glb,
|
|
17
|
+
mount: result.mount,
|
|
18
|
+
params: result.params,
|
|
19
|
+
resolved: (result.resolved ?? {}),
|
|
20
|
+
blender: typeof result.blender === 'string' ? result.blender : undefined,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** A light object's brightness when it sets no `intensity` property: a catalog lamp's. */
|
|
24
|
+
const DEFAULT_LIGHT_INTENSITY = 2;
|
|
25
|
+
const sameValues = (a, b) => Object.keys(a).length === Object.keys(b).length &&
|
|
26
|
+
Object.entries(a).every(([key, value]) => b[key] === value);
|
|
27
|
+
/**
|
|
28
|
+
* GLTFLoader cleans node names (`part:seat` becomes `partseat`) and keeps the
|
|
29
|
+
* original in `userData.name`, and its clip tracks address the cleaned names.
|
|
30
|
+
* Tracks are rebound to their objects' uuids first, then the names restored, so
|
|
31
|
+
* the conventions read the names the script gave.
|
|
32
|
+
*/
|
|
33
|
+
function restoreNames(scene, clips) {
|
|
34
|
+
const objects = [];
|
|
35
|
+
scene.traverse((object) => objects.push(object));
|
|
36
|
+
const byLongestName = objects
|
|
37
|
+
.filter((object) => object.name)
|
|
38
|
+
.sort((a, b) => b.name.length - a.name.length);
|
|
39
|
+
for (const clip of clips) {
|
|
40
|
+
for (const track of clip.tracks) {
|
|
41
|
+
const target = byLongestName.find((object) => track.name.startsWith(`${object.name}.`));
|
|
42
|
+
if (target)
|
|
43
|
+
track.name = `${target.uuid}.${track.name.slice(target.name.length + 1)}`;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
for (const object of objects) {
|
|
47
|
+
const userData = object.userData;
|
|
48
|
+
if (typeof userData.name === 'string')
|
|
49
|
+
object.name = userData.name;
|
|
50
|
+
delete userData.name;
|
|
51
|
+
if (object.isLight && typeof userData.intensity !== 'number') {
|
|
52
|
+
userData.intensity = DEFAULT_LIGHT_INTENSITY;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Reads what a Blender module built (the runner's GLB and report) into the same
|
|
58
|
+
* artifact and manifest as a three.js build. Runs no model code, so any host
|
|
59
|
+
* may call it; running the module itself is the Blender host's job, in its own
|
|
60
|
+
* sandbox. Params are resolved again here and must match what the run used.
|
|
61
|
+
*/
|
|
62
|
+
export async function compileBlenderBuild(input) {
|
|
63
|
+
assertScriptSize(input.code);
|
|
64
|
+
const mount = (input.run.mount ?? 'floor');
|
|
65
|
+
if (!MOUNTS.has(mount))
|
|
66
|
+
throw new Error(`MOUNT must be one of ${[...MOUNTS].join(', ')}`);
|
|
67
|
+
const specs = readParamSpecs(input.run.params);
|
|
68
|
+
const params = resolveParams(specs, input.params ?? {});
|
|
69
|
+
if (!sameValues(params, input.run.resolved)) {
|
|
70
|
+
throw new Error('Blender ran the module with different param values than Pascal resolved');
|
|
71
|
+
}
|
|
72
|
+
const bytes = input.run.glb;
|
|
73
|
+
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
74
|
+
const gltf = await new GLTFLoader().parseAsync(buffer, '');
|
|
75
|
+
restoreNames(gltf.scene, gltf.animations);
|
|
76
|
+
const built = gltf.scene;
|
|
77
|
+
built.animations = gltf.animations;
|
|
78
|
+
return finishBuild({ built, mount, specs, params, code: input.code, language: 'bpy' });
|
|
79
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { CompiledGeometryScript, GeometryScriptLanguage, GeometryScriptMount, GeometryScriptParamSpec, GeometryScriptParamValue } from '@pascal-app/core';
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
export type { GeometryScriptMount };
|
|
4
|
+
export type GeometryScriptCompileInput = {
|
|
5
|
+
code: string;
|
|
6
|
+
params?: Record<string, GeometryScriptParamValue>;
|
|
7
|
+
};
|
|
8
|
+
export type GeometryScriptCompileOutput = CompiledGeometryScript & {
|
|
9
|
+
glb: ArrayBuffer;
|
|
10
|
+
};
|
|
11
|
+
export declare const GEOMETRY_SCRIPT_LIMITS: {
|
|
12
|
+
readonly triangles: 300000;
|
|
13
|
+
readonly materials: 32;
|
|
14
|
+
readonly extent: 60;
|
|
15
|
+
};
|
|
16
|
+
export declare const MOUNTS: Set<"wall" | "ceiling" | "wall-side" | "floor">;
|
|
17
|
+
export declare function readParamSpecs(raw: unknown): GeometryScriptParamSpec[];
|
|
18
|
+
export declare function resolveParams(specs: GeometryScriptParamSpec[], overrides: Record<string, GeometryScriptParamValue>): Record<string, GeometryScriptParamValue>;
|
|
19
|
+
/**
|
|
20
|
+
* Everything after a module has built its object, whatever the language:
|
|
21
|
+
* re-origins it for its mount, reads the conventions into the manifest, samples
|
|
22
|
+
* its clips and exports the GLB. `built.animations` holds its clips.
|
|
23
|
+
*/
|
|
24
|
+
export declare function finishBuild({ built, mount, specs, params, code, language, }: {
|
|
25
|
+
built: THREE.Object3D;
|
|
26
|
+
mount: GeometryScriptMount;
|
|
27
|
+
specs: GeometryScriptParamSpec[];
|
|
28
|
+
params: Record<string, GeometryScriptParamValue>;
|
|
29
|
+
code: string;
|
|
30
|
+
language: GeometryScriptLanguage;
|
|
31
|
+
}): Promise<GeometryScriptCompileOutput>;
|
|
32
|
+
export declare function assertScriptSize(code: string): void;
|
|
33
|
+
export declare function compileGeometryScript(input: GeometryScriptCompileInput): Promise<GeometryScriptCompileOutput>;
|
|
34
|
+
//# sourceMappingURL=compile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../src/compile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,sBAAsB,EAEtB,sBAAsB,EACtB,mBAAmB,EACnB,uBAAuB,EACvB,wBAAwB,EACzB,MAAM,kBAAkB,CAAA;AAGzB,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAW9B,YAAY,EAAE,mBAAmB,EAAE,CAAA;AAEnC,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,wBAAwB,CAAC,CAAA;CAClD,CAAA;AAED,MAAM,MAAM,2BAA2B,GAAG,sBAAsB,GAAG;IAAE,GAAG,EAAE,WAAW,CAAA;CAAE,CAAA;AAEvF,eAAO,MAAM,sBAAsB;;;;CAIzB,CAAA;AAyBV,eAAO,MAAM,MAAM,iDAA0E,CAAA;AAe7F,wBAAgB,cAAc,CAAC,GAAG,EAAE,OAAO,GAAG,uBAAuB,EAAE,CA6BtE;AAED,wBAAgB,aAAa,CAC3B,KAAK,EAAE,uBAAuB,EAAE,EAChC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,wBAAwB,CAAC,GAClD,MAAM,CAAC,MAAM,EAAE,wBAAwB,CAAC,CAc1C;AAqoBD;;;;GAIG;AACH,wBAAsB,WAAW,CAAC,EAChC,KAAK,EACL,KAAK,EACL,KAAK,EACL,MAAM,EACN,IAAI,EACJ,QAAQ,GACT,EAAE;IACD,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAA;IACrB,KAAK,EAAE,mBAAmB,CAAA;IAC1B,KAAK,EAAE,uBAAuB,EAAE,CAAA;IAChC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,wBAAwB,CAAC,CAAA;IAChD,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,sBAAsB,CAAA;CACjC,GAAG,OAAO,CAAC,2BAA2B,CAAC,CA4DvC;AAMD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,QAI5C;AAED,wBAAsB,qBAAqB,CACzC,KAAK,EAAE,0BAA0B,GAChC,OAAO,CAAC,2BAA2B,CAAC,CAkCtC"}
|