Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / tools / preview.luc

import tool_picks
import preview_support
import probe
import preview_cad
import preview_viewport
import preview_poly
import preview_select
import preview_drag
import preview_bevel
import preview_shelf
import preview_modify
import preview_create
import preview_shape
import preview_held
import preview_deform
import preview_icons
from view_projection import project
import time
from luce_ui.ui import Event
from luce_window.input import EventKind, Key
from app import Luce3D
from luce_3d.graphics import Vector3, Matrix4, Group, AmbientLight, DirectionalLight
from geometry_data import GeometryData
from overlay_lines import OverlayLines
from scene_projection import append_scene, append_overlays
import modeling_commands
import node_commands
import sketch_commands
import selection_commands
import take_commands
import script_node
from luce_cad.cad_geometry import CadGeometry
from luce_cad.modeling import CadModeling
from luce_cad.sketch import CadSketch
from luce_std import math
import tool_prompts
import network_wires
from workspace import Workspace
from node import Node

pub func main(arguments: list[str]) -> int!:
    let editor = Luce3D(width = int(arguments[15]) if arguments.length > 16 else 2200, height = int(arguments[16]) if arguments.length > 16 else 1400)
    if arguments.length > 1 and arguments[1] == "freeze_flags":
        let w = editor.workspace
        node_commands.toggle_freeze(w)
        node_commands.add_node(w, "Transform", 40.0, 130.0)
        node_commands.toggle_enabled(w)
        node_commands.add_node(w, "Null", 40.0, 230.0)
        editor.panels.refresh()
    if arguments.length > 17 and arguments[17] == "viewport":
        for id in ["parameters", "outliner", "network", "geometry_spreadsheet"]:
            let panel = editor.panels.dock.find(id) else trap("missing capture panel")
            editor.panels.dock.remove(panel)
    if arguments.length > 5 and arguments[5] == "background":
        editor.workspace.enable_compute()
        editor.wake_on_compute()
    if arguments.length > 4:
        editor.workspace.set_display_mode(int(probe.parse_value(arguments[4])))
    if arguments.length > 23:
        editor.workspace.set_cad_quality(int(probe.parse_value(arguments[23])))
    if arguments.length > 2 and arguments[1] in ["cad", "cad_wire", "analytic"]:
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "File", 40.0, 40.0)
        node_commands.set_text(w, 0, arguments[2])
        if arguments.length > 21 and probe.parse_value(arguments[21]) != 0.0:
            node_commands.set_parameter(w, 0, probe.parse_value(arguments[21]))
        if arguments.length > 6 and arguments[6] != "":
            node_commands.add_node(w, "Blast", 220.0, 100.0)
            node_commands.set_text(w, 0, arguments[6])
            node_commands.set_parameter(w, 0, 1.0)
        if arguments[1] != "analytic":
            node_commands.add_node(w, "Tessellate", 40.0, 160.0)
            if arguments.length > 3:
                node_commands.set_parameter(w, 1, probe.parse_value(arguments[3]))
        node_commands.add_node(w, "Transform", 40.0, 280.0)
        node_commands.set_parameter(w, 4, 0.0 if arguments[2].lower().ends_with(".obj") else -90.0)
        if arguments.length > 11 and arguments[11] != "": node_commands.set_parameter(w, 4, probe.parse_value(arguments[11]))
        if arguments[1] == "cad_wire":
            w.set_display_mode(4)
        w.frame_cube()
        w.view.distance *= 0.75
        w.view.update_camera()
        if arguments[1] == "analytic":
            node_commands.select_node(w, w.network.local_nodes()[0].id)
            print(f"analytic preview: {w.notice}")
        editor.panels.refresh()
    # A point cloud file (--file: a 3DGS PLY draws as Gaussian splats), framed,
    # then turned by --zoom, --yaw, --pitch and --target.
    if arguments.length > 14 and arguments[1] == "splats":
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "File", 40.0, 40.0)
        node_commands.set_text(w, 0, arguments[2])
        w.frame_cube()
        w.view.distance *= probe.parse_value(arguments[8])
        w.view.yaw = probe.parse_value(arguments[9])
        w.view.pitch = probe.parse_value(arguments[10])
        if arguments[12] != "":
            w.view.target = Vector3(probe.parse_value(arguments[12]), probe.parse_value(arguments[13]), probe.parse_value(arguments[14]))
        w.view.update_camera()
        print(f"splats: {w.batches.clouds.length} clouds, {w.notice}")
        editor.panels.refresh()
    # A saved project (--file), as opened from File > Open.
    if arguments.length > 2 and arguments[1] == "project":
        editor.panels.projects.load_from(arguments[2])
    if arguments.length > 1 and arguments[1] in ["outliner", "context_menu"]:
        # The root: the seed Geo, a Geo of details moved 3 along X, and an
        # Assembly Group holding a torus Geo, a light and a camera. The
        # network view is inside the details: the rest is drawn around them.
        let w = editor.workspace
        let body = w.network.scope
        node_commands.leave_group(w)
        node_commands.rename(w, "Body")
        node_commands.add_node(w, "Group", 220.0, 70.0)
        let assembly = w.network.selected
        node_commands.rename(w, "Assembly")
        node_commands.enter_group(w, assembly)
        node_commands.add_node(w, "Geo", 40.0, 60.0)
        node_commands.rename(w, "Ring")
        node_commands.set_parameter(w, 1, -3.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Torus", 40.0, 60.0)
        node_commands.leave_group(w)
        node_commands.add_node(w, "Light", 240.0, 60.0)
        node_commands.leave_group(w)
        node_commands.add_node(w, "Geo", 430.0, 70.0)
        let details = w.network.selected
        node_commands.rename(w, "Details")
        node_commands.set_parameter(w, 1, 3.0)
        node_commands.enter_group(w, details)
        node_commands.add_node(w, "Sphere", 40.0, 60.0)
        node_commands.add_node(w, "Transform", 40.0, 160.0)
        node_commands.set_parameter(w, 2, 0.5)
        _ = body
        w.frame_cube()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] == "occlusion":
        let w = editor.workspace
        node_commands.add_node(w, "PolyBevel", 40.0, 170.0)
        node_commands.add_node(w, "Edit", 40.0, 270.0)
        selection_commands.choose_component(w, 2, false)
        modeling_commands.model(w, "Delete")
        w.frame_cube()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] == "edit":
        let w = editor.workspace
        node_commands.add_node(w, "Transform", 40.0, 200.0)
        node_commands.set_parameter(w, 5, 18.0)
        node_commands.add_node(w, "Edit", 40.0, 330.0)
        selection_commands.choose_component(w, 2, false)
        modeling_commands.extrude(w)
        selection_commands.choose_component(w, 0, false)
        modeling_commands.extrude(w)
        selection_commands.choose_component(w, 2, false)
        modeling_commands.choose_tool(w, "Move")
        selection_commands.choose_component(w, 2, false)
        w.frame_cube()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] in ["modeling", "spreadsheet"]:
        let w = editor.workspace
        node_commands.add_node(w, "AttributeRandomize", 40.0, 160.0)
        node_commands.add_node(w, "PolyBevel", 40.0, 250.0)
        node_commands.set_parameter(w, 0, 0.18)
        node_commands.add_node(w, "Edit", 40.0, 340.0)
        selection_commands.choose_component(w, 2, false)
        node_commands.set_parameter(w, 0, 0.22)
        modeling_commands.model(w, "Inset")
        selection_commands.choose_component(w, 2, false)
        node_commands.set_parameter(w, 0, 0.35)
        modeling_commands.extrude(w)
        modeling_commands.choose_tool(w, "Move")
        selection_commands.choose_component(w, 2, false)
        w.frame_cube()
        editor.panels.refresh()
        if arguments[1] == "spreadsheet":
            let panel = editor.panels.dock.find("geometry_spreadsheet") else trap("sheet")
            editor.panels.dock.select(panel)
    # The parameter pane's states: a disabled row and a tooltip, the value
    # ladder, an open menu.
    if arguments.length > 1 and arguments[1].starts_with("param_"):
        let w = editor.workspace
        let kind = "CopyTransform" if arguments[1] == "param_copy" else ("Mirror" if arguments[1] == "param_menu" else "Transform")
        node_commands.add_node(w, kind, 40.0, 200.0)
        if kind == "CopyTransform":
            node_commands.set_parameter(w, 0, 1.0)
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] == "cad_node":
        # A CAD node: a box with a sketched slot cut into its top (Extrude,
        # Cut), a hole drilled through it (Boolean, Subtract), its edges
        # chamfered.
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Solid Box", 300.0, 40.0)
        node_commands.set_parameter(w, 0, 3.0)
        node_commands.set_parameter(w, 1, 2.0)
        node_commands.set_parameter(w, 2, 2.0)
        let box = w.network.selected
        node_commands.add_node(w, "Sketch", 40.0, 40.0)
        node_commands.set_text(w, 0, sketch_commands.rectangle_text(0.0, 0.0, 1.0, 2.0))
        node_commands.add_node(w, "Extrude", 40.0, 160.0)
        let extrude = w.network.selected
        w.network.connect(box, extrude, 1)
        node_commands.set_parameter(w, 0, 2.0)
        node_commands.set_parameter(w, 1, 2.0)
        node_commands.add_node(w, "Solid Cylinder", 300.0, 160.0)
        node_commands.set_parameter(w, 0, 0.35)
        node_commands.set_parameter(w, 1, 4.0)
        node_commands.add_node(w, "Transform", 300.0, 280.0)
        node_commands.set_parameter(w, 1, 1.0)
        let pin = w.network.selected
        node_commands.select_node(w, extrude)
        node_commands.add_node(w, "Combine", 40.0, 280.0)
        w.network.connect(pin, w.network.selected, 1)
        node_commands.set_parameter(w, 0, 1.0)
        # Every straight convex edge chamfered.
        node_commands.add_node(w, "Chamfer", 40.0, 400.0)
        node_commands.set_parameter(w, 0, 0.12)
        node_commands.display_selected(w)
        w.frame_cube()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] == "cad_round":
        # Every edge of a box filleted: faces, quarter cylinders and sphere
        # corners.
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        node_commands.set_parameter(w, 0, 3.0)
        node_commands.add_node(w, "Fillet", 40.0, 160.0)
        node_commands.set_parameter(w, 0, 0.4)
        w.frame_cube()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] == "cad_corners":
        # A box's top ring and uprights filleted (ball corners at the top,
        # the uprights running out at the bottom).
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        node_commands.set_parameter(w, 0, 3.0)
        node_commands.set_parameter(w, 1, 2.0)
        node_commands.set_parameter(w, 2, 2.0)
        let box = w.network.selected
        let body = CadGeometry.model(preview_support.cooked(w, box).geometry, 0)
        let edges: list[int] = []
        for face in 0..<body.face_count():
            for use in 0..<body.face_edge_count(face):
                let a = body.face_edge_point(face, use, 0.0)
                let b = body.face_edge_point(face, use, 1.0)
                let id = body.face_edge_id(face, use)
                if ((a.y > 0.99 and b.y > 0.99) or (a.x == b.x and a.z == b.z)) and not (id in edges):
                    edges.append(id)
        node_commands.add_node(w, "Fillet", 40.0, 160.0)
        node_commands.set_parameter(w, 0, 0.5)
        node_commands.set_text(w, 0, CadModeling.edge_references(body, edges))
        w.frame_cube()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] in ["cad_sketch", "sketch_marquee"]:
        # A Sketch node edited on the ground: a 40 x 20 mm plate, a 10 mm
        # hole, the plate's width dimensioned, A held (the Create menu); or
        # boxed at the Lines level (sketch_marquee).
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Sketch", 40.0, 40.0)
        let plate = CadSketch.parse(sketch_commands.rectangle_text(20.0, -10.0, 40.0, 20.0))
        let center = plate.add_point(12.0, -10.0)
        _ = plate.add_circle(center, 5.0)
        _ = plate.constrain(13, 0, 1, value = 40.0)
        _ = plate.constrain(8, 0)
        node_commands.set_text(w, 0, plate.text())
        node_commands.display_selected(w)
        w.view.yaw = 0.0
        w.view.pitch = 1.2
        w.view.distance = 90.0
        w.view.target = Vector3(20.0, 0.0, 8.0)
        w.view.update_camera()
        editor.panels.view.sketch.editor.sync()
        if arguments[1] == "sketch_marquee":
            selection_commands.set_mode(w, 2)
        else:
            editor.panels.view.sketch.editor.selection = [9]
        editor.panels.refresh()
        if arguments[1] == "cad_sketch" and arguments.length > 10 and arguments[9] == "0.0":
            editor.app.layout(2200.0, 1400.0)
            let area = editor.panels.view.layout().bounds()
            editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + area.width * 0.6, y = area.y + area.height * 0.5))
            editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.a))
    if arguments.length > 1 and arguments[1] == "sketch_curves":
        # A sketch's free-form curves: a fit point spline, a control point
        # spline (its net is its points), an ellipse, a conic, and a blend
        # between two lines' ends held as curved as each.
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Sketch", 40.0, 40.0)
        let s = CadSketch.parse("")
        let fit: list[int] = []
        for (x, y) in [(0.0, 0.0), (10.0, 12.0), (20.0, 2.0), (30.0, 14.0), (40.0, 4.0)]:
            fit.append(s.add_point(x, y))
        _ = s.add_spline(fit, true)
        let net: list[int] = []
        for (x, y) in [(0.0, 25.0), (8.0, 40.0), (18.0, 22.0), (28.0, 42.0), (36.0, 24.0), (44.0, 38.0)]:
            net.append(s.add_point(x, y))
        _ = s.add_spline(net, false)
        _ = s.add_ellipse(s.add_point(65.0, 10.0), s.add_point(80.0, 16.0), 7.0)
        _ = s.add_conic(s.add_point(55.0, 30.0), s.add_point(85.0, 30.0), s.add_point(70.0, 48.0), 0.65)
        _ = s.add_text(s.add_point(0.0, -25.0), 12.0, "Luce CAD")
        node_commands.set_text(w, 0, s.text())
        node_commands.display_selected(w)
        w.view.yaw = 0.0
        w.view.pitch = 1.4
        w.view.distance = 130.0
        w.view.target = Vector3(42.0, 0.0, -22.0)
        w.view.update_camera()
        editor.panels.view.sketch.editor.sync()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] == "sketch_polygon":
        # Fusion's polygons: a hexagon circumscribed about its one
        # construction circle (radius 10, dimensioned) and a pentagon
        # inscribed in its own, each held by one Polygon constraint.
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Sketch", 40.0, 40.0)
        let s = CadSketch.parse("")
        for (cx, cy, n, inscribed) in [(0.0, 0.0, 6, false), (40.0, 0.0, 5, true)]:
            let center = s.add_point(cx, cy)
            let corners: list[int] = []
            let reach = 10.0 if inscribed else 10.0 / math.cos(math.pi / float(n))
            for at in 0..<n:
                let angle = math.pi * 0.5 + 2.0 * math.pi * float(at) / float(n) + (0.0 if inscribed else math.pi / float(n))
                corners.append(s.add_point(cx + reach * math.cos(angle), cy + reach * math.sin(angle)))
            let circle = s.add_polygon(center, corners, inscribed)
            _ = s.constrain(18, circle, value = 10.0)
        node_commands.set_text(w, 0, s.text())
        node_commands.display_selected(w)
        w.view.yaw = 0.0
        w.view.pitch = 1.4
        w.view.distance = 90.0
        w.view.target = Vector3(20.0, 0.0, 0.0)
        w.view.update_camera()
        editor.panels.view.sketch.editor.sync()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] in ["sketch_trim", "sketch_snap"]:
        # Two lines crossing, a spline across them, and Trim over one
        # line's arm: the piece it would take shows.
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Sketch", 40.0, 40.0)
        let s = CadSketch.parse("")
        _ = s.add_line(s.add_point(0.0, 0.0), s.add_point(40.0, 30.0))
        _ = s.add_line(s.add_point(0.0, 30.0), s.add_point(40.0, 0.0))
        let fit: list[int] = []
        for (x, y) in [(-5.0, 8.0), (15.0, 22.0), (30.0, 10.0), (45.0, 20.0)]:
            fit.append(s.add_point(x, y))
        _ = s.add_spline(fit, true)
        node_commands.set_text(w, 0, s.text())
        node_commands.display_selected(w)
        w.view.yaw = 0.0
        w.view.pitch = 1.5
        w.view.distance = 80.0
        w.view.target = Vector3(20.0, 0.0, -15.0)
        w.view.update_camera()
        editor.panels.refresh()
        editor.app.layout(2200.0, 1302.0)
        let e = editor.panels.view.sketch.editor
        e.sync()
        # Trim over an arm; or Line near the crossing (it snaps there).
        let snapping = arguments[1] == "sketch_snap"
        e.choose("Line" if snapping else "Trim")
        let area = editor.panels.view.layout().bounds()
        let at = project(w, e.frame.at(21.0 if snapping else 36.0, 15.6 if snapping else 27.0), area.width, area.height)
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + at[0], y = area.y + at[1]))
        # (The tool's hint is no failure.)
        w.notice = ""
    if arguments.length > 1 and arguments[1] == "ux_tools":
        # The viewport's tidy layout: an Edit CAD box with one edge
        # filleted, its tool bar along the bottom, the levels at the top
        # left, Q W E R T down the left, and S held: the Modify menu.
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "Edit CAD", 40.0, 40.0)
        modeling_commands.create(w, "Box")
        selection_commands.set_mode(w, 2)
        let mesh = preview_support.shown(w) else trap("edges")
        let tag = mesh.find_attribute("cad_edge", 4)
        var edge = -1
        for at in 0..<(mesh.edge_count()):
            if edge < 0 and mesh.attribute_value(tag, at) >= 0.0: edge = at
        selection_commands.choose_component(w, edge, false)
        modeling_commands.model(w, "Fillet")
        modeling_commands.set_last_step(w, 0, 0.45, 0.0001, 100000.0)
        w.frame_cube()
        editor.panels.refresh()
        editor.app.layout(2200.0, 1400.0)
        editor.panels.refresh()
        let area = editor.panels.view.layout().bounds()
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + area.width * 0.62, y = area.y + area.height * 0.45))
        editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.s))
    if arguments.length > 1 and arguments[1] in ["cad_pick", "cad_peek"]:
        # Pick edges on a G3 fillet of three edges at a box's corner: the
        # fillet shows while its input's edges are picked on.
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        node_commands.set_parameter(w, 0, 2.0)
        node_commands.set_parameter(w, 1, 2.0)
        node_commands.set_parameter(w, 2, 2.0)
        let box = w.network.selected
        let body = CadGeometry.model(preview_support.cooked(w, box).geometry, 0)
        let edges: list[int] = []
        for face in 0..<body.face_count():
            for use in 0..<body.face_edge_count(face):
                let a = body.face_edge_point(face, use, 0.0)
                let b = body.face_edge_point(face, use, 1.0)
                let id = body.face_edge_id(face, use)
                let corner = (a.x > 0.99 and a.y > 0.99 and a.z > 0.99) or (b.x > 0.99 and b.y > 0.99 and b.z > 0.99)
                if corner and not (id in edges):
                    edges.append(id)
        node_commands.add_node(w, "Fillet", 40.0, 160.0)
        let fillet = w.network.selected
        node_commands.set_parameter(w, 0, 0.4)
        node_commands.set_parameter(w, 1, 2.0)
        node_commands.set_text(w, 0, CadModeling.edge_references(body, edges))
        tool_picks.start_named(w, fillet, "Edges")
        # Shift or Cmd held: the input shows.
        if arguments[1] == "cad_peek":
            tool_prompts.set_peek(w, true)
        w.frame_cube()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] == "cad_smooth":
        # A box's every edge filleted G2: setback corners, a smooth patch
        # where each three fillets meet.
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        node_commands.set_parameter(w, 0, 3.0)
        node_commands.set_parameter(w, 1, 2.0)
        node_commands.set_parameter(w, 2, 2.0)
        node_commands.add_node(w, "Fillet", 40.0, 160.0)
        node_commands.set_parameter(w, 0, 0.5)
        node_commands.set_parameter(w, 1, 1.0)
        w.frame_cube()
        editor.panels.refresh()
    if arguments.length > 1 and (arguments[1] == "cad_twice" or arguments[1] == "cad_twice_chamfer"):
        # Fillets on fillets: a box's uprights filleted G3, then its top and
        # bottom loops (lines and the uprights' sections, tangent where they
        # meet) filleted G3 from one picked edge each, or the top chamfered.
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        node_commands.set_parameter(w, 0, 3.0)
        node_commands.set_parameter(w, 1, 2.0)
        node_commands.set_parameter(w, 2, 2.0)
        let body = CadGeometry.model(preview_support.cooked(w, w.network.selected).geometry, 0)
        let uprights: list[int] = []
        for face in 0..<body.face_count():
            for use in 0..<body.face_edge_count(face):
                let a = body.face_edge_point(face, use, 0.0)
                let b = body.face_edge_point(face, use, 1.0)
                let id = body.face_edge_id(face, use)
                if a.x == b.x and a.z == b.z and not (id in uprights):
                    uprights.append(id)
        node_commands.add_node(w, "Fillet", 40.0, 160.0)
        node_commands.set_parameter(w, 0, 0.6)
        node_commands.set_parameter(w, 1, 2.0)
        node_commands.set_text(w, 0, CadModeling.edge_references(body, uprights))
        let rounded = CadGeometry.model(preview_support.cooked(w, w.network.selected).geometry, 0)
        let loops: list[int] = []
        for face in 0..<rounded.face_count():
            for use in 0..<rounded.face_edge_count(face):
                let a = rounded.face_edge_point(face, use, 0.0)
                let b = rounded.face_edge_point(face, use, 1.0)
                let id = rounded.face_edge_id(face, use)
                let top = a.y > 0.99 and b.y > 0.99
                let floor = a.y < -0.99 and b.y < -0.99 and arguments[1] == "cad_twice"
                if (top or floor) and a.x == b.x and loops.length < (2 if arguments[1] == "cad_twice" else 1) and not (id in loops):
                    loops.append(id)
        if arguments[1] == "cad_twice":
            node_commands.add_node(w, "Fillet", 40.0, 280.0)
            node_commands.set_parameter(w, 0, 0.25)
            node_commands.set_parameter(w, 1, 2.0)
        else:
            node_commands.add_node(w, "Chamfer", 40.0, 280.0)
            node_commands.set_parameter(w, 0, 0.2)
        node_commands.set_text(w, 0, CadModeling.edge_references(rounded, loops))
        node_commands.display_selected(w)
        w.frame_cube()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] == "script":
        # A Script node on a grid, its script half-typed: completion opens at `g.`.
        let w = editor.workspace
        node_commands.add_node(w, "Script", 300.0, 170.0)
        script_node.set_code(w, w.network.selected, script_capture)
        node_commands.display_selected(w)
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] == "objects":
        # Object picking: a cube and a moved sphere, both picked at the
        # Object level, their silhouettes outlined.
        let w = editor.workspace
        let cube = w.network.selected
        node_commands.add_node(w, "Sphere", 300.0, 70.0)
        node_commands.add_node(w, "Transform", 300.0, 170.0)
        node_commands.set_parameter(w, 1, 3.0)
        let moved = w.network.selected
        selection_commands.set_mode(w, 0)
        node_commands.pick_node(w, cube, false)
        node_commands.pick_node(w, moved, true)
        w.frame_cube()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] in ["render", "shader", "takes", "render_mode"]:
        # A gold sphere on a grey floor (Assign Material binding a Material to
        # the sphere's faces), a camera, an area light and a Render node,
        # rendered in the Render panel before the capture.
        let w = editor.workspace
        node_commands.delete_selected(w)
        node_commands.add_node(w, "Sphere", 40.0, 40.0)
        node_commands.set_parameter(w, 1, 64.0)
        let ball = w.network.selected
        node_commands.add_node(w, "Materials", 260.0, 40.0)
        let library = w.network.selected
        node_commands.enter_group(w, library)
        node_commands.add_node(w, "Material", 40.0, 40.0)
        node_commands.set_text(w, 0, "materials/gold")
        let gold = w.network.selected
        # Inside: the surface's gold, and with `shader`, a noise-and-checker
        # graph driving its color, roughness and bump, and a rim coat.
        node_commands.enter_group(w, gold)
        let surface = w.network.local_nodes()[0].id
        node_commands.select_node(w, surface)
        for (slot, value) in [(0, 1.0), (1, 0.77), (2, 0.34), (3, 1.0), (9, 0.25)]:
            node_commands.set_parameter(w, slot, value)
        if arguments[1] == "shader":
            node_commands.add_node(w, "Noise Texture", -180.0, 30.0)
            node_commands.set_parameter(w, 0, 8.0)
            node_commands.set_parameter(w, 1, 4.0)
            let noise = w.network.selected
            node_commands.add_node(w, "Checker Texture", 60.0, 230.0)
            for (slot, value) in [(0, 1.0), (1, 0.77), (2, 0.34), (3, 0.6), (4, 0.6), (5, 0.62), (6, 4.0)]:
                node_commands.set_parameter(w, slot, value)
            let checker = w.network.selected
            # The sphere has no texture coordinates: checks by position.
            node_commands.add_node(w, "Position", -180.0, 260.0)
            node_commands.connect(w, w.network.selected, checker, 0, 0)
            node_commands.add_node(w, "Map Range", 60.0, 30.0)
            node_commands.set_parameter(w, 3, 0.1)
            node_commands.set_parameter(w, 4, 0.5)
            let roughness = w.network.selected
            node_commands.connect(w, noise, roughness, 0, 0)
            node_commands.connect(w, checker, surface, 0, 0)
            node_commands.connect(w, roughness, surface, 5, 0)
            # The noise also bumps the surface, shallowly.
            node_commands.add_node(w, "Bump", 300.0, 330.0)
            node_commands.set_parameter(w, 1, 0.03)
            let bump = w.network.selected
            node_commands.connect(w, noise, bump, 3, 0)
            node_commands.connect(w, bump, surface, 11, 0)
            # A clear coat toward the rim, by Layer Weight's Facing.
            node_commands.add_node(w, "Layer Weight", -180.0, 400.0)
            node_commands.set_parameter(w, 0, 0.3)
            node_commands.connect(w, w.network.selected, surface, 6, 1)
            node_commands.select_node(w, surface)
        node_commands.leave_group(w)
        node_commands.leave_group(w)
        node_commands.select_node(w, ball)
        node_commands.add_node(w, "Assign Material", 40.0, 140.0)
        node_commands.connect(w, library, w.network.selected, 1)
        let assigned = w.network.selected
        node_commands.add_node(w, "Grid", 480.0, 40.0)
        node_commands.set_parameter(w, 0, 20.0)
        node_commands.set_parameter(w, 1, 20.0)
        let floor = w.network.selected
        node_commands.select_node(w, assigned)
        node_commands.add_node(w, "Merge", 40.0, 240.0)
        node_commands.connect(w, floor, w.network.selected, 1)
        node_commands.leave_group(w)
        var upstream = w.network.selected
        node_commands.add_node(w, "Camera", 40.0, 340.0)
        node_commands.connect(w, upstream, w.network.selected, 0)
        upstream = w.network.selected
        for (slot, value) in [(1, 3.0), (2, 2.5), (3, 6.0), (4, -12.0), (5, 26.57)]:
            node_commands.set_parameter(w, slot, value)
        node_commands.add_node(w, "Light", 40.0, 440.0)
        node_commands.connect(w, upstream, w.network.selected, 0)
        upstream = w.network.selected
        for (slot, value) in [(1, 2.0), (2, 5.0), (3, 1.0), (10, 30.0), (14, 3.0), (15, 3.0)]:
            node_commands.set_parameter(w, slot, value)
        # The takes scene: a fill light and a close-up camera, and a take
        # through the close-up with the fill off.
        var fill = -1
        var close = -1
        if arguments[1] == "takes":
            node_commands.add_node(w, "Light", 40.0, 540.0)
            node_commands.connect(w, upstream, w.network.selected, 0)
            upstream = w.network.selected
            for (slot, value) in [(1, -4.0), (2, 3.0), (3, 2.0), (10, 6.0), (14, 3.0), (15, 3.0)]:
                node_commands.set_parameter(w, slot, value)
            node_commands.rename(w, "Fill")
            fill = w.network.selected
            node_commands.add_node(w, "Camera", 40.0, 640.0)
            node_commands.connect(w, upstream, w.network.selected, 0)
            upstream = w.network.selected
            for (slot, value) in [(1, 1.6), (2, 1.5), (3, 4.4), (4, -6.0), (5, 20.0)]:
                node_commands.set_parameter(w, slot, value)
            node_commands.rename(w, "Close-up")
            close = w.network.selected
        node_commands.add_node(w, "Render", 40.0, 740.0 if arguments[1] == "takes" else 540.0)
        node_commands.connect(w, upstream, w.network.selected, 0)
        for (slot, value) in [(1, 960.0), (2, 540.0), (3, 64.0), (12, 0.5), (13, 0.5), (14, 0.5), (21, 0.6)]:
            node_commands.set_parameter(w, slot, value)
        if arguments[1] == "takes":
            take_commands.add_take(w)
            take_commands.rename_take(w, "Close-up")
            take_commands.toggle_light(w, fill)
            take_commands.set_camera(w, close)
            if let panel = editor.panels.dock.find("takes"):
                editor.panels.dock.select(panel)
        editor.panels.refresh()
        # render_mode shows the same scene in the viewport's Render mode.
        if arguments[1] == "render_mode":
            w.set_display_mode(int(probe.parse_value(arguments[4])) if arguments[4] != "3" else 5)
            w.frame_cube()
        else:
            editor.panels.render()
        # The shader scene shows the Material's nodes once the render started.
        if arguments[1] == "shader":
            node_commands.select_node(w, gold)
            node_commands.enter_group(w, gold)
            editor.panels.refresh()
    if arguments.length > 1 and arguments[1] in ["contexts", "contexts_menu", "create_menu"]:
        let w = editor.workspace
        node_commands.leave_group(w)
        let geo = w.network.selected
        var at = 1
        for kind in ["CAD", "SDF", "Volumes", "GSplats", "Materials", "Group"]:
            node_commands.add_node(w, kind, 40.0 + float(at % 4) * 180.0, 70.0 + float(at // 4) * 130.0)
            at += 1
        node_commands.select_node(w, geo)
        for kind in ["Camera", "Light", "Render"]:
            node_commands.add_node(w, kind, 40.0 + float(at % 4) * 180.0, 70.0 + float(at // 4) * 130.0)
            at += 1
        editor.panels.refresh()
    # A Group inside a Geo, seen from inside the Group: its torus is the
    # context; the Geo's output (the cube, moved) and another Geo's sphere
    # draw around it as the context display mode says (Greyed by default).
    if arguments.length > 1 and arguments[1] == "nested":
        let w = editor.workspace
        let geo = w.network.scope
        node_commands.add_node(w, "Transform", 40.0, 160.0)
        node_commands.set_parameter(w, 1, 3.0)
        let moved = w.network.selected
        node_commands.add_node(w, "Group", 260.0, 70.0)
        let group = w.network.selected
        node_commands.select_node(w, moved)
        node_commands.display_selected(w)
        node_commands.leave_group(w)
        node_commands.add_node(w, "Geo", 260.0, 70.0)
        node_commands.set_parameter(w, 1, -3.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Sphere", 40.0, 70.0)
        node_commands.enter_group(w, geo)
        node_commands.enter_group(w, group)
        node_commands.add_node(w, "Torus", 40.0, 70.0)
        w.view.distance = 14.0
        w.view.target = Vector3(0.0, 0.5, 0.0)
        w.view.update_camera()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] in ["selection_cad", "selection_patch", "selection_splats"]:
        let w = editor.workspace
        node_commands.delete_selected(w)
        let splats = arguments[1] == "selection_splats"
        node_commands.add_node(w, "GSplats" if splats else "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        if splats:
            node_commands.add_node(w, "File", 40.0, 40.0)
            node_commands.set_text(w, 0, arguments[2])
            selection_commands.set_mode(w, 1)
        else:
            node_commands.add_node(w, "Solid Cylinder", 40.0, 40.0)
            selection_commands.set_mode(w, 5 if arguments[1] == "selection_patch" else 2)
        if let worker = w.compute:
            let deadline = time.now() + 20000000000
            while worker.busy and time.now() < deadline: _ = w.poll_compute()
        if not splats:
            let mesh = preview_support.shown(w) else trap("CAD selection preview")
            if arguments[1] == "selection_patch":
                let owner = mesh.find_attribute("cad_face", 2)
                var face = 0
                for at in 0..<mesh.face_count():
                    if mesh.attribute_value(owner, at) == 2.0: face = at
                selection_commands.choose_component(w, face, false)
            else:
                let tag = mesh.find_attribute("cad_edge", 4)
                var edge = -1
                for at in 0..<mesh.edge_count():
                    if edge < 0 and mesh.attribute_value(tag, at) >= 0.0: edge = at
                selection_commands.choose_component(w, edge, false)
        w.frame_cube()
        w.view.distance *= 1.4
        w.view.update_camera()
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] in ["outline_cad", "outline_splats", "outline_volume"]:
        let w = editor.workspace
        node_commands.leave_group(w)
        node_commands.delete_selected(w)
        let kind = "CAD" if arguments[1] == "outline_cad" else ("GSplats" if arguments[1] == "outline_splats" else "Volumes")
        node_commands.add_node(w, kind, 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        if kind == "CAD": node_commands.add_node(w, "Solid Cylinder", 40.0, 40.0)
        elif kind == "GSplats":
            node_commands.add_node(w, "File", 40.0, 40.0)
            node_commands.set_text(w, 0, arguments[2])
        else:
            node_commands.add_node(w, "Volume", 40.0, 40.0)
            node_commands.set_parameter(w, 9, 1.0)
        node_commands.leave_group(w)
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] in ["marquee_components", "marquee_objects", "marquee_network", "nothing_selected", "through_depth", "sketch_marquee"]:
        setup_selection(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_cad.scenes():
        preview_cad.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_viewport.scenes():
        preview_viewport.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_poly.scenes():
        preview_poly.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_select.scenes():
        preview_select.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_drag.scenes():
        preview_drag.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_bevel.scenes():
        preview_bevel.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_shelf.scenes():
        preview_shelf.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_modify.scenes():
        preview_modify.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_create.scenes():
        preview_create.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_shape.scenes():
        preview_shape.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_icons.scenes():
        preview_icons.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_held.scenes():
        preview_held.setup(editor, arguments[1])
    if arguments.length > 1 and arguments[1] in preview_deform.scenes():
        preview_deform.setup(editor, arguments[1])
    let held_times = preview_held.DragTimes()
    let stroke_times = preview_deform.StrokeTimes()
    var frames = 0
    var computing_frames = 0
    let started = time.now()
    var phase = ""
    let orbit = arguments.length > 7 and arguments[7] == "orbit"
    var final_frame = 336 if orbit else (preview_held.frames(arguments[1]) if arguments.length > 1 else 5)
    if not orbit and arguments.length > 1 and arguments[1] in preview_deform.scenes():
        final_frame = preview_deform.frames(arguments[1])
    var frame_total = 0.0
    var frame_worst = 0.0
    # A scheduled input source keeps the loop awake. Invalidating only inside
    # on_frame measures the idle wait policy as well as rendering.
    var orbit_timer = -1
    if orbit:
        orbit_timer = editor.app.every(0.001, func () -> unit!:
            if frames > 0:
                editor.workspace.view.orbit(0.5, 0.0)
                editor.panels.view.layout().invalidate())
    if arguments.length > 1 and arguments[1].starts_with("gizmo_"):
        let w = editor.workspace
        node_commands.add_node(w, "Transform", 40.0, 220.0)
        modeling_commands.choose_tool(w, "Rotate" if arguments[1] == "gizmo_rotate" else ("Scale" if arguments[1] == "gizmo_scale" else "Move"))
        editor.panels.view.tools.gizmos.pivot = arguments[1] == "gizmo_pivot"
        editor.panels.refresh()
    if arguments.length > 1 and arguments[1] in ["light_move", "camera_move"]:
        let w = editor.workspace
        node_commands.leave_group(w)
        node_commands.add_node(w, "Light" if arguments[1] == "light_move" else "Camera", 240.0, 40.0)
        modeling_commands.choose_tool(w, "Move")
        editor.panels.refresh()
    let observed = editor.app.on_frame(func (elapsed: float) -> unit!:
        if arguments.length > 1:
            preview_held.watch(editor, arguments[1], held_times)
        if let compute = editor.workspace.compute:
            # held_heavy_running is captured while the worker runs.
            if compute.busy and not (arguments.length > 1 and arguments[1] == "held_heavy_running" and frames >= 4):
                let current = compute.service.phase()
                if current != phase:
                    phase = current
                    print(f"compute {float(time.now() - started) / 1000000000.0}s: {phase}")
                computing_frames += 1
                editor.panels.view.layout().invalidate()
                return
            if frames == 0:
                if editor.workspace.notice != "" and not (arguments[1] in ["timeline_refused", "loopcut_hover", "modify_knife", "modify_bridge_pick", "create_box", "create_surface", "pen_strip", "retopo_sphere", "sculpt_draw", "sculpt_drag"]): trap(editor.workspace.notice)
                print(f"Background compute: {computing_frames} live frames in {float(time.now() - started) / 1000000000.0}s; {editor.workspace.notice}")
                # The CAD scenes place their own camera (framing would take
                # the selection).
                if not (arguments[1] in preview_cad.scenes()):
                    editor.workspace.frame_cube()
        if frames == 0 and arguments.length > 10 and arguments[1] in ["cad", "cad_wire", "analytic", "grid"]:
            if arguments[1] == "grid":
                editor.workspace.frame_cube()
            elif arguments[1] != "analytic":
                let visible = editor.workspace.geometry else trap("capture has no polygon geometry; check the group expression")
                assert(visible.face_count() > 0)
            editor.workspace.frame_cube()
            if arguments.length > 18 and int(probe.parse_value(arguments[18])) >= 0:
                let w = editor.workspace
                let whole = preview_support.shown(w) else trap("patch capture requires polygons")
                let owner = whole.find_attribute("cad_face", 2)
                if owner < 0: trap("patch capture requires original CAD face ownership")
                let patch = int(probe.parse_value(arguments[18]))
                let neighbors = arguments.length > 20 and arguments[20] == "neighbors"
                let kept: list[bool] = []
                if neighbors:
                    let seam: list[bool] = []
                    for edge in 0..<whole.edge_count(): seam.append(false)
                    for face in 0..<whole.face_count():
                        let parent = int(whole.attribute_value(owner,face,0))
                        while kept.length <= parent: kept.append(false)
                        if parent != patch: continue
                        for corner in 0..<whole.face_size(face): seam[whole.face_edge(face,corner)] = true
                    for face in 0..<whole.face_count():
                        for corner in 0..<whole.face_size(face):
                            if seam[whole.face_edge(face,corner)]:
                                kept[int(whole.attribute_value(owner,face,0))] = true
                                break
                let remove: list[int] = []
                for face in 0..<whole.face_count():
                    let parent = int(whole.attribute_value(owner,face,0))
                    if not (kept[parent] if neighbors else parent == patch): remove.append(face)
                var isolated = whole.without_faces(remove)
                if arguments.length > 19 and arguments[19] == "neutral":
                    for domain in 0..<4:
                        if isolated.find_attribute("Cd", domain) >= 0: isolated = isolated.without_attribute("Cd", domain)
                if isolated.face_count() == 0: trap("requested CAD patch has no polygons")
                w.geometry = isolated
                w.scene.clear()
                # Inspection lighting must reveal underside patches too. The
                # mesh, normals, depth test and actual wire edges are unchanged.
                w.scene.add(AmbientLight(intensity = 0.6))
                w.scene.add(DirectionalLight(direction = Vector3(3.0, 5.0, 2.0), intensity = 0.45))
                w.scene.add(DirectionalLight(direction = Vector3(-3.0, -5.0, -2.0), intensity = 0.45))
                let branch = Group()
                let data = GeometryData(isolated)
                _ = append_scene(data, branch, mode = w.view.display_mode)
                w.scene.add(branch)
                let wires = OverlayLines.make()
                let normals = OverlayLines.make()
                append_overlays(data, Matrix4(), wires, normals)
                w.batches.wire_batches.clear()
                w.batches.wire_batches.append(wires.batch())
                w.batches.normal_batches.clear()
                w.batches.normal_batches.append(normals.batch())
                w.batches.patch_batches.clear()
                w.view.grid_visible = false
                let bounds = isolated.bounds()
                w.view.target = bounds.minimum.add(bounds.maximum).multiply_scalar(0.5)
                w.view.distance = bounds.maximum.subtract(bounds.minimum).length() * 1.8
                print(f"Isolated assembled patch {patch}, neighbors={neighbors}: {isolated.face_count()} polygons, {isolated.edge_count()} real edges")
            editor.workspace.view.distance *= probe.parse_value(arguments[8])
            editor.workspace.view.yaw = probe.parse_value(arguments[9])
            editor.workspace.view.pitch = probe.parse_value(arguments[10])
            if arguments.length > 14 and arguments[12] != "":
                editor.workspace.view.target = Vector3(probe.parse_value(arguments[12]), probe.parse_value(arguments[13]), probe.parse_value(arguments[14]))
            editor.workspace.view.update_camera()
        if editor.panels.render_view.running():
            editor.panels.render_view.layout().invalidate()
            return
        # The viewport's Render mode refines to its sample cap first.
        if editor.panels.view.rendering():
            editor.panels.view.layout().invalidate()
            return
        # The Script editor's package set warms off the UI thread: the
        # capture waits for it before typing.
        if frames == final_frame - 4 and arguments.length > 1 and arguments[1] == "script" and not editor.panels.inspector.script_ready():
            editor.panels.view.layout().invalidate()
            return
        frames += 1
        # Capture changes the drawable configuration, so exclude its next
        # interval from the 300 measured callbacks.
        if orbit and frames > 35 and frames <= 335:
            frame_total += elapsed * 1000.0
            if elapsed * 1000.0 > frame_worst: frame_worst = elapsed * 1000.0
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_cad.scenes():
            preview_cad.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_viewport.scenes():
            preview_viewport.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_poly.scenes():
            preview_poly.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_select.scenes():
            preview_select.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_drag.scenes():
            preview_drag.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_bevel.scenes():
            preview_bevel.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_shelf.scenes():
            preview_shelf.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_modify.scenes():
            preview_modify.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_create.scenes():
            preview_create.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_shape.scenes():
            preview_shape.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_icons.scenes():
            preview_icons.act(editor, arguments[1], frames, final_frame)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_held.scenes():
            preview_held.act(editor, arguments[1], frames, final_frame, held_times)
        if frames >= 2 and frames < final_frame and arguments.length > 1 and arguments[1] in preview_deform.scenes():
            preview_deform.act(editor, arguments[1], frames, final_frame, stroke_times)
        if frames == 2 and arguments.length > 1 and arguments[1].starts_with("param_"):
            probe_parameters(editor, arguments[1])
        if frames in [3, 4] and arguments.length > 1 and arguments[1] in ["marquee_components", "marquee_objects", "marquee_network", "nothing_selected", "through_depth", "sketch_marquee"]:
            drive_selection(editor, arguments[1], frames)
        if frames == 2:
            editor.panels.graph.frame_all()
            if arguments.length > 1 and arguments[1] == "freeze_flags":
                let graph = editor.panels.graph
                let node = editor.workspace.network.require(1)
                let at = graph.to_screen(node.x + 48.0, node.y + 20.0)
                let bounds = graph.layout().bounds()
                editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = bounds.x + at.x, y = bounds.y + at.y))
            if arguments.length > 1 and arguments[1] in ["shading", "surf_draft"]:
                if let show = editor.panels.view.on_shading:
                    show(false)
            if arguments.length > 1 and arguments[1] == "context_menu":
                if let show = editor.panels.view.on_context_menu:
                    show(false)
            if arguments.length > 1 and arguments[1] in ["menu", "contexts_menu"]:
                editor.panels.graph.pointer_x = 160.0
                editor.panels.graph.pointer_y = 80.0
                editor.panels.open_search()
        if frames == final_frame - 3 and arguments.length > 1 and arguments[1] == "script":
            # Type `g.` at the end of the script, as a user would, and complete.
            let code = editor.panels.inspector.code_body.editor
            code.layout().request_focus()
            code.set_selection(code.length(), code.length())
            code.insert("    g.")
            code.complete()
        if frames == final_frame - 1:
            if arguments.length > 1 and arguments[1] in ["menu", "contexts_menu"]:
                editor.panels.open_search()
                if arguments[1] == "contexts_menu":
                    for _ in 0..<2: editor.panels.search.event(Event(kind = EventKind.key_down, key = Key.down))
                    editor.panels.search.event(Event(kind = EventKind.key_down, key = Key.right))
            # The menu bar's Create (at the root, or in the Geo with its cube
            # selected) and Modify, a submenu open.
            # The network's right-click menu on the cube, or on empty
            # canvas with Create and its first category open.
            if arguments.length > 1 and arguments[1] in ["node_menu", "canvas_menu"]:
                let graph = editor.panels.graph
                let bounds = graph.layout().bounds()
                let node = editor.workspace.network.require(1)
                var at = graph.to_screen(node.x + 48.0, node.y + 20.0)
                if arguments[1] == "canvas_menu":
                    at = graph.to_screen(node.x + 300.0, node.y + 20.0)
                editor.app.dispatch(Event(kind = EventKind.pointer_down, x = bounds.x + at.x, y = bounds.y + at.y, button = 1))
                editor.app.dispatch(Event(kind = EventKind.pointer_up, x = bounds.x + at.x, y = bounds.y + at.y, button = 1))
                if arguments[1] == "canvas_menu":
                    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.down))
                    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.right))
                    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.right))
            if arguments.length > 1 and arguments[1] in ["create_menu", "create_menu_geo", "modify_menu"]:
                let menus = editor.panels.menus
                let menu = menus.modify if arguments[1] == "modify_menu" else menus.create
                menu.open()
                for _ in 0..<(6 if arguments[1] == "modify_menu" else (2 if arguments[1] == "create_menu_geo" else 0)):
                    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.down))
                editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.right))
            if arguments.length > 1 and arguments[1] == "project_settings":
                editor.panels.actions.project_settings.trigger()
            if arguments.length > 1 and arguments[1] == "timeline":
                editor.panels.actions.play.trigger()
                editor.panels.timeline.play_frame()
            probe.begin("Luced 3D")
            editor.panels.view.layout().invalidate()
        elif frames >= final_frame:
            # A notice is a failure, but where the scene shows one.
            if editor.workspace.notice != "" and not (arguments[1] in ["timeline_refused", "loopcut_hover", "modify_knife", "modify_bridge_pick", "create_box", "create_surface", "pen_strip", "retopo_sphere", "sculpt_draw", "sculpt_drag"]): trap(editor.workspace.notice)
            if orbit: print(f"Native orbit: {frame_total / 300.0} ms mean, {frame_worst} ms worst; callbacks, not input-to-photon")
            editor.app.stop())
    editor.app.run()
    observed.disconnect()
    if orbit: editor.app.cancel(orbit_timer)
    probe.save(arguments[0])
    probe.end()
    editor.close()
    return 0

## The marquee captures' scenes: an Edit Mesh on a sphere at the Point
## level (at the Faces level for through_depth); three objects at the root;
## Cube → Transform → Null beside a Sphere in the network; or the root's
## objects, about to be clicked away (sketch_marquee's is cad_sketch's).
func setup_selection(editor: Luce3D, scene: str) -> unit!:
    let w = editor.workspace
    if scene == "sketch_marquee":
        return
    if scene in ["marquee_components", "through_depth"]:
        node_commands.delete_selected(w)
        node_commands.add_node(w, "Sphere", 40.0, 40.0)
        node_commands.set_parameter(w, 1, 24.0)
        node_commands.add_node(w, "Edit Mesh", 40.0, 140.0)
        selection_commands.set_mode(w, 1 if scene == "marquee_components" else 3)
    elif scene == "marquee_network":
        node_commands.add_node(w, "Transform", 40.0, 130.0)
        node_commands.add_node(w, "Null", 40.0, 220.0)
        node_commands.add_node(w, "Sphere", 260.0, 40.0)
        node_commands.add_node(w, "Merge", 150.0, 320.0)
        node_commands.connect(w, by_kind(w, "Null").id, w.network.selected, 1)
    else:
        node_commands.leave_group(w)
        for (kind, x) in [("Sphere", 3.0), ("Torus", 6.0)]:
            node_commands.add_node(w, "Geo", 40.0 + x * 60.0, 40.0)
            node_commands.enter_group(w, w.network.selected)
            node_commands.add_node(w, kind, 0.0, 0.0)
            node_commands.add_node(w, "Transform", 0.0, 100.0)
            node_commands.set_parameter(w, 1, x)
            node_commands.leave_group(w)
        selection_commands.set_mode(w, 0)
    w.frame_cube()
    editor.panels.refresh()

## Frames 3 and 4 of a marquee capture: a box finished, then one held mid
## drag (its rectangle on screen), as the real window's events.
func drive_selection(editor: Luce3D, scene: str, frame: int) -> unit!:
    let w = editor.workspace
    let pane = editor.panels.graph.layout() if scene == "marquee_network" else editor.panels.view.layout()
    let area = pane.bounds()
    editor.app.focus(pane)
    if scene == "marquee_network":
        let graph = editor.panels.graph
        let transform = by_kind(w, "Transform")
        let start = graph.to_screen(transform.x - 30.0, transform.y - 20.0)
        if frame == 3:
            # Click the Sphere's wire into the Merge: picked, in the accent.
            let wire = network_wires.curve(graph.port_point(by_kind(w, "Sphere"), true), graph.port_point(by_kind(w, "Merge"), false, 0), false)[12]
            press(editor, area.x + wire.x, area.y + wire.y, area.x + wire.x, area.y + wire.y, true)
        else:
            let end = graph.to_screen(transform.x + 140.0, transform.y + 140.0)
            press(editor, area.x + start.x, area.y + start.y, area.x + end.x, area.y + end.y, false, shift = true)
    elif scene == "through_depth":
        # V, then a box over the sphere's left half takes its faces through
        # to the back; the view then looks down to show the band front to back.
        if frame == 3:
            editor.panels.actions.through_depth.trigger()
            # The capture ends on a notice: this one is expected.
            w.notice = ""
            let middle = project(w, Vector3(), area.width, area.height)
            press(editor, area.x + middle[0] - 200.0, area.y + middle[1] - 200.0, area.x + middle[0] - 40.0, area.y + middle[1] + 200.0, true)
        else:
            w.view.pitch = 1.35
            w.view.update_camera()
    elif scene == "sketch_marquee":
        # A box over the plate's right corner takes the two sides it meets;
        # then a Shift box held over the hole.
        let frame_of = editor.panels.view.sketch.editor.frame
        let a = project(w, frame_of.at(30.0, -26.0) if frame == 3 else frame_of.at(4.0, -3.0), area.width, area.height)
        let b = project(w, frame_of.at(50.0, -14.0) if frame == 3 else frame_of.at(20.0, -13.0), area.width, area.height)
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = area.x + a[0], y = area.y + a[1]))
        press(editor, area.x + a[0], area.y + a[1], area.x + b[0], area.y + b[1], frame == 3, shift = frame == 4)
    elif scene == "marquee_components":
        let middle = project(w, Vector3(), area.width, area.height)
        if frame == 3:
            press(editor, area.x + middle[0] - 160.0, area.y + middle[1] - 150.0, area.x + middle[0] - 20.0, area.y + middle[1] + 30.0, true)
        else:
            press(editor, area.x + middle[0] + 10.0, area.y + middle[1] - 40.0, area.x + middle[0] + 170.0, area.y + middle[1] + 120.0, false, shift = true)
    elif scene == "marquee_objects":
        let first = project(w, Vector3(), area.width, area.height)
        let second = project(w, Vector3(3.0, 0.0, 0.0), area.width, area.height)
        let third = project(w, Vector3(6.0, 0.0, 0.0), area.width, area.height)
        # A box reaching just into the sphere picks the cube and it (touching
        # is enough); then a Shift box held over the torus.
        if frame == 3:
            press(editor, area.x + first[0] - 40.0, area.y + first[1] - 120.0, area.x + first[0] * 0.25 + second[0] * 0.75, area.y + first[1] + 120.0, true)
        else:
            press(editor, area.x + third[0] - 80.0, area.y + third[1] - 90.0, area.x + third[0] + 90.0, area.y + third[1] + 80.0, false, shift = true)
    elif frame == 3:
        # Pick the middle object and choose Move, then click empty space
        # above them all.
        let middle = project(w, Vector3(3.0, 0.0, 0.0), area.width, area.height)
        press(editor, area.x + middle[0], area.y + middle[1], area.x + middle[0], area.y + middle[1], true)
        modeling_commands.choose_tool(w, "Move")
    else:
        let above = project(w, Vector3(3.0, 4.0, 0.0), area.width, area.height)
        press(editor, area.x + above[0], area.y + above[1], area.x + above[0], area.y + above[1], true)
    editor.panels.refresh()

## The first node of `kind` in the network shown.
func by_kind(w: Workspace, kind: str) -> Node!:
    for node in w.network.local_nodes():
        if node.kind == kind: return node
    trap(kind)

## A press at (x0, y0) moved to (x1, y1), released or held.
func press(editor: Luce3D, x0: float, y0: float, x1: float, y1: float, release: bool, shift: bool = false) -> unit!:
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = x0, y = y0, button = 0, shift = shift))
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = (x0 + x1) * 0.5, y = (y0 + y1) * 0.5, shift = shift))
    editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = x1, y = y1, shift = shift))
    if release:
        editor.app.dispatch(Event(kind = EventKind.pointer_up, x = x1, y = y1, button = 0, shift = shift))

## Drive the parameter pane as a user would for a capture.
func probe_parameters(editor: Luce3D, scene: str) -> unit!:
    let panel = editor.panels.inspector.parameters
    let origin = panel.layout().bounds()
    let first = panel.row_bounds(0)
    if scene == "param_copy":
        panel.set_tip_delay(0.0)
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = origin.x + 30.0, y = origin.y + first.y + 8.0))
    elif scene == "param_ladder":
        let field = panel.field_bounds(0, 0)
        let x = origin.x + field.x + 20.0
        let y = origin.y + field.y + 8.0
        editor.app.dispatch(Event(kind = EventKind.pointer_down, x = x, y = y, button = 2))
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = x + 40.0, y = y + 24.0, button = 2))
    elif scene == "param_menu":
        let axis = panel.row_bounds(0)
        editor.app.dispatch(Event(kind = EventKind.pointer_down, x = origin.x + panel.label_width() + 40.0, y = origin.y + axis.y + 8.0))
        editor.app.dispatch(Event(kind = EventKind.pointer_up, x = origin.x + panel.label_width() + 40.0, y = origin.y + axis.y + 8.0))

## The script the `script` capture completes in.
let script_capture = "## Lift the grid and color it by height.\nfrom luce_geocore.script import Cook, Geometry\n\npub func cook(k: Cook*) -> !:\n    let lift = k.f32(\"lift\", 0.5)\n    let g = k.geometry()\n    let positions = try g.positions()\n    for i in 0..<positions.length:\n        positions[i].y += lift\n"