Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / import_node.luc

## The Import node, Houdini's Object Merge: inside a data network, the result
## of other nodes, named by path, with no wire. Objects are not wired
## together at the object level (docs/NETWORK-LEVELS.md); this is how one
## object reads another.
##
## Its Object and Transform Object rows are node paths (node_paths.luc:
## absolute or relative, Object with wildcards, kept on their nodes through
## renames and regrouping); Object's matches merge in authoring order.
##
## Transform (Houdini's menu, with its Transform Object and Invert
## Transform; Into This Object to start): each source's geometry starts in
## its own frame, the frame Houdini calls its object's (`frame`); None
## leaves it there, Into This Object carries it into the Import's frame,
## Into Specified Object into the Transform Object's (blank: the world;
## ".": the source's own, as None). Invert carries it the other way.
## `carry` writes the carry as placement steps; Network.import_steps
## chooses the frames.
from luce_3d.graphics import Vector3
from geometry_data import GeometryData
from node import Node
import node_catalog
import node_paths

pub let kind_name = "Import"

## Text slots: the Object path and the Transform Object path.
pub let object_slot = 0
pub let transform_object_slot = 1

## Value slots: the Transform menu and its choices, and Invert Transform.
pub let transform_slot = 0
pub let no_transform = 0.0
pub let into_this_object = 1.0
pub let into_specified_object = 2.0
pub let invert_slot = 1

# -- Frames and placement steps

## The containers whose placements carry a node's own frame into the
## world, innermost first: a container that places what it yields starts
## with itself (Houdini's object, whose geometry lies in its frame), any
## other node with the containers it sits in.
pub func frame(nodes: list[Node], node: Node) -> list[Node]:
    let result: list[Node] = []
    for id in node_paths.chain(nodes, node.id if node_catalog.places_children(node.kind) else node.parent):
        if let container = find(nodes, id):
            if node_catalog.places_children(container.kind):
                result.append(container)
    return result

## The placement steps that carry `source`'s result from frame `out_of`
## into frame `into` (each from `frame`). The result arrives already placed
## by its own container when `source` places what it yields, so that
## placement is undone first, unless `out_of` begins with it. Containers
## both frames share from the world down cancel.
pub func carry(source: Node, out_of: list[Node], into: list[Node]) -> list[float]!:
    var out = out_of.length
    var back = into.length
    while out > 0 and back > 0 and out_of[out - 1].id == into[back - 1].id:
        out -= 1
        back -= 1
    let result: list[float] = []
    var first = 0
    if node_catalog.places_children(source.kind):
        if out > 0 and out_of[0].id == source.id:
            first = 1
        else:
            result.extend(inverse_steps(source))
    for at in first..<out:
        result.extend(placement_step(out_of[at]))
    for at in 0..<back:
        result.extend(inverse_steps(into[back - 1 - at]))
    return result

## The step that places a container's yield (translation, rotation, scale),
## as nine numbers.
pub func placement_step(container: Node) -> list[float]:
    let (t, r, s) = container.placement_parts()
    return [t.x, t.y, t.z, r.x, r.y, r.z, s.x, s.y, s.z]

## The steps that undo a container's placement: its translation, then its
## rotation about X, Y and Z in turn (Euler XYZ undone), then its scale.
pub func inverse_steps(container: Node) -> list[float]!:
    let (t, r, s) = container.placement_parts()
    return [-t.x, -t.y, -t.z, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
            0.0, 0.0, 0.0, -r.x, 0.0, 0.0, 1.0, 1.0, 1.0,
            0.0, 0.0, 0.0, 0.0, -r.y, 0.0, 1.0, 1.0, 1.0,
            0.0, 0.0, 0.0, 0.0, 0.0, -r.z, 1.0, 1.0, 1.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 / s.x, 1.0 / s.y, 1.0 / s.z]

## `data` placed by `steps` in turn (identity steps cost nothing).
pub func placed(data: GeometryData, steps: list[float]) -> GeometryData!:
    var result = data
    var at = 0
    while at + 9 <= steps.length:
        result = result.transformed(Vector3(steps[at], steps[at + 1], steps[at + 2]), Vector3(steps[at + 3], steps[at + 4], steps[at + 5]),
                                    Vector3(steps[at + 6], steps[at + 7], steps[at + 8]))
        at += 9
    return result

func find(nodes: list[Node], id: int) -> Node?:
    for node in nodes:
        if node.id == id:
            return node
    return none