dymokomi/luced-3d
Source of release 0.11.7
0.11.7 / src / modeling_commands.luc
## Modeling commands on the displayed geometry: a tool on the selection
## becomes a step of the displayed Edit node, or a verb node inserted after
## the displayed node (edit_steps.luc; Tool → Select waits for components
## when nothing is selected: tool_prompts.luc); primitives, Bisect's stroke,
## extrusion, the tool bar's step numbers and clearing edits. Moves are
## component_moves.luc's.
from luce_geocore.geocore import Domain, Mesh, Selection, Verbs
from luce_3d.graphics import Knife
from workspace import Workspace
from luce_std import math
from edit_operations import EditStep, verb_numbers
from edit_kinds import is_edit
from edit_types import symmetry_slot, keep_slot, edge_level, domain_number
from tools import ToolRun
from tool_record import Tool
from verb_rows import initial_numbers
import tool_registry
import node_catalog
import placing_commands
import selection_commands
import node_commands
from edit_steps import append_step, add_tool_node, insert_after_display
from tool_prompts import start_prompt
import tool_drag
import tool_picks
import edit_held
## Run tool `operation` on the selection: a step of the active Edit node,
## or a new node after the displayed one, its Group the selection's
## expression and its guard the topology it was made on. With nothing
## selected a node waits for its components (Tool → Select). A registry
## tool (tool_registry.luc) says how: the Knife draws its stroke
## (knife_placing.luc), a stroke tool (Bisect) waits for its stroke, a
## Select tool changes the selection, a whole tool runs on
## everything with nothing selected; a hover tool (Loop Cut) with nothing
## selected at its level waits for a click on the edge it shows under the
## pointer. A tool with a drag row then takes the viewport's drag
## (tool_drag.luc). `numbers` replace its starting numbers (Repeat's).
pub func model(w: Workspace, operation: str, numbers: list[float]? = none) -> unit!:
# The tool run before keeps what its drag set.
tool_drag.settle(w)
# A primitive is drawn in the viewport, Poly Pen builds under the
# pointer: neither needs anything shown first.
if let drawing = tool_registry.find(operation):
if drawing.gesture == "create":
start_create(w, drawing)
return
if drawing.gesture == "pen" or drawing.gesture == "sculpt":
start_pen(w, drawing)
return
let mesh = w.geometry else return
var registered: Tool? = none
var run = ToolRun("", [])
if let kind = w.active_kind():
let tool = kind.tool_at(operation, w.level_index()) else return
registered = tool.tool
# A drawing tool with nothing to work on draws instead.
if operation in kind.draw_tools and kind.drawn != none and w.editing():
let chosen = w.current_selection() else return
if chosen.count() == 0 or not tool.offered(w.level_index()):
placing_commands.start_placing(w, operation)
return
if not tool.offered(w.level_index()):
w.notice = refused(w, operation, registered)
w.touch()
return
# The kind's own tool: its verb and numbers (Edit SDF's Subtract).
run = ToolRun(tool.verb, tool.numbers else node_catalog.starting_values(tool.verb))
else:
let tool = tool_registry.find(operation) else return
registered = tool
run = ToolRun(tool.name, tool.starting())
if let given = numbers:
run = ToolRun(run.verb, given)
if let tool = registered:
# The Knife draws its stroke first, on everything.
if tool.gesture == "knife":
placing_commands.start_placing(w, tool.label, "knife")
return
if tool.gesture == "stroke":
choose_tool(w, tool.label)
w.notice = "Drag across the model to cut it; Shift snaps the stroke to 15°"
return
# A hover tool with nothing of its own to run on waits for the edge
# under the pointer; on selected edges it runs on them.
if tool.gesture == "hover" and numbers == none and (w.selected_count() == 0 or not tool.first_pick().takes(w.level_index())):
start_hover(w, tool)
return
if tool.section == "Select":
remember(w, tool.label, run.numbers, -1)
selection_commands.select_with(w, tool, run.numbers)
return
let whole = registered != none and (registered else trap("tool")).whole
let chosen = w.current_selection() else return
if not whole and chosen.count() == 0:
if w.editing():
w.notice = waiting(w, registered)
w.touch()
return
start_prompt(w, operation, run)
return
# A whole tool runs on everything only with nothing selected.
let group = chosen.expression(mesh) if chosen.count() > 0 else ""
run_tool(w, operation, registered, run, group, mesh)
## Run `run` (tool `operation`; `registered` when the registry has it) on
## `group` of `mesh`: a step of the edited node, or a node after the
## displayed one. A registry tool with a drag row then takes the drag.
func run_tool(w: Workspace, operation: str, registered: Tool?, run: ToolRun, group: str, mesh: Mesh) -> unit!:
let group_type = w.group_type()
if w.editing():
let node = w.selected() else return
let symmetry = int(node.values[symmetry_slot])
append_step(w, operation, run.verb, group, run.numbers, mesh)
if let tool = registered:
remember(w, operation, run.numbers, -1)
# A tool that wants its second pick (Bridge given one loop)
# waits for it; else its drag.
if asks_second(tool, run, group, group_type, mesh):
tool_picks.ask(w, node, tool, 1)
return
tool_drag.start(w, tool, mesh, group, group_type, true, symmetry)
return
add_tool_node(w, operation, run, group, mesh)
remember(w, operation, run.numbers, w.network.selected)
if let tool = registered:
if asks_second(tool, run, group, group_type, mesh):
tool_picks.start(w, w.network.require(w.network.selected), tool, 1)
return
tool_drag.start(w, tool, mesh, group, group_type, false)
## Whether mesh tool `tool` run as `run` on `group` still wants its second
## pick (a CAD tool asks for its picks itself: tool_run.luc).
func asks_second(tool: Tool, run: ToolRun, group: str, group_type: int, mesh: Mesh) -> bool!:
if tool.picks.length < 2 or Verbs.index_of(tool.name) < 0:
return false
return Verbs.wants_second(tool.name, mesh, group, group_type, verb_numbers(tool.name, run.numbers))
## A hover tool (Loop Cut) chosen: at the Edge level the viewport shows what
## it would make on the edge under the pointer until a click places it
## (hover_gesture.luc).
func start_hover(w: Workspace, tool: Tool) -> unit!:
if w.level_index() != edge_level:
selection_commands.set_mode(w, edge_level)
choose_tool(w, tool.label)
w.notice = f"{tool.label}: hover an edge to see its ring, the wheel sets the cuts; click to cut, then move to slide and click"
## Hover tool `tool` placed on edge `edge` with `numbers`: run as any tool
## on that edge, its drag then following the pointer.
pub func place_hover(w: Workspace, tool: Tool, edge: int, numbers: list[float]) -> unit!:
let mesh = w.geometry else return
let group = Selection.empty(mesh, domain_number(Domain.edge)).changed(edge, 3).expression(mesh)
w.end_modal_tool()
run_tool(w, tool.label, tool, ToolRun(tool.name, numbers), group, mesh)
## A primitive chosen: the viewport draws it (create_gesture.luc), a step
## of the edited Edit Mesh or a node of its own.
func start_create(w: Workspace, tool: Tool) -> unit!:
choose_tool(w, tool.label)
w.notice = f"{tool.label}: drag on the ground or a surface for its base; Enter makes one at the origin, Esc leaves"
## Poly Pen or Sculpt chosen (pen_gesture.luc, sculpt_gesture.luc): they
## work inside an Edit Mesh, so outside one an Edit Mesh is added after the
## displayed node (Houdini's TopoBuild makes its SOP) and edited.
func start_pen(w: Workspace, tool: Tool) -> unit!:
if not w.editing():
if w.network.find(w.network.display) != none and w.geometry != none:
w.perform("Add Edit Mesh", func () -> unit!:
_ = insert_after_display(w, ToolRun("Edit Mesh", []))
w.rebuild())
else:
node_commands.add_node(w, "Edit Mesh", 40.0, 70.0)
choose_tool(w, tool.label)
if tool.gesture == "sculpt":
w.notice = "Sculpt: drag on the surface to stroke (Ctrl inverts, Shift smooths); [ and ] size the brush; the tool bar sets the brush, strength and falloff; Esc leaves"
return
w.notice = "Poly Pen: click corners (M: quads, triangles, n-gon, strip); Ctrl+click extends the nearest edge, Ctrl+drag an edge or corner; drag a point to move it; Shift+click deletes; Ctrl+Shift+click splits an edge; Backspace, Esc"
## Keep tool `label` and its numbers (and the node it made) for Repeat.
func remember(w: Workspace, label: str, numbers: list[float], node: int):
w.last_tool = label
w.last_tool_numbers = numbers.copy()
w.last_tool_node = node
## Repeat: the tool run last, with its last numbers (its node's, when it
## made one that is still there), on what is selected now.
pub func repeat(w: Workspace) -> unit!:
if w.last_tool == "":
return
var numbers = w.last_tool_numbers
if let node = w.network.find(w.last_tool_node):
numbers = node.values[..<numbers.length]
model(w, w.last_tool, numbers)
## Why a tool does not run at the current level.
func refused(w: Workspace, operation: str, registered: Tool?) -> str:
if let tool = registered:
return f"{tool.label} works on {tool.levels_text(level_names(w))}"
return f"{operation} does not apply to {w.level().name}"
## What a tool waits for with nothing selected.
func waiting(w: Workspace, registered: Tool?) -> str:
if let tool = registered:
return tool.waiting(level_names(w))
return "Select components first"
## The selection levels' names (Object, Vertices, Edges, Polygons, …).
func level_names(w: Workspace) -> list[str]:
let result: list[str] = []
for level in w.levels():
result.append(level.name)
return result
## Create primitive `name` as a step of the displayed Edit node; its
## piece becomes the selected Object.
pub func create(w: Workspace, name: str) -> unit!:
let kind = w.active_kind() else return
if not w.editing():
return
let node = w.selected() else return
let step = kind.create(name) else return
w.perform("Create " + name, func () -> unit!:
node.edits.append(step)
w.network.invalidate(node.id)
w.adopt_output = true
w.pending_level = 0
w.rebuild())
## A Bisect stroke from (x0, y0) to (x1, y1) of the viewport: a Clip that
## keeps both sides along the plane through the eye and the stroke
## (recorded as a point and a normal, so replays need no camera). It
## cuts the selection, or the faces the stroke passes over.
pub func bisect(w: Workspace, x0: float, y0: float, x1: float, y1: float, width: float, height: float) -> unit!:
let mesh = w.geometry else return
let plane = Knife.plane(w.view.camera, x0, y0, x1, y1, width, height) catch failure:
w.notice = failure.message
w.touch()
return
let chosen = w.current_selection() else return
var group = chosen.expression(mesh) if chosen.count() > 0 else ""
var group_type = w.group_type()
if group == "":
let under = Knife.faces_under(mesh, w.view.camera, x0, y0, x1, y1, width, height)
if under.count() == 0:
w.notice = "The stroke crosses no faces"
w.touch()
return
group = under.expression(mesh)
group_type = 3
let numbers = initial_numbers("Clip")
numbers[0] = 3.0
numbers[3] = 2.0
numbers[4] = plane.point.x
numbers[5] = plane.point.y
numbers[6] = plane.point.z
numbers[7] = plane.normal.x
numbers[8] = plane.normal.y
numbers[9] = plane.normal.z
if w.editing():
let node = w.selected() else return
w.perform("Bisect", func () -> unit!:
node.edits.append(EditStep("Clip", group, group_type, numbers, mesh.topology_hash(), int(node.values[symmetry_slot]), node.values[keep_slot] != 0.0))
w.network.invalidate(node.id)
w.adopt_output = true
w.rebuild())
remember(w, "Bisect", numbers, -1)
# A tool is used once.
w.end_modal_tool()
return
w.perform("Bisect", func () -> unit!:
let node = insert_after_display(w, ToolRun("Clip", numbers))
node.texts[0] = group
node.values[node_catalog.group_type_slot] = float(group_type)
node.guard = mesh.topology_hash()
w.adopt_output = true
w.rebuild())
remember(w, "Bisect", numbers, -1)
w.end_modal_tool()
pub func extrude(w: Workspace) -> unit!:
if w.selected_count() == 0 or not w.on_domain(Domain.face):
w.notice = "Select faces to extrude"
w.touch()
return
model(w, "Extrude")
## Number `slot` of the Edit node's last step (the tool bar's), kept
## within `minimum` and `maximum`: the step runs again with it.
pub func set_last_step(w: Workspace, slot: int, value: float, minimum: float, maximum: float) -> unit!:
let node = w.selected() else return
if not is_edit(node.kind) or node.edits.length == 0 or slot < 0 or slot >= node.edits[-1].numbers.length:
return
let accepted = math.max(minimum, math.min(maximum, value))
if node.edits[-1].numbers[slot] == accepted:
return
w.perform("Change " + node.edits[-1].verb, func () -> unit!:
let numbers = node.edits[-1].numbers.copy()
numbers[slot] = accepted
node.edits[-1] = node.edits[-1].with_numbers(numbers)
w.network.invalidate(node.id)
w.rebuild())
# Repeat runs the tool with what its bar was set to.
if let tool = tool_registry.find(w.last_tool):
if tool.name == node.edits[-1].verb and w.last_tool_node < 0:
w.last_tool_numbers = node.edits[-1].numbers.copy()
## Set the last step's numbers from `slot` on to `values` at once (a value
## ramp's slots), as one change.
pub func set_last_step_values(w: Workspace, slot: int, values: list[float]) -> unit!:
let node = w.selected() else return
if not is_edit(node.kind) or node.edits.length == 0 or slot < 0:
return
let numbers = node.edits[-1].numbers.copy()
while numbers.length < slot + values.length:
numbers.append(0.0)
if numbers[slot..<slot + values.length] == values:
return
w.perform("Change " + node.edits[-1].verb, func () -> unit!:
for at in 0..<values.length:
numbers[slot + at] = values[at]
node.edits[-1] = node.edits[-1].with_numbers(numbers)
w.network.invalidate(node.id)
w.rebuild())
if let tool = tool_registry.find(w.last_tool):
if tool.name == node.edits[-1].verb and w.last_tool_node < 0:
w.last_tool_numbers = node.edits[-1].numbers.copy()
## Choose tool `tool`. Select chosen again while splats are edited cycles
## how a drag selects them: box, lasso, brush.
pub func choose_tool(w: Workspace, tool: str):
# The tool run before keeps what its drag set.
tool_drag.settle(w) catch failure:
w.drag_session = none
if tool == "Select" and w.edit_tool == "Select":
if let kind = w.active_kind():
if kind.tints:
w.splat_shape = (w.splat_shape + 1) % 3
w.notice = "Select splats with: " + ["Box", "Lasso", "Brush"][w.splat_shape]
w.edit_tool = tool
w.touch()
## Select through the depth (V), or only what shows: Edit GSplats' splats
## (through by default; Visible only picks the splats seen), else the
## component boxes (visible only by default).
pub func toggle_through_depth(w: Workspace):
if picks_splats(w):
w.splat_visible = not w.splat_visible
else:
w.through_depth = not w.through_depth
w.notice = "Box selects " + ("through the depth" if selects_through(w) else "only what shows")
w.touch()
## Whether a box selects through the depth now (splats or components).
pub func selects_through(w: Workspace) -> bool:
return not w.splat_visible if picks_splats(w) else w.through_depth
## Whether the selection is on splats (a GSplats context).
func picks_splats(w: Workspace) -> bool:
if let kind = w.selection_kind():
return kind.tints
return false
## The splat brush's radius times `factor`, within 2..400 points.
pub func scale_brush(w: Workspace, factor: float):
if tool_registry.gesture_of(w.edit_tool) == "sculpt":
w.sculpt_radius = math.clamp(w.sculpt_radius * factor, 3.0, 800.0)
w.touch()
return
w.splat_brush = math.clamp(w.splat_brush * factor, 2.0, 400.0)
w.touch()
pub func clear_edits(w: Workspace) -> unit!:
let node = w.selected() else return
# A node that holds its result starts over from its input (Reload
# Input), one undo step.
if w.holds(node.kind):
w.perform("Reload input", func () -> unit!:
edit_held.reload(w.network, node)
w.selection = none
w.network.invalidate(node.id)
w.rebuild())
return
if not is_edit(node.kind) or node.edits.length == 0:
return
w.perform("Clear edits", func () -> unit!:
node.edits = []
w.selection = none
w.network.invalidate(node.id)
w.rebuild())