@pascal-app/geometry-script 0.0.0-stage → 1.0.3
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/compile.test.d.ts +2 -0
- package/dist/compile.test.d.ts.map +1 -0
- package/dist/compile.test.js +199 -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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile.test.d.ts","sourceRoot":"","sources":["../src/compile.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { matchScriptSlotsToLibrary, scriptSource } from '@pascal-app/core';
|
|
3
|
+
import { addObject, applySceneChanges, editedScriptParams } from '@pascal-app/core/agent-operations';
|
|
4
|
+
import { GeometryArtifactManifest, ItemNode, LevelNode } from '@pascal-app/core/schema';
|
|
5
|
+
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
|
|
6
|
+
import { compileGeometryScript } from './index.js';
|
|
7
|
+
const SASH = `
|
|
8
|
+
import * as THREE from 'three'
|
|
9
|
+
export const mount = 'wall'
|
|
10
|
+
export default function build() {
|
|
11
|
+
const g = new THREE.Group()
|
|
12
|
+
const sash = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 0.05), new THREE.MeshStandardMaterial())
|
|
13
|
+
sash.name = 'sash'
|
|
14
|
+
g.add(sash)
|
|
15
|
+
const times = [0, 1]
|
|
16
|
+
g.animations = [new THREE.AnimationClip('open', 1, [new THREE.VectorKeyframeTrack('sash.position', times, [0, 0, 0, 0, 0.7, 0])])]
|
|
17
|
+
return g
|
|
18
|
+
}
|
|
19
|
+
`;
|
|
20
|
+
describe('clips', () => {
|
|
21
|
+
test('a clip is stored ending on its last pose, not wrapped to its first', async () => {
|
|
22
|
+
const { glb } = await compileGeometryScript({ code: SASH });
|
|
23
|
+
const gltf = await new GLTFLoader().parseAsync(glb, '');
|
|
24
|
+
const track = gltf.animations[0].tracks.find((t) => t.name.endsWith('.position'));
|
|
25
|
+
const start = track.values[1];
|
|
26
|
+
const end = track.values[track.values.length - 2];
|
|
27
|
+
expect(end - start).toBeCloseTo(0.7, 3);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
test('an edit keeps the current param values the new code still accepts', async () => {
|
|
31
|
+
const module = (params) => `export const params = ${params}
|
|
32
|
+
export default function build({ THREE }) {
|
|
33
|
+
const g = new THREE.Group(); g.add(new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1))); return g
|
|
34
|
+
}`;
|
|
35
|
+
const before = await compileGeometryScript({
|
|
36
|
+
code: module(`{ width: { default: 5.2, min: 3, max: 8 }, depth: 2.4, height: 2.75, finish: 'pine', rail: 1, gone: 9 }`),
|
|
37
|
+
params: { width: 6.3, depth: 1.7, height: 2.52, finish: 'walnut', rail: 0, gone: 4 },
|
|
38
|
+
});
|
|
39
|
+
const node = { type: 'item', source: scriptSource(before) };
|
|
40
|
+
const after = await compileGeometryScript({
|
|
41
|
+
code: module(`{
|
|
42
|
+
width: { default: 5.2, min: 3, max: 8 },
|
|
43
|
+
depth: { default: 2.4, min: 2, max: 4 },
|
|
44
|
+
height: 2.75,
|
|
45
|
+
finish: { default: 'oak', options: ['oak', 'ash'] },
|
|
46
|
+
rail: true,
|
|
47
|
+
lit: false,
|
|
48
|
+
}`),
|
|
49
|
+
params: editedScriptParams(node, { height: 3 }),
|
|
50
|
+
});
|
|
51
|
+
expect(after.params).toEqual({
|
|
52
|
+
width: 6.3,
|
|
53
|
+
depth: 2,
|
|
54
|
+
height: 3,
|
|
55
|
+
finish: 'oak',
|
|
56
|
+
rail: true,
|
|
57
|
+
lit: false,
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
test('building and adding a scripted item assigns Pascal finishes and rebuilding keeps paint', async () => {
|
|
61
|
+
const compiled = await compileGeometryScript({
|
|
62
|
+
code: `export default function build({ THREE }) {
|
|
63
|
+
const group = new THREE.Group();
|
|
64
|
+
const samples = [
|
|
65
|
+
['slot_oak', '#a77440', 0, 1, false],
|
|
66
|
+
['slot_hardware', '#b08d57', 0.9, 1, false],
|
|
67
|
+
['glass', '#87ceeb', 0, 0.3, true],
|
|
68
|
+
['slot_paint', '#eae6de', 0, 1, false],
|
|
69
|
+
['slot_unknown', '#ff00ff', 0, 1, false],
|
|
70
|
+
['slot_oakTop', '#f3dcb5', 0, 1, false],
|
|
71
|
+
];
|
|
72
|
+
samples.forEach(([name, color, metalness, opacity, transparent], i) => {
|
|
73
|
+
const material = new THREE.MeshStandardMaterial({ color, metalness, opacity, transparent, roughness: 0.7 });
|
|
74
|
+
material.name = name;
|
|
75
|
+
const mesh = new THREE.Mesh(new THREE.BoxGeometry(0.1, 0.2, 0.1), material);
|
|
76
|
+
mesh.position.x = i * 0.2; group.add(mesh);
|
|
77
|
+
});
|
|
78
|
+
const bulb = new THREE.MeshStandardMaterial({color: '#ffffff', emissive: '#ffffff'});
|
|
79
|
+
bulb.name = 'slot_bulb'; group.add(new THREE.Mesh(new THREE.BoxGeometry(0.1, 0.1, 0.1), bulb));
|
|
80
|
+
return group;
|
|
81
|
+
}`,
|
|
82
|
+
});
|
|
83
|
+
const level = LevelNode.parse({ id: 'level_material_test' });
|
|
84
|
+
const initial = { [level.id]: level };
|
|
85
|
+
const context = { activeLevelId: level.id };
|
|
86
|
+
const added = addObject(initial, { compiled, reason: 'no catalog cabinet' }, context);
|
|
87
|
+
const nodes = applySceneChanges(initial, added.changes);
|
|
88
|
+
const node = ItemNode.parse(nodes[added.result.nodeId]);
|
|
89
|
+
expect(node.slots).toEqual({
|
|
90
|
+
oak: 'library:wood-finewood27',
|
|
91
|
+
hardware: 'library:metal-brass',
|
|
92
|
+
glass: 'library:preset-glass',
|
|
93
|
+
paint: 'library:preset-softwhite',
|
|
94
|
+
oaktop: 'library:wood-finewood27',
|
|
95
|
+
});
|
|
96
|
+
expect(node.source.manifest.slots.find((slot) => slot.id === 'hardware')).toMatchObject({
|
|
97
|
+
color: '#b08d57',
|
|
98
|
+
metalness: 0.9,
|
|
99
|
+
roughness: 0.7,
|
|
100
|
+
});
|
|
101
|
+
expect(node.source.manifest.slots.find((slot) => slot.id === 'unknown')?.color).toBe('#ff00ff');
|
|
102
|
+
expect(node.source.manifest.slots.find((slot) => slot.id === 'bulb')?.emissive).toBe(true);
|
|
103
|
+
const artifact = await new GLTFLoader().parseAsync(compiled.glb, '');
|
|
104
|
+
expect(artifact.parser.json.materials.some((material) => material.name === 'slot_glass')).toBe(true);
|
|
105
|
+
for (const paint of ['scene:mtl_user', 'library:preset-white', '#123456']) {
|
|
106
|
+
const painted = ItemNode.parse({ ...node, slots: { ...node.slots, oak: paint } });
|
|
107
|
+
const rebuilt = addObject({ ...nodes, [node.id]: painted }, { compiled, nodeId: node.id }, context);
|
|
108
|
+
const after = applySceneChanges({ ...nodes, [node.id]: painted }, rebuilt.changes);
|
|
109
|
+
expect(ItemNode.parse(JSON.parse(JSON.stringify(after[node.id]))).slots).toEqual({
|
|
110
|
+
...node.slots,
|
|
111
|
+
oak: paint,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
test('conflicting materials in one slot do not invent a matching tone', async () => {
|
|
116
|
+
const { manifest } = await compileGeometryScript({
|
|
117
|
+
code: `export default function build({ THREE }) {
|
|
118
|
+
const group = new THREE.Group();
|
|
119
|
+
for (const color of ['#a77440', '#3e220d']) {
|
|
120
|
+
const material = new THREE.MeshStandardMaterial({ color }); material.name = 'slot_body';
|
|
121
|
+
group.add(new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), material));
|
|
122
|
+
}
|
|
123
|
+
return group;
|
|
124
|
+
}`,
|
|
125
|
+
});
|
|
126
|
+
expect(manifest.slots).toHaveLength(1);
|
|
127
|
+
expect(manifest.slots[0].color).toBeUndefined();
|
|
128
|
+
});
|
|
129
|
+
test('only see-through materials hint glass', async () => {
|
|
130
|
+
const { manifest } = await compileGeometryScript({
|
|
131
|
+
code: `export default function build({ THREE }) {
|
|
132
|
+
const group = new THREE.Group();
|
|
133
|
+
const materials = [
|
|
134
|
+
new THREE.MeshStandardMaterial({ color: '#fff8e7', transparent: true, opacity: 0.85 }),
|
|
135
|
+
new THREE.MeshStandardMaterial({ color: '#2e7d32', transparent: true }),
|
|
136
|
+
new THREE.MeshStandardMaterial({ color: '#ffffff', transparent: true, opacity: 0.3 }),
|
|
137
|
+
new THREE.MeshPhysicalMaterial({ color: '#ffffff', transmission: 1 }),
|
|
138
|
+
];
|
|
139
|
+
['slot_shade', 'slot_leaves', 'slot_pane', 'slot_lens'].forEach((name, i) => {
|
|
140
|
+
materials[i].name = name;
|
|
141
|
+
group.add(new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), materials[i]));
|
|
142
|
+
});
|
|
143
|
+
return group;
|
|
144
|
+
}`,
|
|
145
|
+
});
|
|
146
|
+
expect(matchScriptSlotsToLibrary(manifest)).toMatchObject({
|
|
147
|
+
pane: 'library:preset-glass',
|
|
148
|
+
lens: 'library:preset-glass',
|
|
149
|
+
});
|
|
150
|
+
expect(matchScriptSlotsToLibrary(manifest).shade).not.toBe('library:preset-glass');
|
|
151
|
+
expect(matchScriptSlotsToLibrary(manifest).leaves).not.toBe('library:preset-glass');
|
|
152
|
+
});
|
|
153
|
+
test('cutter footprints retain concavity and host kind while old manifests still load', async () => {
|
|
154
|
+
const { manifest } = await compileGeometryScript({
|
|
155
|
+
code: `export default function build({ THREE }) {
|
|
156
|
+
const g = new THREE.Group();
|
|
157
|
+
g.add(new THREE.Mesh(new THREE.BoxGeometry(4, 1, 4), new THREE.MeshStandardMaterial()));
|
|
158
|
+
const shape = new THREE.Shape(); shape.moveTo(0,0); shape.lineTo(2,0); shape.lineTo(2,1); shape.lineTo(1,1); shape.lineTo(1,2); shape.lineTo(0,2); shape.closePath();
|
|
159
|
+
const geometry = new THREE.ExtrudeGeometry(shape, {depth:2, bevelEnabled:false}); geometry.rotateX(-Math.PI/2);
|
|
160
|
+
const cutter = new THREE.Mesh(geometry); cutter.name='cut:slab'; cutter.position.y=-1; g.add(cutter); return g;
|
|
161
|
+
}`,
|
|
162
|
+
});
|
|
163
|
+
const parsed = GeometryArtifactManifest.parse(manifest);
|
|
164
|
+
expect(parsed.cutters).toHaveLength(1);
|
|
165
|
+
expect(parsed.cutters[0].host).toBe('slab');
|
|
166
|
+
const ring = parsed.cutters[0].polygon;
|
|
167
|
+
const area = Math.abs(ring.reduce((sum, p, i) => {
|
|
168
|
+
const q = ring[(i + 1) % ring.length];
|
|
169
|
+
return sum + p[0] * q[1] - q[0] * p[1];
|
|
170
|
+
}, 0)) / 2;
|
|
171
|
+
expect(area).toBeCloseTo(3);
|
|
172
|
+
expect(ring).toHaveLength(6);
|
|
173
|
+
const { cutters, ...legacy } = manifest;
|
|
174
|
+
expect(GeometryArtifactManifest.parse(legacy).cutters).toBeUndefined();
|
|
175
|
+
});
|
|
176
|
+
test('only slab and ceiling cutter outlines are stored while wall cutters stay in the artifact', async () => {
|
|
177
|
+
for (const [mount, name, host] of [
|
|
178
|
+
['floor', 'cut:wall', undefined],
|
|
179
|
+
['wall', 'cutout', undefined],
|
|
180
|
+
['wall-side', 'cutout', undefined],
|
|
181
|
+
['floor', 'cutout', 'mounted'],
|
|
182
|
+
['ceiling', 'cutout', 'mounted'],
|
|
183
|
+
['ceiling', 'cut:ceiling', 'ceiling'],
|
|
184
|
+
]) {
|
|
185
|
+
const { manifest, glb } = await compileGeometryScript({
|
|
186
|
+
code: `export const mount = '${mount}';
|
|
187
|
+
export default function build({ THREE }) {
|
|
188
|
+
const group = new THREE.Group();
|
|
189
|
+
group.add(new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1)));
|
|
190
|
+
const cutter = new THREE.Mesh(new THREE.BoxGeometry(0.5, 2, 0.5));
|
|
191
|
+
cutter.name = '${name}'; group.add(cutter); return group;
|
|
192
|
+
}`,
|
|
193
|
+
});
|
|
194
|
+
expect(manifest.cutout).toBe(true);
|
|
195
|
+
expect(manifest.cutters?.map((cutter) => cutter.host)).toEqual(host ? [host] : []);
|
|
196
|
+
const artifact = await new GLTFLoader().parseAsync(glb, '');
|
|
197
|
+
expect(artifact.parser.json.nodes.some((node) => node.name === name)).toBe(true);
|
|
198
|
+
}
|
|
199
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,KAAK,0BAA0B,EAC/B,KAAK,2BAA2B,EAChC,KAAK,mBAAmB,GACzB,MAAM,WAAW,CAAA;AAClB,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sandbox-window.d.ts","sourceRoot":"","sources":["../src/sandbox-window.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sandbox-worker.d.ts","sourceRoot":"","sources":["../src/sandbox-worker.ts"],"names":[],"mappings":"AAQA,OAAO,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/// <reference lib="webworker" />
|
|
2
|
+
// The compile worker that runs inside the editor's opaque-origin sandbox frame.
|
|
3
|
+
// Built into one self-contained classic script (scripts/build-sandbox.ts): the
|
|
4
|
+
// frame receives it as text and starts it from a blob (an opaque origin cannot
|
|
5
|
+
// start a module worker), under the frame's CSP (no
|
|
6
|
+
// network, fonts, images or nested frames). Removing ambient capabilities
|
|
7
|
+
// below is defence in depth on top of that boundary.
|
|
8
|
+
import './sandbox-window.js';
|
|
9
|
+
import { compileGeometryScript } from './compile.js';
|
|
10
|
+
const scope = globalThis;
|
|
11
|
+
const BLOCKED = [
|
|
12
|
+
'fetch',
|
|
13
|
+
'XMLHttpRequest',
|
|
14
|
+
'WebSocket',
|
|
15
|
+
'WebTransport',
|
|
16
|
+
'EventSource',
|
|
17
|
+
'importScripts',
|
|
18
|
+
'indexedDB',
|
|
19
|
+
'caches',
|
|
20
|
+
'Worker',
|
|
21
|
+
'SharedWorker',
|
|
22
|
+
'BroadcastChannel',
|
|
23
|
+
'Request',
|
|
24
|
+
'Response',
|
|
25
|
+
'FontFace',
|
|
26
|
+
];
|
|
27
|
+
const post = self.postMessage.bind(self);
|
|
28
|
+
for (const name of BLOCKED) {
|
|
29
|
+
for (let target = scope; target; target = Object.getPrototypeOf(target)) {
|
|
30
|
+
if (Object.hasOwn(target, name)) {
|
|
31
|
+
try {
|
|
32
|
+
Object.defineProperty(target, name, {
|
|
33
|
+
value: undefined,
|
|
34
|
+
configurable: false,
|
|
35
|
+
writable: false,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
catch { }
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
self.addEventListener('message', async (event) => {
|
|
43
|
+
const { id, code, params } = event.data;
|
|
44
|
+
try {
|
|
45
|
+
const output = await compileGeometryScript({ code, params });
|
|
46
|
+
post({ id, ok: true, output }, [output.glb]);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
post({ id, ok: false, error: error instanceof Error ? error.message : String(error) });
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
post({ ready: true });
|