Scripting
Scripts build and change designs with JavaScript. Use them to make a part from a few numbers, generate a family of sizes, run a study over a parameter, or export many variants at once.
Running a script
Open Insert ▸ more ▸ Scripts (or search for "Scripts" with Ctrl K). Write or pick a script and press Run (Ctrl Enter). Output from r3d.log appears under the editor, with the line number of any error. Save in design keeps the script with the project.
A whole run is one undo step: Ctrl Z takes back everything it did.
Record macro turns what you do into a script: while it records, every feature you add is noted; Stop recording opens a script that adds them again, with their settings as you left them. Features the macro makes refer to each other through variables, so running it again makes a fresh set; faces and edges are picked where you clicked, so a macro suits this design or one built the same way. Save it in the design to keep it.
A first script
// a 120 × 80 plate, 8 mm thick, with eight counterbored M5 holes
r3d.param('t', 8);
const s = r3d.sketch('XY', (sk) => sk.centerRect(0, 0, 120, 80));
const plate = r3d.extrude(s, 't'); // returns the new body's id
await r3d.rebuild();
const pts = [];
for (const x of [-45, -15, 15, 45]) for (const y of [-25, 25]) pts.push([x, y, 8]);
r3d.hole(plate, pts, [0, 0, 1], { standard: 'cbore', size: 'M5', through: true });
await r3d.rebuild();
r3d.log('Mass:', (await r3d.properties(plate)).mass.toFixed(1), 'g');
Scripts are async: use await before r3d.rebuild(), r3d.properties() and r3d.export(). The model also rebuilds by itself when the script ends.
The r3d API
| Call | What it does |
|---|---|
r3d.param(name)r3d.param(name, value) | Reads a parameter, or creates or changes it. Values can be numbers or expressions such as 'width / 2'. |
r3d.sketch(plane, build, name?) | Adds a sketch on 'XY', 'XZ' or 'YZ', or on a plane { origin, xDir, normal }. build receives a sketch builder. Returns the sketch id. |
r3d.extrude(sketch, distance, options?, name?) | Extrudes every closed region of a sketch. Options are the Extrude fields, for example { side: 'symmetric', op: 'cut', target: bodyId }. Returns the feature id, which is also the new body's id. |
r3d.revolve(sketch, options?, name?) | Revolves a sketch, for example { axis: 'sy', angle: 360 }. |
r3d.box(params), r3d.cylinder(params) | Primitives: { w, d, h, x, y, z } and { r, h, axis, x, y, z }. Returns the body id. |
r3d.hole(body, points, normal, options?) | Holes at points [[x, y, z], …] on a face with the given normal. Options: { standard: 'clearance' | 'cbore' | 'csink' | 'tapped', size: 'M6', fit: 'normal', through: true }, or { diameter, depth } for custom holes. |
r3d.feature(type, params, name?) | Any timeline feature by its type and fields (the same names as in its dialog). See feature types. |
await r3d.rebuild() | Rebuilds now. Returns { bodies, errors }, with errors by feature name. |
r3d.bodies() | The bodies: [{ id, name, material }]. |
await r3d.properties(id) | Mass properties: { volume, area, mass, centerOfMass, bbox } in mm, mm², mm³ and grams. |
r3d.material(id, key) | Sets a body's material, for example 'steel', 'aluminum', 'abs', 'pla', 'brass' or 'oak'. |
r3d.configure(name) | Switches to a configuration by name, or null for Default. |
await r3d.export(format, ids?) | Downloads bodies as 'step', 'stl' or '3mf' (all visible bodies if no ids are given). |
r3d.log(…), console.log(…) | Writes to the output panel. Objects are shown as JSON. |
r3d.units | The design's units, such as 'mm'. |
Sketch builder
Inside r3d.sketch, the builder draws in sketch coordinates. Calls can be chained, and points shared by curves are joined automatically.
line(x1, y1, x2, y2) | A line. |
polyline([[x, y], …], closed?) | Connected lines. |
rect(x1, y1, x2, y2), centerRect(cx, cy, w, h) | Rectangles. |
circle(cx, cy, r) | A circle. |
ellipse(cx, cy, rx, ry, rotation?) | An ellipse with radius rx along its major axis and ry across it, turned rotation degrees from X. |
arc(cx, cy, r, a0, a1) | An arc, counter-clockwise from angle a0 to a1 in degrees. |
polygon(cx, cy, r, n, rotation?) | A regular polygon with n sides on a circle of radius r. |
slot(x1, y1, x2, y2, width) | A slot between two centres. |
spline([[x, y], …], closed?) | A smooth curve through points. |
Feature types
r3d.feature accepts every feature in the app. The most used are extrude, revolve, sweep, loft, hole, fillet, chamfer, shell, combine, rpattern, cpattern, ppattern, mirror, move, thread, fastener, plane, sheetbase and flange. Their fields have the same names as in the feature dialogs. For example, a rectangular pattern:
r3d.feature('rpattern', { bodies: [boss], dir1: 'X', count1: 4, spacing1: 25, dir2: 'Y', count2: 2, spacing2: 40 });
Features that pick faces or edges take references like the ones the app stores, for example { body, p: [x, y, z], n: [nx, ny, nz] } for a face: a point on it and its outward normal.
Tips
- Drive dimensions from parameters (
r3d.param) and pass the parameter's name instead of a number. The design then stays editable by hand after the script has run. - Use the ids that features return instead of
r3d.bodies()[0], so a script works in a design that already has other bodies. - To test a range of values, change a parameter,
await r3d.rebuild(), then readr3d.properties, as in the "Mass for a range of sizes" example. - Scripts are plain JavaScript, so loops, functions,
Mathand arrays all work.