dymokomi/luced-3d
Source of release 0.11.9
0.11.9 / src / project_file.luc
## Atomic .prisma project files. Relative geometry references resolve at the file.
import edit_held
from luce_prism.prism import Document, Value, Encoding, write_durable
from network import Network
from workspace import Workspace
from project_graph import write_graph, read_graph
import project_values as values
from luce_std import files, paths
from luce_ui import ui
import platform
from view_panes import panes_size, valid_panes
from luce_geocore.geocore import GeometryFile
from geometry_data import GeometryData
from held import held_as
pub class Project:
pub let graph: Network
## The viewport's layout and its panes' views (ViewPanes.state()).
pub let panes: list[float]
pub let graph_view: list[float]
pub func init(self, graph: Network, panes: list[float], graph_view: list[float]):
self.graph = graph
self.panes = panes
self.graph_view = graph_view
## Absolute lexical filename; the containing directory must exist, not the file.
pub func filename(path: str) -> str!:
if path.trim() == "": error(ui.invalid, "Choose a project filename")
return paths.join(files.canonical(paths.directory(path)), paths.base(path))
## Prefer a relative file path when both files share a filesystem root.
pub func relative(source: str, directory: str) -> str!:
let normalized = paths.normalize(source)
if paths.root(normalized) != paths.root(directory): return normalized
# normalize uses forward separators after the root, even on Windows;
# retain literal backslashes in POSIX filenames.
let a = (normalized.replace("\\", "/") if platform.windows else normalized).split("/")
let base = paths.normalize(directory)
let b = (base.replace("\\", "/") if platform.windows else base).split("/")
var common = 0
while common < a.length and common < b.length and a[common] == b[common]: common += 1
let parts: list[str] = []
for at in common..<b.length:
if b[at] != "": parts.append("..")
for at in common..<a.length: parts.append(a[at])
return parts.join("/")
pub func save(path: str, workspace: Workspace, graph_view: list[float]) -> unit!:
let destination = filename(path)
let graph = workspace.network.copy()
for node in graph.nodes:
if node.kind != "File" or node.texts[0] == "": continue
let absolute = paths.normalize(paths.join(files.canonical("."), node.texts[0]))
node.texts[0] = relative(absolute, paths.directory(destination))
# Frozen results live beside the project (<name>.frozen/), by relative
# path; each is copied there once.
for node in graph.nodes:
if node.frozen == "": continue
let folder = destination + ".frozen"
files.ensure_directory(folder)
let kept = paths.join(folder, paths.base(node.frozen)) if paths.directory(node.frozen) != folder else node.frozen
if kept != node.frozen and not files.exists(kept):
files.copy(node.frozen, kept, replace = true)
node.frozen = relative(kept, paths.directory(destination))
# Edit nodes that hold their result keep it beside the project
# (<name>.held/n<id>.prism, a luce-geocore crate), its last action
# baked in: a project holds results, not actions. A file already
# holding the result is not written again.
# Actions still running on the worker are run here: the file holds
# their results.
edit_held.finish(graph)
for node in graph.nodes:
let held = node.held else continue
let folder = destination + ".held"
files.ensure_directory(folder)
let file = paths.join(folder, f"n{node.id}.prism")
if GeometryFile.key(file) != held.key:
_ = GeometryFile.save(held.result.geometry, file, held.key)
node.held_file = relative(file, paths.directory(destination))
node.held_input = held.input_stamp
node.held_actions = held.actions + (1 if held.made_for != 0 else 0)
node.held_aligns = held.result.aligns.join("\n")
node.edits = []
let doc = Document()
write_graph(doc, graph)
doc.add("/scene/view", "editor_view")
let w = workspace
# The viewport's layout and every pane's view (view_panes.luc).
doc.set("/scene/view", "panes", Value.numbers([panes_size], w.panes.state()))
doc.set("/scene/view", "network", Value.numbers([3], graph_view))
# Never produce a project our bounded loader would refuse to open. Encode
# once; Prism's durable writer still uses synchronized atomic replacement.
let encoded = doc.encode(Encoding.text)
if encoded.size() > 67108864: error(ui.invalid, "Project exceeds the 64 MiB file limit")
write_durable(destination, encoded.bytes())
pub func load(path: str) -> Project!:
let source = filename(path)
let doc = Document.parse(files.read_text(source, limit = 67108864))
let graph = read_graph(doc)
let panes = values.numbers(doc, "/scene/view", "panes", panes_size, panes_size)
let graph_view = values.numbers(doc, "/scene/view", "network", 3, 3)
if not valid_panes(panes) or graph_view[0] < 0.25 or graph_view[0] > 2.5:
error(ui.invalid, "Invalid project views or network zoom")
for node in graph.nodes:
if node.kind == "File" and node.texts[0] != "":
node.texts[0] = values.line(paths.normalize(paths.join(paths.directory(source), node.texts[0])))
if node.frozen != "":
node.frozen = paths.normalize(paths.join(paths.directory(source), node.frozen))
# A held result comes back as the node's result, nothing to adjust.
if node.held_file != "":
let file = paths.normalize(paths.join(paths.directory(source), node.held_file))
let key = GeometryFile.key(file)
if key == 0:
error(ui.invalid, f"{node.name}'s held result is missing ({node.held_file})")
let data = GeometryData(geometry = GeometryFile.load(file))
if node.held_aligns != "":
data.aligns = node.held_aligns.split("\n")
node.held = held_as(data, key, node.held_input, node.held_actions)
node.held_key = key
node.held_file = ""
node.edits = []
return Project(graph, panes, graph_view)
## Authored edits only: selection, cooked caches and background progress aren't dirty.
pub func same_graph(a: Network, b: Network) -> bool:
if a.nodes.length != b.nodes.length: return false
for at in 0..<a.nodes.length:
let left = a.nodes[at]
let right = b.nodes[at]
if left.id != right.id or left.kind != right.kind or left.name != right.name or left.parent != right.parent or left.enabled != right.enabled or left.visible != right.visible or left.x != right.x or left.y != right.y or left.inputs != right.inputs or left.values != right.values or left.texts != right.texts or left.guard != right.guard or left.hidden != right.hidden or left.frozen != right.frozen or left.time_dependent != right.time_dependent or left.held_key != right.held_key or left.edits.length != right.edits.length:
return false
for index in 0..<left.edits.length:
# A step's key covers its verb, group, numbers and guard.
if left.edits[index].key != right.edits[index].key:
return false
# The active take's state is the nodes', compared above.
if a.takes.length != b.takes.length or a.take != b.take: return false
for at in 0..<a.takes.length:
if a.takes[at].name != b.takes[at].name or (at != a.take and not a.takes[at].same(b.takes[at])): return false
return true