Skip to content
AlphaToolBox3D is in early alpha: features are incomplete and things can break. Use it at your own risk and keep copies of your files. Report a problem
ToolBox3D

Learn · Scripting · Lesson 1 of 3

Build a fence with Python

In about ten minutes you will write a small tool that builds a fence of posts and rails, places every piece, groups the result and undoes as a single step. It uses the same operations as the editor's buttons.

  • 10 minutes
  • Beginner
  • Browser or Windows
Illustration

1. Open the Script Editor

  1. Open the editor and choose Windows › Script Editor (Ctrl+Alt+E).
  2. Start a new script tab. Everything you write begins with one import:
    import toolbox3d as tb
    
    COUNT = 10
    SPACING = 0.5  # scene units between posts
    
    tb.scene.set_editor("model")

Creating geometry belongs to the Model editor, so the script switches to it first. tb.scene.set_editor uses the same switch as the editor switcher: your work in the current editor is handed over before the Model editor opens. If that hand-over needs a decision from you, the script stops with a NotApplicable error instead of guessing.

2. Make the posts

Every button in ToolBox3D is a registered operation, and tb.ops exposes all of them. tb.ops.create.cube takes keyword arguments and returns a result whose created list holds the new objects.

parts = []
for i in range(COUNT):
    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 * SPACING, 0.5, 0)
    parts.append(post)

Setting post.transform.position is not a raw memory write: the handle runs the registered scene.set_transform operation, so the change is validated and undoes like any edit made in the viewport. Look up any operation's arguments in the API reference.

3. Add the rails

length = (COUNT - 1) * SPACING
for n, y in enumerate((0.35, 0.8)):
    rail = tb.ops.create.cube(name=f"Rail{n + 1}", width=length, height=0.06, depth=0.04).created[0]
    rail.transform.position = (length / 2, y, 0)
    parts.append(rail)

Each rail spans the whole fence and sits at the middle of it. Lengths are in scene units, the same units the channel box shows.

4. One undo step

A script run is already one undo step, and it rolls back if the script fails. Wrapping the build in a named group makes that step read clearly in the history and keeps it atomic inside a larger script:

with tb.undo.group("Build fence"):
    parts = []
    # … the post and rail loops from steps 2 and 3 …

If anything inside the block raises, everything the block did is rolled back and the error is shown in the output console with a plain-language message.

5. Group and report

Still inside the with block, group the pieces so the fence moves as one object, then tell the artist the tool has finished:

    tb.ops.scene.group(name="Fence", nodes=parts)
tb.ui.message(f"Fence with {COUNT} posts built", level="success")

Press Run. Twelve pieces appear under one Fence group in the outliner; one Ctrl+Z removes them all. Change COUNT and SPACING and run it again.

Shortcut: this exact script ships as 02_fence.py under Help › Script Examples in the Script Editor, and on the examples page.

Next steps