Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.4

0.11.4 / tools / preview.luc

import probe
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
import takes
from luce_cad.cad_geometry import CadGeometry
from luce_cad.modeling import CadModeling
from luce_cad.sketch import CadSketch
import tool_prompts

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 > 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()
    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, 2)
            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] == "outliner":
        let w = editor.workspace
        node_commands.set_output(w, 1, -1)
        node_commands.add_node(w, "Group", 220.0, 70.0)
        let vehicle = w.network.selected
        node_commands.rename(w, "Assembly")
        node_commands.enter_group(w, vehicle)
        node_commands.add_node(w, "Cube", 40.0, 60.0)
        node_commands.rename(w, "Body")
        node_commands.add_node(w, "Group", 240.0, 60.0)
        let detail = w.network.selected
        node_commands.rename(w, "Details")
        node_commands.set_parameter(w, 1, 3.0)
        node_commands.enter_group(w, detail)
        node_commands.add_node(w, "Sphere", 40.0, 60.0)
        node_commands.rename(w, "Sphere")
        node_commands.leave_group(w)
        node_commands.leave_group(w)
        w.network.selected = -1
        node_commands.add_node(w, "Torus", 430.0, 70.0)
        node_commands.add_node(w, "Transform", 430.0, 180.0)
        node_commands.set_parameter(w, 1, -3.0)
        node_commands.rename(w, "Independent island")
        node_commands.select_node(w, vehicle)
        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, "Boolean", 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(w.network.evaluate_data(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] == "cad_sketch":
        # 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).
        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()
        editor.panels.view.sketch.editor.selection = [9]
        editor.panels.refresh()
        if 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] 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 = w.geometry 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(w.network.evaluate_data(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_prompts.pick_edges(w, fillet)
        # 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] == "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 screen bounds outlined.
        let w = editor.workspace
        let cube = w.network.nodes[0].id
        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] == "render" or arguments[1] == "shader" or arguments[1] == "takes"):
        # 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, "Material", 260.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.select_node(w, ball)
        node_commands.add_node(w, "Assign Material", 40.0, 140.0)
        node_commands.connect(w, gold, 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)
        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.add_node(w, "Camera", 40.0, 340.0)
        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)
        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)
            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)
            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)
        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, takes.camera_path(w.network.require(close)))
            if let panel = editor.panels.dock.find("takes"):
                editor.panels.dock.select(panel)
        editor.panels.refresh()
        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()
    var frames = 0
    var computing_frames = 0
    let started = time.now()
    var phase = ""
    let orbit = arguments.length > 7 and arguments[7] == "orbit"
    let final_frame = 336 if orbit else 5
    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()
    let observed = editor.app.on_frame(func (elapsed: float) -> unit!:
        if let compute = editor.workspace.compute:
            if compute.busy:
                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 != "": trap(editor.workspace.notice)
                print(f"Background compute: {computing_frames} live frames in {float(time.now() - started) / 1000000000.0}s; {editor.workspace.notice}")
                editor.workspace.frame_cube()
        if frames == 0 and arguments.length > 10 and arguments[1] in ["cad", "cad_wire", "analytic"]:
            if 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 = w.geometry 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_batch = wires.batch()
                w.batches.normal_batch = normals.batch()
                w.batches.patch_batch = none
                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 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 arguments.length > 1 and arguments[1].starts_with("param_"):
            probe_parameters(editor, arguments[1])
        if frames == 2:
            editor.panels.graph.frame_all()
            if arguments.length > 1 and arguments[1] == "shading":
                if let show = editor.panels.view.on_shading:
                    show(false)
            if arguments.length > 1 and arguments[1] == "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] == "menu":
                editor.panels.open_search()
            probe.begin("Luced 3D")
            editor.panels.view.layout().invalidate()
        elif frames >= final_frame:
            if editor.workspace.notice != "": 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

## 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"