dymokomi/luced-3d
Source of release 0.11.7
0.11.7 / src / project_graph.luc
## Versioned graph schema, the one node serializer for project files and the
## compute worker's delta requests: authored nodes and Edit recipes, never
## cooked caches. A node is a header (identity, wiring, flags) and a body (what
## its cook reads); requests send bodies only when a node's stamp changed.
from luce_prism.prism import Document, Value
from network import Network
from node import Node
from edit_operations import EditStep
import sketch_node
from luce_cad.sketch import CadSketch
from luce_cad.model import CadTolerances
import project_values as values
from luce_ui import ui
from edit_kinds import is_edit, EditKinds
from take import Take
import takes
import node_catalog
import code_cook
import script_cook
import levels
pub func write_graph(doc: Document, graph: Network) -> unit!:
write_scene(doc, graph)
doc.set("/scene", "next_id", Value.integer(graph.next_id))
for node in graph.nodes:
let path = f"/scene/nodes/n{node.id}"
write_header(doc, path, node)
write_body(doc, path, node, none)
write_takes(doc, graph)
## The takes, the active one's state as the nodes hold it.
func write_takes(doc: Document, graph: Network) -> unit!:
doc.set("/scene", "take", Value.integer(graph.take))
doc.add("/scene/takes", "takes")
let saved = takes.current(graph)
for at in 0..<saved.length:
let take = saved[at]
let path = f"/scene/takes/t{at}"
doc.add(path, "take")
doc.set(path, "name", Value.text(take.name))
values.put_ids(doc, path, "ids", take.ids)
let visible: list[float] = []
let enabled: list[float] = []
for k in 0..<take.ids.length:
visible.append(1.0 if take.visible[k] else 0.0)
enabled.append(1.0 if take.enabled[k] else 0.0)
doc.set(path, "visible", Value.numbers([visible.length], visible))
doc.set(path, "enabled", Value.numbers([enabled.length], enabled))
doc.set(path, "cameras", Value.strings([take.cameras.length], take.cameras))
doc.set(path, "hidden", Value.strings([take.hidden.length], take.hidden))
values.put_ids(doc, path, "counts", take.counts)
doc.set(path, "transforms", Value.numbers([take.transforms.length], take.transforms))
func read_takes(doc: Document, graph: Network) -> unit!:
let count = doc.child_count("/scene/takes")
if doc.type_at("/scene/takes") != "takes" or count < 1 or count > 256: error(ui.invalid, "Invalid project takes")
graph.takes = []
for at in 0..<count:
let path = f"/scene/takes/t{at}"
if not doc.contains(path) or doc.type_at(path) != "take": error(ui.invalid, "Invalid project take")
let take = Take(values.line(values.text(doc, path, "name")))
take.ids = values.ids(doc, path, "ids", 128, 1)
let visible = values.numbers(doc, path, "visible", take.ids.length, take.ids.length)
let enabled = values.numbers(doc, path, "enabled", take.ids.length, take.ids.length)
let cameras = doc.get(path, "cameras")
let hidden = doc.get(path, "hidden")
if cameras.rank() != 1 or cameras.count() != take.ids.length or hidden.rank() != 1 or hidden.count() != take.ids.length: error(ui.invalid, "Invalid take cameras or hidden objects")
take.counts = values.ids(doc, path, "counts", 128, 0)
if take.counts.length != take.ids.length: error(ui.invalid, "Invalid take transforms")
var total = 0
for width in take.counts:
total += width
take.transforms = values.numbers(doc, path, "transforms", total, total)
for k in 0..<take.ids.length:
take.visible.append(visible[k] != 0.0)
take.enabled.append(enabled[k] != 0.0)
take.cameras.append(values.line(cameras.text_at(k)))
take.hidden.append(hidden.text_at(k))
graph.takes.append(take)
graph.take = values.integer(doc, "/scene", "take", 0, count - 1)
pub func write_scene(doc: Document, graph: Network) -> unit!:
doc.add("/scene", "luced3d")
doc.set("/scene", "version", Value.integer(1))
doc.set("/scene", "selected", Value.integer(graph.selected))
doc.set("/scene", "display", Value.integer(graph.display))
doc.set("/scene", "scope", Value.integer(graph.scope))
doc.set("/scene", "units", Value.text(graph.units))
let t = graph.tolerances
doc.set("/scene", "tolerances", Value.numbers([4], [t.positional, t.fit, t.tangent, t.curvature]))
doc.set("/scene", "time", Value.numbers([4], [graph.frame, graph.start, graph.end, graph.fps]))
doc.add("/scene/nodes", "network")
pub func write_header(doc: Document, path: str, node: Node) -> unit!:
doc.add(path, node.kind)
doc.set(path, "id", Value.integer(node.id))
doc.set(path, "name", Value.text(node.name))
doc.set(path, "parent", Value.integer(node.parent))
doc.set(path, "visible", Value.boolean(node.visible))
doc.set(path, "position", Value.numbers([2], [node.x, node.y]))
values.put_ids(doc, path, "inputs", node.inputs)
values.put_ids(doc, path, "sources", node.sources)
## With `shared`, Edit step groups travel once under /scene/lists by content
## key; the map collects the groups this document must carry. Projects inline
## them. Without `steps` the node's Edit steps stay behind (the worker reads a
## holding node's result by its key: a long Sculpt stroke is not sent again
## on every pointer move).
pub func write_body(doc: Document, path: str, node: Node, shared: map[int, str]?, steps: bool = true) -> unit!:
doc.set(path, "enabled", Value.boolean(node.enabled))
doc.set(path, "parameters", Value.numbers([node.values.length], node.values))
doc.set(path, "texts", Value.strings([node.texts.length], node.texts))
doc.set(path, "guard", Value.integer(node.guard))
doc.set(path, "hidden", Value.strings([node.hidden.length], node.hidden))
doc.set(path, "frozen", Value.text(node.frozen))
doc.set(path, "time_dependent", Value.boolean(node.time_dependent))
# A holding Edit node's result: by its key for the worker, by its file
# for a project (project_file.luc writes it; its last action baked).
doc.set(path, "held_key", Value.integer(node.held_key))
doc.set(path, "held_note", Value.text(node.held_note))
doc.set(path, "held_failure", Value.text(node.held_failure))
doc.set(path, "held_file", Value.text(node.held_file))
doc.set(path, "held_input", Value.integer(node.held_input))
doc.set(path, "held_actions", Value.integer(node.held_actions))
doc.set(path, "held_aligns", Value.text(node.held_aligns))
# An action running on the worker (edit_held.luc): requests only.
if shared != none:
doc.set(path, "held_base", Value.integer(node.held_base))
doc.add(path + "/edits", "recipes")
let written = node.edits.length if steps else 0
doc.set(path, "edit_count", Value.integer(written))
for at in 0..<written:
let edit = node.edits[at]
let recipe = path + f"/edits/e{at}"
doc.add(recipe, "step")
doc.set(recipe, "verb", Value.text(edit.verb))
if let groups = shared:
doc.set(recipe, "group_key", Value.integer(edit.group_key))
groups[edit.group_key] = edit.group
else:
doc.set(recipe, "group", Value.text(edit.group))
doc.set(recipe, "group_type", Value.integer(edit.group_type))
doc.set(recipe, "numbers", Value.numbers([edit.numbers.length], edit.numbers))
doc.set(recipe, "texts", Value.strings([edit.texts.length], edit.texts))
doc.set(recipe, "topology", Value.integer(edit.topology))
doc.set(recipe, "symmetry", Value.integer(edit.symmetry))
doc.set(recipe, "keep", Value.boolean(edit.keep))
## Worker requests come from the live graph: `trusted` skips the text-field
## checks projects need (they would build UI widgets on the worker thread).
pub func read_header(doc: Document, path: str, trusted: bool = false) -> Node!:
let id = values.integer(doc, path, "id", 1, 2147483646)
let position = values.numbers(doc, path, "position", 2, 2)
let node = Node(id, doc.type_at(path), position[0], position[1])
apply_header(doc, path, node, trusted)
return node
## `node` (of the header's id and kind) taking the header's name, place,
## parent, visibility and wires: what a worker request changes of a node
## whose body stays.
pub func apply_header(doc: Document, path: str, node: Node, trusted: bool = false) -> unit!:
let position = values.numbers(doc, path, "position", 2, 2)
node.x = position[0]
node.y = position[1]
let name = values.text(doc, path, "name")
node.name = name if trusted else values.line(name)
node.parent = values.integer(doc, path, "parent", -1, 2147483646)
node.visible = values.flag(doc, path, "visible")
# Its kind's inputs, or none on an object level (read_graph checks which).
let inputs = values.ids(doc, path, "inputs", node.inputs.length)
if inputs.length != node.inputs.length and inputs.length != 0: error(ui.invalid, "Invalid input port count")
node.inputs = inputs
let sources = values.ids(doc, path, "sources", node.sources.length)
if sources.length != inputs.length: error(ui.invalid, "Invalid input source count")
node.sources = sources
pub func has_body(doc: Document, path: str) -> bool:
return doc.has_property(path, "enabled")
## A project's text `text` for slot `slot` of a `kind` node, checked: a
## Sketch's sketch must read as one (it is lines of entities and
## constraints) and a Code node's code may be many lines; other texts are
## single lines, as their fields hold.
func checked_text(kind: str, slot: int, text: str) -> str!:
if kind == sketch_node.kind_name and slot == 0:
_ = CadSketch.parse(text)
return text
# A Code node's body, header and spare list are lines of text.
if kind == script_cook.kind_name and slot in [script_cook.code_slot, script_cook.lock_slot, code_cook.spares_slot, code_cook.ramps_slot]:
if text.byte_count > 1048576: error(ui.invalid, "A Script node's text exceeds 1 MiB")
return text
if kind == code_cook.kind_name and slot >= code_cook.body_slot:
if text.byte_count > 1048576: error(ui.invalid, "A Code node's text exceeds 1 MiB")
return text
return values.line(text)
## `shared` resolves group keys (worker requests); projects inline them.
pub func read_body(doc: Document, path: str, node: Node, shared: map[int, str]?, kinds: EditKinds) -> unit!:
node.enabled = values.flag(doc, path, "enabled")
node.values = values.numbers(doc, path, "parameters", node.values.length, node.values.length)
let texts = doc.get(path, "texts")
if texts.rank() != 1 or texts.count() != node.texts.length: error(ui.invalid, "Invalid text parameter count")
for at in 0..<texts.count():
let text = texts.text_at(at)
node.texts[at] = text if shared != none else checked_text(node.kind, at, text)
node.guard = values.stamp(doc, path, "guard")
let hidden = doc.get(path, "hidden")
if hidden.rank() != 1 or hidden.count() > 4096: error(ui.invalid, "Invalid hidden object paths")
node.hidden = []
for at in 0..<hidden.count():
let text = hidden.text_at(at)
node.hidden.append(text if shared != none else values.line(text))
let frozen = values.text(doc, path, "frozen")
node.frozen = frozen if shared != none else values.line(frozen)
node.time_dependent = values.flag(doc, path, "time_dependent")
node.held_key = values.stamp(doc, path, "held_key")
node.held_note = values.text(doc, path, "held_note")
node.held_failure = values.text(doc, path, "held_failure")
node.held_file = values.text(doc, path, "held_file")
node.held_input = values.stamp(doc, path, "held_input")
node.held_actions = values.integer(doc, path, "held_actions", 0, 1000000000)
node.held_aligns = values.text(doc, path, "held_aligns")
if shared != none:
node.held_base = values.stamp(doc, path, "held_base")
let count = values.integer(doc, path, "edit_count", 0, 100000)
if count != doc.child_count(path + "/edits") or (not is_edit(node.kind) and count != 0):
error(ui.invalid, "Invalid Edit recipe collection")
node.edits = []
# Each Edit kind's own steps (a sketch's Create Rectangle, an SDF's
# SDF Mode), not only geometry verbs.
let kind = (kinds.of(node.kind) else error(ui.invalid, "Unknown Edit operation")) if count > 0 else none
for at in 0..<count:
let recipe = path + f"/edits/e{at}"
if doc.type_at(recipe) != "step":
error(ui.invalid, "Unknown Edit recipe")
let verb = values.text(doc, recipe, "verb")
if not (kind else error(ui.invalid, "Unknown Edit operation")).knows(verb):
error(ui.invalid, "Unknown Edit operation")
var group = ""
if let groups = shared:
group = groups[values.stamp(doc, recipe, "group_key")] else error(ui.invalid, "Missing Edit group")
else:
group = values.text(doc, recipe, "group")
# A Sculpt stroke or a Lattice carries its dabs and moves here.
let numbers = values.numbers(doc, recipe, "numbers", 0, 1048576)
let stored = doc.get(recipe, "texts")
if stored.rank() != 1 or stored.count() > 16: error(ui.invalid, "Invalid Edit step texts")
let step_texts: list[str] = []
for index in 0..<stored.count():
let text = stored.text_at(index)
if text.byte_count > 1048576: error(ui.invalid, "An Edit step's text exceeds 1 MiB")
step_texts.append(text)
node.edits.append(EditStep(verb, group, values.integer(doc, recipe, "group_type", 0, 4), numbers, values.stamp(doc, recipe, "topology"),
values.integer(doc, recipe, "symmetry", 0, 3), values.flag(doc, recipe, "keep"), step_texts))
## Parse fully and check references/cycles before a caller replaces live state.
pub func read_graph(doc: Document) -> Network!:
if not doc.contains("/scene") or doc.type_at("/scene") != "luced3d" or values.integer(doc, "/scene", "version", 1, 1) != 1:
error(ui.invalid, "Not a supported Luced 3D project")
let graph = Network(false)
if doc.type_at("/scene/nodes") != "network": error(ui.invalid, "Missing project network")
if doc.child_count("/scene/nodes") > 128:
error(ui.invalid, "Project exceeds 128 nodes")
for index in 0..<doc.child_count("/scene/nodes"):
let path = doc.child_at("/scene/nodes", index) else error(ui.invalid, "Missing project node")
let node = read_header(doc, path)
if graph.find(node.id) != none: error(ui.invalid, "Duplicate project node ID")
read_body(doc, path, node, none, graph.kinds)
graph.nodes.append(node)
graph.next_id = values.integer(doc, "/scene", "next_id", 1, 2147483647)
for node in graph.nodes:
if node.id >= graph.next_id: error(ui.invalid, "Invalid next node ID")
let ancestors: list[int] = [node.id]
var parent = node.parent
while parent >= 0:
if parent in ancestors or ancestors.length > 16: error(ui.invalid, "Invalid Group hierarchy")
ancestors.append(parent)
let group = graph.require(parent)
if not node_catalog.holds_nodes(group.kind): error(ui.invalid, "A node parent must be a context node")
parent = group.parent
# Each node sits in a network that may hold it, as authoring allows.
let context = graph.context_at(node.parent)
if not node_catalog.allowed(node.kind, context): error(ui.invalid, f"{node.kind} can't be in a {node_catalog.context_label(context)} network")
if node.inputs.length != graph.input_count(node.kind, node.parent): error(ui.invalid, "Invalid input port count")
for input in node.inputs:
if input == -1: continue
if graph.require(input).parent != node.parent or graph.depends_on(input, node.id):
error(ui.invalid, "Invalid or cyclic project connection")
# A data level's display flag is a radio button.
for node in graph.nodes:
if node_catalog.holds_nodes(node.kind) and graph.level_of(node.id) == levels.data_level and graph.display_count(node.id) > 1:
error(ui.invalid, "Several display flags in one network")
read_scene(doc, graph)
read_takes(doc, graph)
if graph.scope != -1 and not node_catalog.holds_nodes(graph.require(graph.scope).kind): error(ui.invalid, "Invalid network scope")
for id in [graph.selected, graph.display]:
if id != -1 and graph.require(id).parent != graph.scope: error(ui.invalid, "Selection outside network scope")
return graph
pub func read_scene(doc: Document, graph: Network) -> unit!:
graph.selected = values.integer(doc, "/scene", "selected", -1, 2147483646)
graph.display = values.integer(doc, "/scene", "display", -1, 2147483646)
graph.scope = values.integer(doc, "/scene", "scope", -1, 2147483646)
let units = values.text(doc, "/scene", "units")
if not (units in ["mm", "in"]): error(ui.invalid, "Invalid project units")
graph.units = units
let numbers = values.numbers(doc, "/scene", "tolerances", 4, 4)
let tolerances = CadTolerances(positional = numbers[0], fit = numbers[1], tangent = numbers[2], curvature = numbers[3])
tolerances.checked() catch failure:
error(ui.invalid, f"Invalid project tolerances: {failure.message}")
graph.tolerances = tolerances
let time = values.numbers(doc, "/scene", "time", 4, 4)
if not (time[3] > 0.0 and time[3] <= 1000.0 and time[2] >= time[1]): error(ui.invalid, "Invalid project timeline")
graph.frame = time[0]
graph.start = time[1]
graph.end = time[2]
graph.fps = time[3]