Skip to content

Examples

These scripts ship with Modlumi. On Windows they’re in C:\Program Files\modlumi\scripting\examples, and on Linux in share/modlumi/examples inside the installation, together with a tsconfig.json that type-checks them as described in Getting started. Run them as they are, or paste them into the script console.

Draws a one-meter square. Closing the outline creates a face.

outline.js
// Draws a one-meter square; closing the outline creates a face.
const model = modlumi.activeModel;
model.operation("Draw outline", () => {
model.entities.addEdges([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0]]);
});
console.log(model.entities.length);

Lists the root entities, component definitions and materials of the open model. Run it against an existing file:

Terminal window
modlumi --background model.skp --run-script inspect.js
inspect.js
// Lists the root entities, definitions, and materials of the open model.
const model = modlumi.activeModel;
for (const entity of model.entities) {
console.log(entity.type, entity.entityId);
switch (entity.type) {
case "Edge":
console.log(" length:", entity.length, "m");
break;
case "Face":
console.log(" area:", entity.area, "m²");
break;
}
}
for (const definition of model.definitions) {
console.log("Definition:", definition.name, definition.entities.length);
}
for (const material of model.materials) {
console.log("Material:", material.name, material.color);
}

Extrudes a box and saves a PNG from a fixed camera. See Image export for where this works.

box.js
// Extrudes a box and exports a PNG from a fixed camera (Linux builds with headless rendering).
const { Camera } = modlumi;
const model = modlumi.activeModel;
model.operation("Box", () => {
model.entities.addEdges([[0, 0, 0], [4, 0, 0], [4, 3, 0], [0, 3, 0], [0, 0, 0]]);
const [face] = model.entities.ofType("Face");
face.extrude(2);
});
const view = model.activeView;
view.camera = new Camera([5, -7, 5.5], [2, 1.5, 1], { perspective: false, fieldOfView: 35 });
view.writeImage("box.png", { width: 640, height: 480 });