dymokomi/luced-3d
Source of release 0.11.9
## The same Command instances drive toolbar buttons, menus and shortcuts.
from luce_ui.ui import Command, Shortcut
from luce_window.input import Key
from interop import Connection
from workspace import Workspace
from network import kinds
from edit_kinds import is_edit, edit_tool_names, edit_primitive_names
from edit_types import level_names, sketch_level_names
from tools import transform_tools, transform_keys
import modeling_commands
import component_moves
import cad_commands
from edit_cad_actions import EditCadActions
import tool_run
import retopology
import tool_registry
from selection_actions import SelectionActions
import selection_commands
import node_commands
import context_display
from node_clipboard import NodeClipboard, duplicate
from tool_icons import asset_name
pub class Actions:
pub let new_project: Command
pub let open_project: Command
pub let save: Command
pub let save_as: Command
pub let reset_layout: Command
## Window › Four views and Full screen viewport: the viewport's panes
## (Space tapped over the viewport), and the viewport filling the
## window (Space with F).
pub let four_views: Command
pub let full_screen: Command
## Window › Project settings…: the project's units and time.
pub let project_settings: Command
pub let undo: Command
pub let redo: Command
pub let frame: Command
pub let grid: Command
pub let hulls: Command
pub let display: Command
pub let parent: Command
## The Network panel's toolbar: the node menu, the output, framing.
pub let add_node: Command
pub let set_output: Command
pub let frame_nodes: Command
pub let group: Command
pub let enabled: Command
pub let freeze: Command
pub let delete: Command
## Rename the selected node (F2): the panels' prompt asks for the name.
pub let rename: Command
## Cut, Copy, Paste and Duplicate of the picked nodes
## (node_clipboard.luc); the panels connect Paste, which places the nodes.
pub let cut: Command
pub let copy: Command
pub let paste: Command
pub let duplicate: Command
pub let clipboard: NodeClipboard
## The CAD hand-offs (cad_commands.luc): Finish Sketch, Extrude and
## Revolve from a sketch's profiles while one is edited; Sketch on face
## and Extrude face of the selected faces; Press Pull dispatching on
## what is selected, as Fusion's (tool_run.luc). The CAD tools are
## commands of `modeling`, as every tool is.
pub let finish_sketch: Command
## Slice while sketching: the body in front of the plane cut away from
## view (checked while on).
pub let slice: Command
## Redefine Sketch Plane of the sketch edited (Fusion's).
pub let redefine_plane: Command
pub let extrude_profiles: Command
pub let revolve_profiles: Command
pub let sketch_on_face: Command
pub let extrude_face: Command
pub let press_pull: Command
## Edit CAD's sketch commands: Create Sketch, Edit Sketch, Show/Hide.
pub let model: EditCadActions
pub let clear_edits: Command
## The context the modeling commands' icons were looked up for.
var icon_context: str = ""
## Repeat the registry tool run last (tool_run.repeat) on what is
## selected now: the first entry of S and of the right-click menu.
pub let repeat_tool: Command
## Retopology's snap target: pick the object new points land on, or
## let every surface count again (retopology.luc).
pub let snap_target: Command
pub let clear_snap_target: Command
pub let select_all: Command
## The splat brush's radius ([ and ]).
pub let smaller_brush: Command
pub let larger_brush: Command
## Boxes select through the depth, or only what shows (V): components,
## and Edit GSplats' splats.
pub let through_depth: Command
## The timeline's (timeline_pane.luc connects them): ↑ play or stop,
## ←/→ a frame back or on, ↓ the range's first frame.
pub let play: Command
pub let previous_frame: Command
pub let next_frame: Command
pub let first_frame: Command
pub let last_frame: Command
pub let looping: Command
pub let play_backwards: Command
pub let modes: list[Command]
## The context display modes: All, Greyed, Hidden (context_display.luc).
pub let context_modes: list[Command]
pub let tools: list[Command]
pub let add: list[Command]
pub let modeling: list[Command]
## Create a primitive in the displayed Edit node.
pub let create: list[Command]
## Invert, Select none, Hide, Unhide and the selection sets (the Select
## tools are the registry's).
pub let selection: SelectionActions
pub let all: list[Command]
let connections: list[Connection]
let workspace: Workspace
pub func init(self, workspace: Workspace) -> unit!:
self.workspace = workspace
self.new_project = Command("New project", Shortcut(Key.n, primary = true), id = "new_project")
self.open_project = Command("Open project…", Shortcut(Key.o, primary = true), id = "open_project")
self.save = Command("Save", Shortcut(Key.s, primary = true), id = "save_project")
self.save_as = Command("Save As…", Shortcut(Key.s, primary = true, shift = true), id = "save_project_as")
self.reset_layout = Command("Reset layout", id = "reset_layout")
self.four_views = Command("Four views (Space)", id = "four_views")
self.full_screen = Command("Full screen viewport (Space F)", id = "full_screen_viewport")
self.project_settings = Command("Project settings…", id = "project_settings")
self.undo = Command("Undo", Shortcut(Key.z, primary = true), id = "undo")
self.redo = Command("Redo", Shortcut(Key.z, primary = true, shift = true), id = "redo")
self.frame = Command("Frame geometry", Shortcut(Key.f), id = "frame")
self.grid = Command("Grid", Shortcut(Key.g), id = "grid")
self.hulls = Command("Control hulls", id = "control_hulls")
# D is the viewport's Select marking menu; the Network panel takes D
# for the display flag itself (network_view.luc).
self.display = Command("Display flag", id = "display_node")
self.parent = Command("Up", Shortcut(Key.u), id = "parent_group")
self.add_node = Command("Add node (Tab)", id = "add_node_menu")
self.set_output = Command("Set Output", id = "set_output")
self.frame_nodes = Command("Frame nodes", id = "frame_nodes")
self.group = Command("Group", Shortcut(Key.g, primary = true), id = "group_nodes")
self.enabled = Command("Enable / bypass", Shortcut(Key.b), id = "toggle_node")
self.freeze = Command("Freeze / unfreeze", Shortcut(Key.b, shift = true), id = "freeze_node")
self.play = Command("Play", Shortcut(Key.up), id = "timeline_play")
self.previous_frame = Command("Previous frame", Shortcut(Key.left), id = "timeline_previous")
self.next_frame = Command("Next frame", Shortcut(Key.right), id = "timeline_next")
self.first_frame = Command("First frame", Shortcut(Key.down), id = "timeline_first")
self.last_frame = Command("Last frame", id = "timeline_last")
self.looping = Command("Loop", id = "timeline_loop")
self.play_backwards = Command("Play backwards", id = "timeline_play_backwards")
self.delete = Command("Delete node", id = "delete_node")
self.rename = Command("Rename…", Shortcut(Key.f2), id = "rename_node")
self.cut = Command("Cut", Shortcut(Key.x, primary = true), id = "cut_nodes")
self.copy = Command("Copy", Shortcut(Key.c, primary = true), id = "copy_nodes")
self.paste = Command("Paste", Shortcut(Key.v, primary = true), id = "paste_nodes")
self.duplicate = Command("Duplicate", Shortcut(Key.d, primary = true), id = "duplicate_nodes")
self.clipboard = NodeClipboard()
self.finish_sketch = Command("Finish Sketch", id = "finish_sketch", icon = "finish-sketch")
self.slice = Command("Slice", id = "sketch_slice", icon = "sketch-slice")
self.redefine_plane = Command("Redefine Sketch Plane", id = "redefine_sketch_plane", icon = "sketch-plane")
self.extrude_profiles = Command("Extrude", id = "extrude_profiles", icon = asset_name("Extrude"))
self.sketch_on_face = Command("Sketch on face", id = "sketch_on_face", icon = asset_name("Sketch"))
self.revolve_profiles = Command("Revolve", id = "revolve_profiles", icon = asset_name("Revolve"))
self.extrude_face = Command("Extrude face", id = "extrude_face", icon = asset_name("Extrude"))
self.press_pull = Command("Press Pull", id = "press_pull", icon = asset_name("Press Pull"))
self.model = EditCadActions(workspace, self.sketch_on_face)
self.clear_edits = Command("Clear edits", id = "clear_edits")
self.repeat_tool = Command("Repeat", id = "repeat_tool", icon = "repeat")
self.snap_target = Command("Snap Target…", id = "pick_snap_target", icon = "snap-target")
self.clear_snap_target = Command("Clear Snap Target", id = "clear_snap_target")
self.select_all = Command("Select all", Shortcut(Key.a, primary = true), id = "select_all", icon = "select-all")
self.connections = [self.undo.on_trigger(workspace.undo), self.redo.on_trigger(workspace.redo),
self.parent.on_trigger(func () -> unit!: node_commands.leave_group(workspace)),
self.set_output.on_trigger(func () -> unit!: node_commands.set_output(workspace)),
self.group.on_trigger(func () -> unit!: node_commands.group_selected(workspace)),
self.frame.on_trigger(workspace.frame_or_fill),
self.four_views.on_trigger(func () -> unit!: workspace.toggle_four()),
self.full_screen.on_trigger(func () -> unit!: workspace.toggle_full_screen()), self.grid.on_trigger(workspace.toggle_grid), self.hulls.on_trigger(workspace.toggle_hulls),
self.display.on_trigger(func () -> unit!: node_commands.display_selected(workspace)), self.enabled.on_trigger(func () -> unit!: node_commands.toggle_enabled(workspace)),
self.freeze.on_trigger(func () -> unit!: node_commands.toggle_freeze(workspace)),
self.cut.on_trigger(func () -> unit!: self.clipboard.cut(workspace)),
self.copy.on_trigger(func () -> unit!: self.clipboard.copy(workspace)),
self.duplicate.on_trigger(func () -> unit!: duplicate(workspace)),
self.delete.on_trigger(func () -> unit!: node_commands.delete_selected(workspace)),
self.clear_edits.on_trigger(func () -> unit!: modeling_commands.clear_edits(workspace)),
self.repeat_tool.on_trigger(func () -> unit!: tool_run.repeat(workspace)),
self.snap_target.on_trigger(func () -> unit!: retopology.pick(workspace)),
self.clear_snap_target.on_trigger(func () -> unit!: retopology.clear(workspace)),
self.finish_sketch.on_trigger(func () -> unit!: cad_commands.finish_sketch(workspace)),
self.slice.on_trigger(func () -> unit!: cad_commands.toggle_slice(workspace)),
self.redefine_plane.on_trigger(func () -> unit!: cad_commands.redefine_plane(workspace)),
self.extrude_profiles.on_trigger(func () -> unit!: cad_commands.extrude_profiles(workspace)),
self.revolve_profiles.on_trigger(func () -> unit!: cad_commands.revolve_profiles(workspace)),
self.extrude_face.on_trigger(func () -> unit!: cad_commands.extrude_face(workspace)),
self.press_pull.on_trigger(func () -> unit!: tool_run.press_pull(workspace)),
self.sketch_on_face.on_trigger(func () -> unit!: cad_commands.sketch_on_face(workspace)), self.select_all.on_trigger(func () -> unit!: selection_commands.select_all(workspace))]
self.add = []
self.modeling = []
# Every Edit kind's tools and the CAD nodes' tools, one command a
# label (from the kind and tool registries); the strip shows the
# active kind's. Each runs the tool of its context (tool_run.luc).
for operation in tool_names():
let name = operation
let command = Command(operation, id = "edit_" + operation, icon = asset_name(operation))
self.modeling.append(command)
self.connections.append(command.on_trigger(func () -> unit!: tool_run.run(workspace, name)))
self.selection = SelectionActions(workspace)
# Selection levels 1-7 (Object, Point, Edge, Face, Smart, then Edit
# CAD's Patch and Curves): their labels follow the Edit kind's levels
# (Vertices, CVs, …).
let keys = [Key.digit_1, Key.digit_2, Key.digit_3, Key.digit_4, Key.digit_5, Key.digit_6, Key.digit_7]
self.modes = []
for index in 0..<keys.length:
self.modes.append(Command(level_names()[index], Shortcut(keys[index]), id = f"select_level_{index + 1}"))
for index in 0..<self.modes.length:
let mode = index
self.connections.append(self.modes[index].on_trigger(func () -> unit!:
selection_commands.set_mode(workspace, mode)))
self.context_modes = []
for index in 0..<context_display.names().length:
let mode = index
let command = Command(context_display.names()[index], id = "context_" + context_display.names()[index].lower())
self.context_modes.append(command)
self.connections.append(command.on_trigger(func () -> unit!: workspace.set_context_mode(mode)))
# Primitives an Edit node creates (each kind offers its own).
self.create = []
for primitive in edit_primitive_names():
let name = primitive
let command = Command(primitive, id = "create_" + primitive.lower().replace(" ", "_"), icon = asset_name(primitive))
self.create.append(command)
self.connections.append(command.on_trigger(func () -> unit!: modeling_commands.create(workspace, name)))
# The transform tools, Maya's keys: Select, Move, Rotate, Scale and
# the selected node's own handles (Manipulator).
let tool_keys = transform_keys()
self.tools = []
for index in 0..<transform_tools().length:
let tool = transform_tools()[index]
let command = Command(tool, Shortcut(tool_keys[index]), id = "tool_" + tool.lower(), icon = asset_name(tool))
self.tools.append(command)
self.connections.append(command.on_trigger(func (): modeling_commands.choose_tool(workspace, tool)))
self.smaller_brush = Command("Smaller brush", Shortcut(Key.left_bracket), id = "smaller_brush")
self.larger_brush = Command("Larger brush", Shortcut(Key.right_bracket), id = "larger_brush")
self.connections.append(self.smaller_brush.on_trigger(func (): modeling_commands.scale_brush(workspace, 0.8)))
self.connections.append(self.larger_brush.on_trigger(func (): modeling_commands.scale_brush(workspace, 1.25)))
self.through_depth = Command("Select through the depth", Shortcut(Key.v), id = "select_through_depth", icon = "x-ray")
self.connections.append(self.through_depth.on_trigger(func (): modeling_commands.toggle_through_depth(workspace)))
for kind in kinds():
self.add.append(Command(kind, id = "add_" + kind, icon = asset_name(kind)))
# The sketch's features and the CAD nodes added remember their
# tool for Repeat (a tool run from its command remembers itself).
let features = [self.extrude_profiles, self.revolve_profiles, self.sketch_on_face, self.extrude_face]
let names = ["Extrude", "Revolve", "Sketch", "Extrude"]
for at in 0..<features.length:
let name = names[at]
self.connections.append(features[at].on_trigger(func () -> unit!: tool_run.remember(workspace, name)))
for command in self.add:
let name = command.text()
self.connections.append(command.on_trigger(func () -> unit!:
if workspace.network.context() == "cad" and not workspace.sketching() and tool_registry.cad_node(name) != none:
tool_run.remember(workspace, name)))
self.all = [self.undo, self.redo, self.frame, self.grid, self.hulls, self.display, self.parent, self.add_node, self.set_output, self.frame_nodes, self.group, self.enabled, self.freeze, self.play, self.play_backwards, self.previous_frame, self.next_frame, self.first_frame, self.last_frame, self.looping, self.delete, self.rename, self.cut, self.copy, self.paste, self.duplicate, self.finish_sketch, self.slice, self.redefine_plane, self.extrude_profiles, self.revolve_profiles, self.sketch_on_face, self.extrude_face, self.press_pull, self.clear_edits, self.repeat_tool, self.snap_target, self.clear_snap_target, self.select_all, self.smaller_brush, self.larger_brush, self.through_depth] + self.add + self.modes + self.context_modes + self.tools + self.modeling + self.create + self.selection.commands()
self.all += [self.new_project, self.open_project, self.save, self.save_as, self.project_settings, self.reset_layout, self.four_views, self.full_screen] + self.model.all()
self.refresh()
## Tool `label`'s command.
pub func command(self, label: str) -> Command?:
for command in self.modeling:
if command.text() == label:
return command
return none
## The tools of the viewport's context, each its command: the active
## Edit kind's (Edit Mesh's on other nodes), or in a CAD network its
## nodes'.
pub func modify_commands(self) -> list[Command]:
let w = self.workspace
let result: list[Command] = []
if w.network.context() == "cad" and not w.editing():
let labels: list[str] = []
for entry in tool_registry.cad_nodes():
labels.append(entry.label)
for command in self.modeling:
if command.text() in labels: result.append(command)
return result
let kind = w.strip_kind()
for command in self.modeling:
if kind.tool(command.text()) != none: result.append(command)
return result
## The active Edit kind's drawing tools (Edit CAD's CV Curve, EP Curve,
## Curve on Surface; Edit Mesh's PolyDraw): clicks place their points.
pub func draw_commands(self) -> list[Command]:
let kind = self.workspace.strip_kind()
let result: list[Command] = []
for command in self.modeling:
if command.text() in kind.draw_tools: result.append(command)
return result
## The Create marking menu's primitives inside an Edit node.
pub func create_commands(self) -> list[Command]:
let kind = self.workspace.strip_kind()
let result: list[Command] = []
for command in self.create:
if command.text() in kind.primitives: result.append(command)
return result
pub func refresh(self) -> unit!:
let w = self.workspace
self.undo.set_enabled(w.history.can_undo() or w.in_transaction())
self.redo.set_enabled(w.history.can_redo())
self.undo.set_text(w.history.undo_label())
self.redo.set_text(w.history.redo_label())
let node = w.selected()
self.delete.set_enabled(node != none or w.network.selected_wires().length > 0)
self.rename.set_enabled(node != none)
self.cut.set_enabled(node != none)
self.copy.set_enabled(node != none)
self.duplicate.set_enabled(node != none)
# The system clipboard is read only when pasting (another window may
# have copied nodes since); without it, while a copy is kept.
self.paste.set_enabled(self.clipboard.system or self.clipboard.kept != "")
self.enabled.set_enabled(node != none)
self.freeze.set_enabled(node != none)
self.display.set_enabled(node != none)
self.parent.set_enabled(w.network.scope >= 0 and not w.sketching())
# Only a data level has an Output node (docs/NETWORK-LEVELS.md).
self.set_output.set_enabled(node != none and w.network.level_of(w.network.scope) == "data")
# A sketch being edited takes single keys for its tools (D, O, …).
if w.sketching():
for command in [self.display, self.enabled, self.cut, self.copy, self.paste, self.duplicate] + self.tools:
command.set_enabled(false)
self.group.set_enabled(node != none and not w.network.in_material())
if let selected = node:
self.enabled.set_checked(selected.enabled)
self.freeze.set_checked(selected.frozen != "")
self.display.set_checked(selected.visible)
let holding = w.holds(selected.kind)
self.clear_edits.set_text("Reload Input" if holding else "Clear edits")
self.clear_edits.set_enabled(is_edit(selected.kind) and (selected.edits.length > 0 or selected.held != none))
else:
self.clear_edits.set_enabled(false)
self.cad_refresh()
self.model.refresh()
let selecting = w.selecting()
self.select_all.set_enabled(selecting)
# Where pick geometry hides inside surfaces (Edit SDF) boxes always
# select through; a sketch is flat.
let kind = w.selection_kind()
self.through_depth.set_enabled(selecting and not w.sketching() and not (kind != none and (kind else trap("kind")).see_through))
self.through_depth.set_checked(modeling_commands.selects_through(w))
let levels = w.levels()
let sketching = w.sketching()
for index in 0..<self.context_modes.length:
self.context_modes[index].set_checked(index == w.display.context_mode)
for index in 0..<self.modes.length:
self.modes[index].set_enabled(index in w.shown_levels())
self.modes[index].set_checked(index == (w.sketch_level if sketching else w.component_mode))
if sketching and index < sketch_level_names().length:
self.modes[index].set_text(sketch_level_names()[index])
elif index < levels.length and levels[index].available:
self.modes[index].set_text(levels[index].name)
for command in self.create:
command.set_enabled(w.editing() and w.tool_available(command.text()))
# Move, Rotate, Scale and Manipulator need something to act on.
for tool in self.tools:
if not w.sketching():
tool.set_enabled(tool is self.tools[0] or w.transform_target())
tool.set_checked(tool.text() == w.tool_in_use())
# Tools apply to the selection, or prompt for one (Tool → Select);
# on the Edit node they need a selection.
# A tool's icon follows the context (context_icon): looked up again
# only when the context changes, not on every refresh (a drag's).
let icon_key = f"{w.network.context()}|{w.editing()}|{w.strip_kind().name}"
let icons_stale = icon_key != self.icon_context
self.icon_context = icon_key
let picked = w.selected_count() > 0
for command in self.modeling:
command.set_enabled(selecting and w.tool_available(command.text()) and (not w.editing() or picked or runs_whole(w, command.text()) or command.text() in w.strip_kind().draw_tools))
if icons_stale:
let icon = context_icon(w, command.text())
if command.icon() != icon: command.set_icon(icon)
self.repeat_tool.set_enabled(selecting and w.last_tool != "" and w.tool_available(w.last_tool))
self.clear_snap_target.set_enabled(w.snap_target >= 0)
self.repeat_tool.set_text("Repeat " + w.last_tool if w.last_tool != "" else "Repeat")
self.selection.refresh(selecting)
# The arrows nudge picked CVs while the viewport has the keyboard and
# the Move tool is on (Alias's step size); the timeline keeps them
# otherwise.
let nudging = w.viewport_focus and component_moves.nudges(w)
for command in [self.play, self.previous_frame, self.next_frame, self.first_frame]:
command.set_enabled(not nudging)
self.grid.set_checked(w.view.grid_visible)
self.hulls.set_checked(w.display.hulls_visible)
self.four_views.set_checked(w.panes.four)
self.full_screen.set_checked(w.panes.full_screen)
## The CAD hand-offs apply to what is edited or selected.
func cad_refresh(self) -> unit!:
let w = self.workspace
let sketching = cad_commands.in_sketch(w)
self.finish_sketch.set_enabled(sketching)
self.slice.set_enabled(sketching)
self.redefine_plane.set_enabled(sketching)
self.slice.set_checked(w.slice)
let shown = cad_commands.shown_profiles(w)
self.extrude_profiles.set_enabled(sketching or shown)
self.revolve_profiles.set_enabled(sketching or shown)
self.extrude_profiles.set_text("Extrude profile" if w.sketch_profiles.length == 1 else ("Extrude profiles" if w.sketch_profiles.length > 1 else "Extrude"))
let faces = cad_commands.selected_faces(w).length > 0
self.sketch_on_face.set_enabled(faces)
let edges = cad_commands.selected_edges(w).length > 0
self.extrude_face.set_enabled(faces)
self.press_pull.set_enabled(sketching or shown or faces or edges)
pub func close(self):
self.selection.close()
self.model.close()
for connection in self.connections:
connection.disconnect()
self.connections.clear()
## Tool `label`'s icon where the viewport is: one command serves a label in
## every context, but the tool it runs is the context's, so it draws that
## tool's icon (Edit SDF's Intersect a boolean, Edit CAD's two surfaces
## crossing; Edit CAD's Reverse a curve's direction, Edit Mesh's normals).
## In a CAD network the label is its CAD node's.
pub func context_icon(w: Workspace, label: str) -> str:
if not (w.network.context() == "cad" and not w.editing()):
if let tool = w.strip_kind().tool(label):
if tool.icon != "": return tool.icon
return asset_name(label)
## Whether tool `name` of the viewport's kind runs on everything with
## nothing selected (a registry tool's `whole`: Subdivide, Clean).
func runs_whole(w: Workspace, name: str) -> bool:
let tool = w.strip_kind().tool(name) else return false
let registered = tool.tool else return false
# A primitive makes a piece of its own, and Poly Pen and Sculpt work
# under the pointer: none needs a selection.
return registered.whole or registered.inputs == 0 or registered.gesture in ["create", "pen", "sculpt"]
## Every tool's label once: the Edit kinds' tools, then the CAD nodes'.
func tool_names() -> list[str]:
let result = edit_tool_names()
for entry in tool_registry.cad_nodes():
if not (entry.label in result):
result.append(entry.label)
return result