Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.3

0.11.3 / 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 script_node
import script_cook
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 displays it: its tools edit the geometry
## the viewport shows, however the node was reached.
pub func select_node(w: Workspace, id: int) -> unit!:
    let node = w.network.require(id)
    w.network.picked = [id]
    if node.id != w.network.selected:
        w.placing = none
    let shows = is_edit(node.kind) and (w.network.display != id or w.network.preview)
    if w.network.selected != id or shows:
        w.network.selected = id
        if shows:
            w.network.display = id
            w.network.preview = false
        w.selection = none
        w.end_modal_tool()
        w.revision += 1
        if w.compute != none or shows:
            w.rebuild()

pub func display_selected(w: Workspace) -> unit!:
    let node = w.selected() else return
    w.perform("Preview node", func () -> unit!:
        w.network.preview = not (w.network.preview and w.network.display == node.id)
        w.network.display = node.id
        w.selection = none
        w.rebuild())

pub func toggle_output(w: Workspace) -> unit!:
    let node = w.selected() else return
    let output = not w.network.is_output(node.id)
    set_output(w, node.id, 1 if output else -1)

pub func auto_output(w: Workspace) -> unit!:
    let node = w.selected() else return
    set_output(w, node.id, 0)

pub func set_output(w: Workspace, id: int, state: int) -> unit!:
    if state < -1 or state > 1:
        return
    w.perform("Change Out flag", func () -> unit!:
        let node = w.network.require(id)
        node.output = state
        w.network.invalidate(id)
        w.rebuild())

pub func toggle_visible(w: Workspace) -> unit!:
    let node = w.selected() else return
    w.perform("Toggle visibility", func () -> unit!:
        node.visible = not node.visible
        w.network.visibility_changed(node)
        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.
pub func pick_node(w: Workspace, id: int, toggle: bool) -> unit!:
    if not toggle:
        select_node(w, id)
        return
    let picked = w.network.selection()
    if let at = picked.index_of(id):
        _ = picked.remove_at(at)
        if picked.length == 0:
            return
        select_node(w, picked[picked.length - 1])
    else:
        picked.append(id)
        select_node(w, id)
    w.network.picked = picked
    w.revision += 1

## Pick `ids` (a box), added to the picked nodes with `add`.
pub func pick_nodes(w: Workspace, ids: list[int], add: bool) -> unit!:
    let picked = w.network.selection() if add else []
    for id in ids:
        if not (id in picked):
            picked.append(id)
    if picked.length == 0:
        return
    select_node(w, picked[picked.length - 1])
    w.network.picked = picked
    w.revision += 1

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.network.scope = id
    let children = w.network.local_nodes()
    w.network.selected = children[0].id if children.length > 0 else -1
    w.network.display = w.network.selected
    w.network.preview = false
    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.network.scope = node.parent
    w.network.selected = node.id
    w.network.display = node.id
    w.network.preview = false
    w.selection = none
    w.rebuild()

pub func reveal_node(w: Workspace, id: int) -> unit!:
    let node = w.network.require(id)
    w.network.scope = node.parent
    w.network.preview = false
    w.network.display = 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.revision += 1
        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 source = w.network.selected
        let node = w.network.add(kind, x, y)
        # A Script node's starter names parameters: their rows.
        if kind == script_cook.kind_name:
            script_node.refresh(node)
        # A geometry node takes the selected one as its input; shader nodes
        # are wired by their sockets.
        if node.inputs.length > 0 and not is_shader(kind) and source >= 0 and w.network.require(source).parent == node.parent:
            w.network.connect(source, node.id, 0)
        w.network.selected = node.id
        w.network.display = node.id
        w.network.preview = false
        w.selection = none
        w.end_modal_tool()
        w.rebuild())

pub func delete_selected(w: Workspace) -> unit!:
    let ids = w.network.selection()
    if ids.length == 0:
        return
    w.perform("Delete node" if ids.length == 1 else "Delete nodes", func () -> unit!:
        for id in ids:
            if w.network.find(id) != none:
                w.network.remove(id)
        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.
    w.perform("Rename node", func () -> unit!:
        node.name = name
        if w.compute != none: w.rebuild())

pub func set_parameter(w: Workspace, slot: int, value: float) -> unit!:
    let node = w.selected() else return
    if slot < 0 or slot >= node.values.length or not math.is_finite(value):
        return
    if node.values[slot] == value:
        return
    w.perform("Change " + node.kind, func () -> unit!:
        if node.kind in ["Transform", "Group", "CAD"] and slot >= 7 and math.abs(value) < 0.000001:
            error(ui.invalid, "Scale cannot be zero")
        var accepted = value
        for parameter in node_catalog.parameters(node.kind, node.texts[0]):
            if parameter.slot == slot:
                accepted = math.max(parameter.minimum, math.min(parameter.maximum, accepted))
                if parameter.integer:
                    accepted = math.floor(accepted + 0.5)
        node.values[slot] = accepted
        if node.kind == script_cook.kind_name and slot == script_cook.time_slot:
            script_node.refresh_time(node)
        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, value: str) -> unit!:
    let node = w.selected() else return
    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())