Turtle (3-D)

Pure-Python port of BOSL2’s turtle3d.scad as a Turtle3D class. A turtle walks through 3-D space carrying an orientation frame; a flat list of commands drives it, and the result is either the list of points it visited (points()) or a list of 4x4 transforms (transforms()) ready to sweep a profile with path_sweep() / sweep().

The full simple command set is ported — moves (move/jump/xmove …), relative turns (left/right/up/down/roll), absolute turns (xrot/yrot/zrot/rot/ setdir), arcs (arcleft/arcright/arcup/arcdown/arcxrot …/arctodir/ arcrot), the length/angle/scale/arcsteps settings, and repeat. Compound commands are also supported — a single nested list starting with "move" or "arc" that applies several effects at once (["move", 40, "grow", 2, "twist", 180, "steps", 40]), with grow/shrink scaling the swept profile, twist rotating it, roll/rollto rolling the frame, and, for arc, relative or absolute rotation.

class pybosl2.turtle.turtle3d.Turtle3D(state=Anchor.RIGHT)[source]

Bases: object

A 3-D turtle: walk it with a command list to produce a path or a list of sweep transforms.

The turtle starts at the origin pointing in state (default RIGHT = +X), with “up” along +Z. Commands are a flat list of TurtleCommand objects. Turns: left/right (about up), up/down (about side), roll (about heading), and absolute xrot/yrot/zrot. Arcs: arcleft/arcright/arcup/arcdown/arcxrot/arcyrot/arczrot. move/jump translate; length/angle/scale/arcsteps set defaults; repeat repeats. A compound TurtleCommand (is_compound=True) applies several effects at once (grow/shrink/twist/roll/steps/reverse).

Examples

A rounded square path swept into a tube:

from pybosl2.turtle import turtle3d, TurtleCommand, TurtleCommandType as Tct
from pybosl2.path3d import Path3D

sq = [[-1, -1], [1, -1], [1, 1], [-1, 1]]
path = turtle3d([
    TurtleCommand(Tct.MOVE, size=20),
    TurtleCommand(Tct.ARCLEFT, radius=3),
    TurtleCommand(Tct.MOVE, size=20),
    TurtleCommand(Tct.ARCLEFT, radius=3),
    TurtleCommand(Tct.MOVE, size=20),
    TurtleCommand(Tct.ARCLEFT, radius=3),
    TurtleCommand(Tct.MOVE, size=20),
    TurtleCommand(Tct.ARCLEFT, radius=3),
]).points()
Path3D(path).path_sweep(sq, closed=True).polyhedron().show()
Loading 3-D preview…

⬇ Download STL mesh

Parameters:
state : Any

run(commands, repeat=1)[source]

Execute commands (optionally repeat times), advancing this turtle’s state.

Returns:

self.

Parameters:
commands : Sequence[TurtleCommand]

repeat : int

Return type:

Turtle3D

points()[source]

Return the de-duplicated list of 3-D points the turtle has visited.

Return type:

list[list[float]]

stroke(width=1, cap=None, closed=None)[source]

Render the turtle’s current path as a 3-D stroked tube.

Parameters:
width : float

Stroke line width.

cap : CapType | CapSpec | None

Optional endcap style applied to both ends.

closed : bool | None

Override whether the path is treated as closed.

Returns:

A Bosl2Solid representing the tubular stroke.

Return type:

Bosl2Solid

transforms()[source]

Return the list of 4x4 transforms (position + orientation) for sweeping a profile along the path.

Return type:

list[ndarray]

full_state()[source]

Return the turtle’s internal Turtle3DState.

Return type:

Turtle3DState