Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.4

0.11.4 / src / cache_node.luc

## The Cache node (Houdini's File Cache): writes its input's cooked geometry
## to a .prism file and passes it through; with Load from disk it reads the
## file instead and never cooks upstream. The file records the node's stamp,
## and in read mode the node's stamp is the one recorded, so switching to
## read mode keeps every downstream result, and upstream edits no longer
## reach downstream nodes.
from luce_geocore.geocore import GeometryFile
from luce_cad.cad_geometry import CadGeometry
from geometry_data import GeometryData
from luce_ui import ui

## Whether a node reads its cache file (and not its input).
pub func reads_file(kind: str, enabled: bool, values: list[float]) -> bool:
    return kind == "Cache" and enabled and values[0] != 0.0

## A Cache node's stamp: in write mode the one its input gives it
## (`written`); in read mode the one its file was written with.
pub func cache_stamp(reads: bool, path: str, revision: int, written: int) -> int!:
    if not reads:
        return written
    let file = path.trim()
    let key = GeometryFile.key(file) if file != "" else 0
    if key != 0:
        return key
    return nonzero(("Cache", "unwritten", file, revision).hash())

## Cook: load the file, or save the input when the file does not hold it
## (`stamp` is the node's, which the file records).
pub func cook_cache(reads: bool, path: str, stamp: int, data: GeometryData) -> GeometryData!:
    let file = path.trim()
    # CAD models are written and read by luce-cad's codec (registering again
    # is harmless; the app registers it at startup).
    _ = CadGeometry.register_codec()
    if reads:
        if file == "":
            error(ui.invalid, "Choose a cache file to load")
        return GeometryData(geometry = GeometryFile.load(file))
    if file == "":
        let passed = GeometryData(geometry = data.geometry)
        passed.warning = "No cache file: nothing is written"
        return passed
    if stamp == 0 or GeometryFile.key(file) != stamp:
        _ = GeometryFile.save(data.geometry, file, stamp)
    return data

## A frozen node's result (Houdini's lock): written once to `path` with
## the node's stamp, read back from then on; its stamp is the file's, so
## upstream edits stop there and downstream results stay.
pub func freeze_to(data: GeometryData, path: str, stamp: int) -> unit!:
    _ = CadGeometry.register_codec()
    _ = GeometryFile.save(data.geometry, path, nonzero(stamp))

## A frozen node's stamp: the one its file was written with.
pub func frozen_stamp(path: str) -> int!:
    let key = GeometryFile.key(path) if path != "" else 0
    return key if key != 0 else nonzero(("Frozen", "missing", path).hash())

## A frozen node's result, read from its file.
pub func load_frozen(path: str) -> GeometryData!:
    _ = CadGeometry.register_codec()
    return GeometryData(geometry = GeometryFile.load(path))

func nonzero(value: int) -> int:
    return value if value != 0 else 1