Docs · Scripting
Python scripting
Every tool in ToolBox3D is a registered operation, and every operation can be called from Python. The same import toolbox3d as tb scripts run in the browser editor, in the Windows app and headless with tbpy.
Your first script
- Open the editor and choose Windows › Script Editor (Ctrl+Alt+E). It opens beside the viewport, with the output console above the code.
- Create a new tab and type a short script:
import toolbox3d as tb for obj in tb.scene.objects(): print(obj.path, obj.type) - Press Run. The output console lists every object in the scene. In the browser, the first run downloads Python (Pyodide) once; later runs start straight away.
- Open Help › Script Examples in the Script Editor for ready-made scripts, or browse them on the examples page.
While you work in the editor, the console echoes the Python line for each action you perform, so you can copy a step you did by hand into a script.
The toolbox3d package
Scripts address your data — objects, meshes, materials — not editor panels. Handles are light references that read from the current scene and write through registered operations, so a property change undoes like the same change made in the UI.
import toolbox3d as tb
crate = tb.scene.get("Cube") # by name, or its outliner path ("Props/Cube")
crate.name = "Crate" # renames through an operation
crate.transform.position = (0, 1, 0) # scene units
print(crate.bbox.size, crate.mesh.vertex_count)
positions = crate.mesh.positions() # numpy (N, 3) array
crate.mesh.set_positions(positions * 1.1)
| Module | What it holds |
|---|---|
tb.scene | Objects, lookup by name or id, the selection, materials and the active editor. |
tb.ops | Every registered operation, generated from the app's registry. |
tb.types | The handles: Object, Mesh, Material and the Result an operation returns. |
tb.undo | Undo groups, undo and redo. |
tb.io | Open, import, export and save files. |
tb.ui | Messages and simple questions for the artist running the script. |
tb.app, tb.project, tb.math | Where the script runs, scripts saved in the project, and vector maths. |
Operations
Operation mesh.bevel becomes tb.ops.mesh.bevel(...). Arguments are keyword-only and snake_case; lengths are in scene units, angles in degrees and colours are linear RGB(A) floats. Without targets=, an operation works on the current selection, just like its button.
result = tb.ops.create.cube(name="Post", width=0.1, height=1.0, depth=0.1)
post = result.created[0]
tb.ops.describe("create.cube") # the full schema: arguments, units, ranges, editors
tb.ops.list("mesh.") # every operation whose id starts with mesh.
The API reference lists every operation with its arguments and defaults. It is generated from the same registry that builds the Script Editor's completion, so it always matches the editor.
Undo and errors
A whole script run is one undo step, and it is rolled back if the script raises. Group parts of a script into their own steps with tb.undo.group:
with tb.undo.group("Build fence"):
for i in range(10):
post = tb.ops.create.cube(name=f"Post{i + 1}", width=0.08, height=1.0, depth=0.08).created[0]
post.transform.position = (i * 0.5, 0.5, 0)
Errors are tb.ToolBox3DError subclasses with a plain-language message: InvalidParams, NotApplicable (for example, nothing selected), Ownership, Cancelled, Stale, AmbiguousName, NotFound and HostCapability (something the current host cannot do, such as a dialog under tbpy). Stop in the Script Editor cancels a running script and rolls back its changes.
Files and project scripts
tb.io.open, tb.io.import_file, tb.io.export and tb.io.save run the same readers and writers as the File menu. In the browser they work on Python's in-memory files; on Windows and under tbpy they read and write your disk.
Scripts saved under Project Scripts in the Script Editor travel inside the .tb3d project file, so a rig or asset can carry the tools that build it. Read and write them from Python with tb.project.scripts.
Batch runs with tbpy
tbpy is ToolBox3D's headless Python for the Windows app, like mayapy or hython: the same API against a ToolBox3D session without a window. It finds Python 3.12 or newer through TOOLBOX3D_PYTHON, the bundled interpreter, the py launcher or your PATH.
tbpy convert.py models/*.obj # run a script with arguments
tbpy -c "import toolbox3d as tb; print(tb.app.is_headless)"
tbpy # interactive prompt
The exit status is the script's: 1 for an uncaught exception, 2 for a usage error and 3 when Python or the session could not start.
Reference
- Python API reference — the package modules and every operation's Python signature.
- Script examples — the scripts shipped in the Script Editor's Examples folder.
- OpenSCAD lessons — parametric 3D-print parts in
.scadcode for the OpenRSCAD Editor (BOSL2 built in). - Operation reference — every action by editor, searchable, for artists.