dymokomi/luced-3d
Source of release 0.11.6
0.11.6 / tests / editor / code_worker_tests.luc
## The Code node on the compute worker (how the app cooks): the code, its
## parameters and the timeline's clock reach the worker; a frame change
## recooks the node that reads the time; an error comes back with its line;
## freezing keeps the frame's result while the timeline moves.
from workspace import Workspace
import time
import node_commands
import code_node
import code_cook
import timeline_commands
from luce_std import math
func settle(w: Workspace) -> unit!:
let session = w.compute else trap("compute")
let deadline = time.now() + 20000000000
while session.busy and time.now() < deadline:
_ = w.poll_compute()
assert(not session.busy)
func top(w: Workspace) -> float!:
return (w.geometry else trap("geometry")).bounds().maximum.y
pub func code_worker_tests() -> unit!:
let w = Workspace()
w.enable_compute()
settle(w)
node_commands.delete_selected(w)
node_commands.add_node(w, "Grid", 0.0, 0.0)
node_commands.add_node(w, "Code", 0.0, 100.0)
let id = w.network.selected
node_commands.display_selected(w)
code_node.set_code(w, id, code_cook.body_slot, "p.P[1] = k.f32(\"base\", 0.5) + (f32)k.frame * 0.1\n")
settle(w)
assert(w.network.require(id).failure == "" and math.abs(top(w) - 0.6) < 1e-5)
# A parameter changed on the UI side reaches the worker.
let slot = code_cook.spares_of(w.network.require(id).texts[code_cook.spares_slot])[0].slot
w.network.selected = id
node_commands.set_parameter(w, slot, 2.0)
settle(w)
assert(math.abs(top(w) - 2.1) < 1e-5)
# The frame reaches it; the node recooks on a new frame.
timeline_commands.set_frame(w, 11.0)
settle(w)
assert(math.abs(top(w) - 3.1) < 1e-5)
# An error comes back with its line.
code_node.set_code(w, id, code_cook.body_slot, "p.P[1] = nope\n")
settle(w)
assert(w.network.require(id).failure.contains("line 1, column "))
w.undo()
settle(w)
assert(w.network.require(id).failure == "" and math.abs(top(w) - 3.1) < 1e-5)
# printf output and warnings come back from the worker.
code_node.set_code(w, id, code_cook.body_slot, "if p.ptnum == 0:\n k.printf(\"frame %d\\n\", (f32)k.frame)\n k.warning(\"first point\")\np.P[1] = k.f32(\"base\", 0.5) + (f32)k.frame * 0.1\n")
settle(w)
assert(w.network.require(id).console == "frame 11\n" and w.network.require(id).warning.starts_with("first point"))
# Frozen at frame 11, it no longer follows the timeline.
w.network.selected = id
node_commands.toggle_freeze(w)
settle(w)
timeline_commands.set_frame(w, 31.0)
settle(w)
assert(math.abs(top(w) - 3.1) < 1e-5)
node_commands.toggle_freeze(w)
settle(w)
assert(math.abs(top(w) - 5.1) < 1e-5)
(w.compute else trap("compute")).close()
print("PASS code on the worker: parameters, clock, frames, errors, undo, freeze")