dymokomi/luced-3d
Source of release 0.11.1
0.11.1 / 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, Node
from edit_operations import EditStep
import project_values as values
from luce_geocore.geocore import Verbs
from luce_ui import ui
from edit_kinds import is_edit
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)
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", "preview", Value.boolean(graph.preview))
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, "output", Value.integer(node.output))
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)
## 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.
pub func write_body(doc: Document, path: str, node: Node, shared: map[int, str]?) -> 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.add(path + "/edits", "recipes")
doc.set(path, "edit_count", Value.integer(node.edits.length))
for at in 0..<node.edits.length:
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, "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])
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.output = values.integer(doc, path, "output", -1, 1)
node.visible = values.flag(doc, path, "visible")
let inputs = values.ids(doc, path, "inputs", node.inputs.length)
if inputs.length != node.inputs.length: error(ui.invalid, "Invalid input port count")
node.inputs = inputs
return node
pub func has_body(doc: Document, path: str) -> bool:
return doc.has_property(path, "enabled")
## `shared` resolves group keys (worker requests); projects inline them.
pub func read_body(doc: Document, path: str, node: Node, shared: map[int, str]?) -> 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 values.line(text)
node.guard = values.stamp(doc, path, "guard") if doc.has_property(path, "guard") else 0
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 = []
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 Verbs.index_of(verb) < 0:
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")
let numbers = values.numbers(doc, recipe, "numbers", 0, 64)
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")))
## 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.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 group.kind != "Group": error(ui.invalid, "A node parent must be a Group")
parent = group.parent
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")
read_scene(doc, graph)
if graph.scope != -1 and graph.require(graph.scope).kind != "Group": 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)
graph.preview = values.flag(doc, "/scene", "preview")