Examples
These scripts ship with Modlumi for Windows, in
C:\Program Files\modlumi\scripting\examples, 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.
Draw an outline
Section titled “Draw an outline”Draws a one-meter square. Closing the outline creates a face.
// Draws a one-meter square with edges, then fills it; edges alone make no face.const model = modlumi.activeModel;model.operation("Draw outline", () => { const [side] = model.entities.addEdges([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0]]); model.entities.findFaces(side);});console.log(model.entities.length);Build from solids
Section titled “Build from solids”Builds a table from a box and four cylinders. Each solid is its own group.
// Builds a table from solids: a box for the top and four cylinders for the legs.const model = modlumi.activeModel;const width = 1.6;const depth = 0.8;const height = 0.72;const inset = 0.1;model.operation("Table", () => { const entities = model.entities; entities.addBox([0, 0, height], width, depth, 0.04).name = "Top"; for (const x of [inset, width - inset]) { for (const y of [inset, depth - inset]) { entities.addCylinder([x, y, 0], 0.03, height).name = "Leg"; } }});console.log(model.entities.ofType("Group").length);Inspect a model
Section titled “Inspect a model”Lists the root entities, component definitions and materials of the open model. Run it against an existing file:
modlumi --background model.skp --run-script 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);}Save views as scenes
Section titled “Save views as scenes”Adds scenes that look at the whole model from the front, from its four corners and from above, as one undo step. Click them in the Scenes panel to look around.
// Adds scenes that look at the whole model from the front, the four corners and above, in one// undo step, and lists every scene with where its camera stands.const model = modlumi.activeModel;const bounds = model.entities.bounds;if (bounds === null) { throw new Error("The model is empty");}const center = bounds.center;const distance = bounds.diagonal * 1.5;// Where each camera stands as seen from the center, scaled to the distance.const views = [ { name: "Front", direction: new modlumi.Vector3d(0, -1, 0.3) }, { name: "Front left", direction: new modlumi.Vector3d(-1, -1, 0.6) }, { name: "Front right", direction: new modlumi.Vector3d(1, -1, 0.6) }, { name: "Back left", direction: new modlumi.Vector3d(-1, 1, 0.6) }, { name: "Back right", direction: new modlumi.Vector3d(1, 1, 0.6) },];model.operation("Add views", () => { for (const { name, direction } of views) { const eye = center.offset(direction.normalize().scale(distance)); model.addScene(name, new modlumi.Camera(eye, center, { fieldOfView: 35 })); } // Looking straight down needs another up direction, and shows the plan without perspective. const above = center.offset(new modlumi.Vector3d(0, 0, distance)); model.addScene("Plan", new modlumi.Camera(above, center, { up: [0, 1, 0], perspective: false }));});for (const scene of model.scenes) { console.log(`${scene.name}: ${scene.camera.perspective ? "perspective" : "parallel"} from ${scene.camera.eye}`);}Export an image
Section titled “Export an image”Extrudes a box and saves a PNG from a fixed camera. See Image export for where this works.
// Extrudes a box and exports a PNG from a fixed camera.const { Camera } = modlumi;const model = modlumi.activeModel;model.operation("Box", () => { model.entities.addRectangle([0, 0, 0], 4, 3).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 });More recipes
Section titled “More recipes”- Walk every group and component with world coordinates.
- Select faces by area.
- Build a box by extruding a rectangle.
- Draw on a wall with an upright rectangle.
- Sweep a profile along a path.
- Make a radial array.
- Render a model from four sides.
- Read complete plugins: the modlumi-plugins repository on GitHub has the source of the plugins that come with Modlumi and of the example plugins.