dymokomi/luced-3d
Source of release 0.11.8
0.11.8 / src / node_commands.luc
## Commands on the node network, each one undo step through
## Workspace.perform: selecting, displaying and flagging nodes, entering and
## leaving Groups, adding, deleting, wiring and moving nodes, and changing
## their parameters, names and files.
from workspace import Workspace
from edit_kinds import is_edit
from shader_nodes import is_shader
import node_catalog
import node_paths
import node_room
import levels
import script_node
import script_cook
import sketch_node
from luce_std import math
from luce_ui import ui
from grouping import group_nodes
from cache_node import freeze_to
from luce_std import files, paths
import time
## Selecting an Edit node also gives it the display flag: its tools edit
## the geometry the viewport shows, however the node was reached.
## Selecting a Sketch edits it, Fusion's way: it takes the display flag, so
## the viewport shows the model as of the sketch (the sketch passes its
## input's body on), and the node displayed before is kept to show again
## when the sketch is left (another node or nothing selected, or Finish
## Sketch), as Fusion rolls back and forward.
pub func select_node(w: Workspace, id: int) -> unit!:
let node = w.network.require(id)
w.network.picked = [id]
w.network.wires = []
if node.id != w.network.selected:
w.placing = none
let sketch = node.kind == sketch_node.kind_name and node.enabled
let shows = (is_edit(node.kind) or sketch) and w.network.display != id
if w.network.selected != id or shows:
# An Edit CAD sketch is edited while its node stays selected.
if id != w.edit_sketch_node:
w.edit_sketch = ""
w.edit_sketch_node = -1
if sketch and shows and w.sketch_return < 0:
w.sketch_return = w.network.display
elif not sketch and leave_sketch(w, is_edit(node.kind)):
w.rebuild()
w.network.selected = id
if shows:
w.network.set_display(id)
w.selection = none
w.end_modal_tool()
w.touch()
if w.compute != none or shows:
w.rebuild()
## D: the selected node's display flag (docs/NETWORK-LEVELS.md). On an
## object level it toggles (the node renders or not); on a data level it
## moves to the node, which the viewport then shows.
pub func display_selected(w: Workspace) -> unit!:
let node = w.selected() else return
toggle_display(w, node.id)
## Set Output: the selected node feeds its data level's Output node (made
## after it when there is none), which is what the container yields, as
## Houdini's Output SOP.
pub func set_output(w: Workspace) -> unit!:
let node = w.selected() else return
if w.network.level_of(node.parent) != levels.data_level:
w.notice = "Every displayed node renders here; an Output node goes inside a container"
w.touch()
return
if node.kind == levels.output_kind:
return
for other in w.network.children(node.parent):
if other.kind == levels.output_kind:
connect(w, node.id, other.id, 0)
return
add_node(w, levels.output_kind, node.x, node.y + node_room.row)
pub func toggle_display(w: Workspace, id: int) -> unit!:
let node = w.network.require(id)
if w.network.on_object_level(id):
w.perform("Display " + node.name if not node.visible else "Hide " + node.name, func () -> unit!:
node.visible = not node.visible
w.network.visibility_changed(node)
w.rebuild())
return
if node.visible and w.network.display == id:
return
w.perform("Display " + node.name, func () -> unit!:
w.network.set_display(id)
w.selection = none
w.rebuild())
## Put the picked nodes, and the nodes feeding them, in a new Group.
pub func group_selected(w: Workspace) -> unit!:
let ids = w.network.selection()
if ids.length == 0:
return
w.perform("Group", func () -> unit!:
_ = group_nodes(w.network, ids)
w.selection = none
w.rebuild())
## Pick node `id` alone, or with `toggle` add it to the picked nodes (it
## becomes the selected one) or take it out; picked wires stay picked.
pub func pick_node(w: Workspace, id: int, toggle: bool) -> unit!:
if not toggle:
select_node(w, id)
return
let wires = w.network.wires
let picked = w.network.selection()
if let at = picked.index_of(id):
_ = picked.remove_at(at)
if picked.length == 0:
select_nothing(w)
w.network.wires = wires
return
select_node(w, picked[picked.length - 1])
else:
picked.append(id)
select_node(w, id)
w.network.picked = picked
w.network.wires = wires
w.touch()
## Pick `ids` (a box): they alone (operation 0), added to the picked
## nodes (1), or taken from them (2). Picking none of them alone leaves
## the selection as it was.
pub func pick_nodes(w: Workspace, ids: list[int], operation: int) -> unit!:
let picked = w.network.selection() if operation != 0 else []
for id in ids:
let at = picked.index_of(id)
if operation == 2:
if let found = at:
_ = picked.remove_at(found)
elif at == none:
picked.append(id)
let wires = w.network.wires if operation != 0 else []
if picked.length == 0:
if operation == 2:
select_nothing(w)
w.network.wires = wires
return
let primary = w.network.selected if w.network.selected in picked else picked[picked.length - 1]
select_node(w, primary)
w.network.picked = picked
w.network.wires = wires
w.touch()
## A click on empty space, in the viewport or the network view: nothing is
## selected, so the Parameters pane is empty and the viewport offers only
## what applies without a node.
pub func select_nothing(w: Workspace) -> unit!:
if w.selected() == none and w.network.wires.length == 0:
return
if leave_sketch(w, false):
w.rebuild()
w.edit_sketch = ""
w.edit_sketch_node = -1
w.network.pick_nothing()
w.placing = none
w.selection = none
w.end_modal_tool()
w.touch()
## A sketch edited from a rolled-back display is left: the node displayed
## before shows again, unless the node selected next takes the display
## (`taken`) or that node is gone or in another network. Whether the display
## moved.
pub func leave_sketch(w: Workspace, taken: bool) -> bool!:
let back = w.sketch_return
w.sketch_return = -1
if back < 0 or taken:
return false
let node = w.network.find(back) else return false
if node.parent != w.network.scope or w.network.display == back:
return false
w.network.set_display(back)
return true
## Pick `wires` (each a node and its input port): they alone (operation
## 0, with no node), added to what is picked (1), or taken from it (2).
pub func pick_wires(w: Workspace, wires: list[(int, int)], operation: int) -> unit!:
if operation == 0:
w.network.pick_nothing()
let picked = w.network.wires.copy()
for wire in wires:
let at = picked.index_of(wire)
if operation == 2:
if let found = at:
_ = picked.remove_at(found)
elif at == none:
picked.append(wire)
w.network.wires = picked
w.touch()
pub func enter_group(w: Workspace, id: int) -> unit!:
let node = w.network.require(id)
if not node_catalog.holds_nodes(node.kind):
return
if w.in_transaction():
w.commit()
w.sketch_return = -1
w.network.scope = id
let children = w.network.local_nodes()
let shown = w.network.display_in(id)
w.network.selected = shown if shown >= 0 else (children[0].id if children.length > 0 else -1)
w.network.display = w.network.selected
w.selection = none
w.rebuild()
pub func leave_group(w: Workspace) -> unit!:
if w.network.scope < 0:
return
let node = w.network.require(w.network.scope)
if w.in_transaction():
w.commit()
w.sketch_return = -1
w.network.scope = node.parent
w.network.selected = node.id
w.network.display = node.id
w.selection = none
w.rebuild()
## Take the network view to node `id`'s network and select it (the
## Outliner's click). The display flag stays where it is.
pub func reveal_node(w: Workspace, id: int) -> unit!:
let node = w.network.require(id)
w.network.scope = node.parent
let shown = w.network.display_in(node.parent)
w.network.display = shown if shown >= 0 else id
w.network.selected = -1
select_node(w, id)
w.rebuild()
pub func toggle_enabled(w: Workspace) -> unit!:
let node = w.selected() else return
w.perform("Toggle node", func () -> unit!:
node.enabled = not node.enabled
w.network.invalidate(node.id)
w.selection = none
w.rebuild())
## Freeze the selected node (Houdini's lock): its result as it cooks now is
## written to a file it reads from then on, drawn red; upstream edits no
## longer reach it or what follows. Again: it cooks from its inputs.
pub func toggle_freeze(w: Workspace) -> unit!:
let node = w.selected() else return
if node.frozen != "":
w.perform("Unfreeze " + node.name, func () -> unit!:
node.frozen = ""
w.network.invalidate(node.id)
w.rebuild())
return
let data = w.network.copy().evaluate_data(node.id) catch failure:
w.notice = f"{node.name} cannot freeze: {failure.message}"
w.touch()
return
let folder = frozen_folder()
files.ensure_directory(folder)
let path = paths.join(folder, f"n{node.id}-{time.now()}.prism")
freeze_to(data, path, node.stamp)
w.perform("Freeze " + node.name, func () -> unit!:
node.frozen = path
w.network.invalidate(node.id)
w.rebuild())
## Where frozen results wait until their project is saved beside them.
func frozen_folder() -> str!:
return paths.join(paths.join(files.home_directory(), ".luced-3d"), "frozen")
pub func add_node(w: Workspace, kind: str, x: float, y: float) -> unit!:
w.perform("Add " + kind, func () -> unit!:
let context = w.network.context()
let refused = node_catalog.refusal(kind, context, w.network.has_inputs(w.network.scope))
if refused != "":
error(ui.invalid, refused)
let source = w.network.selected
let node = w.network.add(kind, x, y)
# The new node takes the display: nothing to roll forward to.
w.sketch_return = -1
# A Script node's starter names parameters: their rows.
if kind == script_cook.kind_name:
script_node.refresh(node)
# Scene objects are created independently of the selection. Inside
# data networks, operators take the selected node as their input.
if context != "scene" and node.inputs.length > 0 and not node_catalog.holds_nodes(kind) and not is_shader(kind) and source >= 0 and w.network.require(source).parent == node.parent:
w.network.connect(source, node.id, 0)
# Never on another node: what it covers moves down.
node_room.make_room(w.network, node.id)
w.network.selected = node.id
w.network.set_display(node.id)
w.selection = none
w.end_modal_tool()
w.rebuild())
## Delete the picked wires and nodes (one undo step).
pub func delete_selected(w: Workspace) -> unit!:
let ids = w.network.selection()
let wires = w.network.selected_wires()
if ids.length == 0 and wires.length == 0:
return
let label = ("Delete node" if ids.length == 1 else "Delete nodes") if ids.length > 0 else ("Delete wire" if wires.length == 1 else "Delete wires")
w.perform(label, func () -> unit!:
for (id, port) in wires:
w.network.disconnect(id, port)
w.network.wires = []
for id in ids:
if w.network.find(id) != none:
w.network.remove(id)
w.selection = none
w.rebuild())
## Disconnect `wires` (each a node and its input port) in one undo step:
## the network view's Y-drag cut.
pub func cut_wires(w: Workspace, wires: list[(int, int)]) -> unit!:
if wires.length == 0:
return
w.perform("Cut wire" if wires.length == 1 else "Cut wires", func () -> unit!:
for (id, port) in wires:
w.network.disconnect(id, port)
w.selection = none
w.rebuild())
pub func connect(w: Workspace, source: int, target: int, port: int, output: int = 0) -> unit!:
w.perform("Connect nodes", func () -> unit!:
w.network.connect(source, target, port, output)
w.selection = none
w.rebuild())
pub func disconnect(w: Workspace, target: int, port: int) -> unit!:
w.perform("Disconnect nodes", func () -> unit!:
w.network.disconnect(target, port)
w.selection = none
w.rebuild())
pub func move_node(w: Workspace, id: int, x: float, y: float) -> unit!:
w.perform("Move node", func () -> unit!:
let node = w.network.require(id)
node.x = x
node.y = y)
pub func rename(w: Workspace, name: str) -> unit!:
let node = w.selected() else return
if name == "" or name == node.name:
return
# Names reach the worker's messages and group paths, not its stamps;
# node paths follow the node (Network.rename).
w.perform("Rename node", func () -> unit!:
w.network.rename(node.id, name)
if w.compute != none: w.rebuild())
pub func set_parameter(w: Workspace, slot: int, value: float) -> unit!:
set_row(w, slot, [value])
## Set the selected node's value slots from `slot` on to `values`, each
## kept within its parameter's range, as one change with one cook (a vector
## row's components together).
pub func set_row(w: Workspace, slot: int, values: list[float]) -> unit!:
let node = w.selected() else return
if slot < 0 or slot + values.length > node.values.length:
return
for value in values:
if not math.is_finite(value):
return
if node.values[slot..<slot + values.length] == values:
return
w.perform("Change " + node.kind, func () -> unit!:
let parameters = node_catalog.parameters(node.kind, node.texts[0])
for at in 0..<values.length:
let target = slot + at
if node.kind in node_catalog.transforming_kinds() and target >= 7 and math.abs(values[at]) < 0.000001:
error(ui.invalid, "Scale cannot be zero")
var accepted = values[at]
for parameter in parameters:
if parameter.slot == target:
accepted = math.max(parameter.minimum, math.min(parameter.maximum, accepted))
if parameter.integer:
accepted = math.floor(accepted + 0.5)
node.values[target] = accepted
if node.kind == script_cook.kind_name and target == script_cook.time_slot:
script_node.refresh_time(node)
w.network.invalidate(node.id)
w.rebuild())
## Set the selected node's value slots from `slot` on to `values` at once
## (a value ramp's slots), unclamped, as one change.
pub func set_parameters(w: Workspace, slot: int, values: list[float]) -> unit!:
let node = w.selected() else return
if slot < 0 or slot + values.length > node.values.length or node.values[slot..<slot + values.length] == values:
return
w.perform("Change " + node.kind, func () -> unit!:
for at in 0..<values.length:
node.values[slot + at] = values[at]
w.network.invalidate(node.id)
w.rebuild())
## The selected node's first parameter (a primitive's size).
pub func set_size(w: Workspace, size: float) -> unit!:
set_parameter(w, 0, size)
## A verb node's ramp row in text slot `slot` now holds `numbers` (a ramp
## row's edit, in the undo step the panel's begin/commit bracket), as its
## text.
pub func set_ramp_text(w: Workspace, id: int, slot: int, numbers: list[float]) -> unit!:
let node = w.network.find(id) else return
let text = node_catalog.ramp_text(numbers)
if slot < 0 or slot >= node.texts.length or node.texts[slot] == text:
return
node.texts[slot] = text
w.network.invalidate(node.id)
w.rebuild()
pub func set_text(w: Workspace, slot: int, text: str) -> unit!:
let node = w.selected() else return
# A node path is kept normalized (node_paths.normalize).
let value = node_paths.normalize(text) if node_paths.row(node.kind, slot) != none else text
if slot < 0 or slot >= node.texts.length or node.texts[slot] == value:
return
w.perform("Change " + node.kind, func () -> unit!:
# A File node turning to USD or FBX takes that format's rows' defaults.
let format = node_catalog.file_format(value)
if node.kind == "File" and slot == 0 and (format == "usd" or format == "fbx") and node_catalog.file_format(node.texts[0]) != format:
for parameter in node_catalog.parameters("File", value):
node.values[parameter.slot] = parameter.initial
node.texts[slot] = value
w.network.invalidate(node.id)
w.rebuild())
pub func reload_file(w: Workspace) -> unit!:
let node = w.selected() else return
if node.kind != "File":
return
w.perform("Reload geometry file", func () -> unit!:
node.source_revision += 1
w.network.invalidate(node.id)
w.rebuild())
## Keep IDs: run the node's group on the new topology as numbers (Houdini's
## behaviour), clearing its guard.
pub func keep_ids(w: Workspace, id: int) -> unit!:
let node = w.network.require(id)
if node.guard == 0:
return
w.perform("Keep IDs", func () -> unit!:
node.guard = 0
w.network.invalidate(node.id)
w.rebuild())