dymokomi/luced-3d
Source of release 0.11.2
0.11.2 / 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, the knife,
## extrusion, the tool bar's step numbers and clearing edits. Moves are
## component_moves.luc's.
from luce_geocore.geocore import Domain
from luce_3d.graphics import Knife
from workspace import Workspace
from luce_std import math
from edit_operations import EditStep
from edit_kinds import is_edit
from edit_types import symmetry_slot, keep_slot
from tools import ToolRun, tool_run, verb_defaults
import node_catalog
import placing_commands
from edit_steps import append_step, add_tool_node, insert_after_display
from tool_prompts import start_prompt
## Run `tool` on the selection (see tools.luc): 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).
pub func model(w: Workspace, operation: str) -> unit!:
let mesh = w.geometry else return
# The knife is a stroke in the viewport (knife below), not a click.
if operation == "Knife":
choose_tool(w, "Knife")
w.notice = "Drag across the model to cut it; Shift snaps the stroke to 15°"
return
var run = ToolRun("", [])
if let kind = w.active_kind():
# 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
let tool = kind.tool(operation) else return
if chosen.count() == 0 or not tool.offered(w.level_index()):
placing_commands.start_placing(w, operation)
return
# The kind's own tool: its verb and numbers, whether or not
# tools.luc knows the name (Edit SDF's Subtract).
let tool = kind.tool(operation) else return
if not tool.offered(w.level_index()):
w.notice = f"{operation} does not apply to {w.level().name}"
w.revision += 1
return
run = ToolRun(tool.verb, tool.numbers else node_catalog.starting_values(tool.verb))
else:
run = tool_run(operation) else return
let wants_group = not (operation in ["Subdivide", "Clean"])
let chosen = w.current_selection() else return
if wants_group and chosen.count() == 0:
if w.editing():
w.notice = "Select components first"
w.revision += 1
return
start_prompt(w, operation, run)
return
let group = chosen.expression(mesh) if wants_group else ""
if w.editing():
append_step(w, operation, run.verb, group, run.numbers, mesh)
else:
add_tool_node(w, operation, run, group, mesh)
## 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 knife 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 knife(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.revision += 1
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.revision += 1
return
group = under.expression(mesh)
group_type = 3
let numbers = verb_defaults("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("Knife", 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())
# A tool is used once.
w.end_modal_tool()
return
w.perform("Knife", 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())
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.revision += 1
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())
## 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):
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.revision += 1
## Edit GSplats: select only the splats seen in the region (Visible only),
## or every splat there through the depth.
pub func toggle_visible_splats(w: Workspace):
w.splat_visible = not w.splat_visible
w.notice = "Select splats: " + ("visible only" if w.splat_visible else "through the depth")
w.revision += 1
## The splat brush's radius times `factor`, within 2..400 points.
pub func scale_brush(w: Workspace, factor: float):
w.splat_brush = math.clamp(w.splat_brush * factor, 2.0, 400.0)
w.revision += 1
pub func clear_edits(w: Workspace) -> unit!:
let node = w.selected() else 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())