dymokomi/luced-3d
Source of release 0.11.3
0.11.3 / tests / editor / code_node_tests.luc
## The Code node (Houdini's Wrangle in Luce Base): its starter bends a
## grid; a literal k.f32 parameter becomes a row whose value the cook uses
## and that keeps its value through code edits; an error fails the node
## with its line (and the editor's mark parses it); code reading the time
## follows the timeline; primitives get colors; Detail writes a detail
## attribute; the code survives a project round trip, many lines; edits
## are undo steps.
from workspace import Workspace
import node_commands
import code_node
import code_cook
import project_file
import timeline_commands
from luce_std import files, paths, math
pub func code_node_tests() -> unit!:
let w = Workspace()
node_commands.delete_selected(w)
node_commands.add_node(w, "Grid", 0.0, 0.0)
let flat = (w.geometry else trap("grid")).bounds()
assert(flat.maximum.y - flat.minimum.y < 1e-6)
node_commands.add_node(w, "Code", 0.0, 100.0)
let id = w.network.selected
node_commands.display_selected(w)
let code = w.network.require(id)
assert(code.failure == "")
# The starter: P.y follows sin(P.x · 4) · 0.1.
let bent = (w.geometry else trap("bent")).bounds()
assert(bent.maximum.y > 0.05 and bent.minimum.y < -0.05)
# A parameter row, its value used, kept through an edit.
code_node.set_code(w, id, code_cook.body_slot, "p.P[1] = k.f32(\"height\", 0.5)\n")
let spares = code_cook.spares_of(code.texts[code_cook.spares_slot])
assert(spares.length == 1 and spares[0].name == "height" and code.values[spares[0].slot] == 0.5)
assert(math.abs((w.geometry else trap("lifted")).bounds().maximum.y - 0.5) < 1e-6)
w.network.selected = id
node_commands.set_parameter(w, spares[0].slot, 2.0)
assert(math.abs((w.geometry else trap("higher")).bounds().maximum.y - 2.0) < 1e-6)
code_node.set_code(w, id, code_cook.body_slot, "p.P[1] = k.f32(\"height\", 0.5) * 2.0\n")
assert(math.abs((w.geometry else trap("doubled")).bounds().maximum.y - 4.0) < 1e-6)
# A vector parameter reaches the code whole.
code_node.set_code(w, id, code_cook.body_slot, "p.P = p.P + k.vec3(\"at\", [1.0, 2.0, 3.0, 0.0])\n")
let at = (w.geometry else trap("at")).bounds()
assert(math.abs(at.maximum.y - 2.0) < 1e-6 and math.abs(at.minimum.y - 2.0) < 1e-6)
# Vector rows have the parameter's own components.
code_node.set_code(w, id, code_cook.body_slot, "let a = k.vec2(\"flat\", [1.0, 2.0])\nlet b = k.vec4(\"wide\", [1.0, 2.0, 3.0, 4.0])\np.P[1] = a[0] + b[3]\n")
let rows = code_node.spare_rows(w.network.require(id))
assert(rows.length == 2 and rows[0].components == 2 and rows[1].components == 4)
assert(math.abs((w.geometry else trap("rows")).bounds().maximum.y - 5.0) < 1e-6)
# nearpoints averaging keeps a clean sphere round, a little smaller.
let sw = Workspace()
node_commands.delete_selected(sw)
node_commands.add_node(sw, "Sphere", 0.0, 0.0)
node_commands.set_parameter(sw, 1, 32.0)
node_commands.add_node(sw, "Code", 0.0, 100.0)
let smooth_id = sw.network.selected
code_node.set_code(sw, smooth_id, code_cook.body_slot, "let g = k.input(0)\nlet near = g.nearpoints(p.P, 0.3, 8)\nvar sum: f32[4] = [0.0, 0.0, 0.0, 0.0]\nfor i in 0..<near.count:\n sum = sum + g.point_P(near.points[i])\np.P = sum * (1.0 / (f32)near.count)\n")
node_commands.display_selected(sw)
let round = (sw.geometry else trap("smoothed")).bounds()
# (The sphere sits on the ground: y 0 to 2, x -1 to 1.) The edit stays:
# its collapsed pole faces draw no guide and roll nothing back.
assert(sw.network.require(smooth_id).texts[code_cook.body_slot].starts_with("let g = k.input(0)"))
assert(round.maximum.x > 0.9 and round.maximum.x < 1.0 and round.maximum.y > 1.9 and round.maximum.y < 2.0 and round.minimum.y > 0.0 and round.minimum.y < 0.1)
# Second-input reads match geometry: a sphere of radius 1.5 sitting on
# the ground (center y 1.5), 64 segments; from the point (5, 0, 0) its
# surface is sqrt(25 + 1.5²) - 1.5 away; a ray up from (0, -1, 0) meets
# it at y 0.
let dw = Workspace()
node_commands.delete_selected(dw)
node_commands.add_node(dw, "Grid", 0.0, 0.0)
let dgrid = dw.network.selected
node_commands.add_node(dw, "Sphere", 200.0, 0.0)
node_commands.set_parameter(dw, 0, 1.5)
node_commands.set_parameter(dw, 1, 64.0)
let dball = dw.network.selected
dw.network.selected = dgrid
node_commands.add_node(dw, "Code", 0.0, 100.0)
let did = dw.network.selected
dw.network.connect(dball, did, 1)
code_node.set_code(dw, did, code_cook.body_slot, "let g = k.input(1)\nk.set_point_f32(p.ptnum, \"far\", g.xyzdist([5.0, 0.0, 0.0, 0.0]))\nlet hit = g.intersect([0.0, -1.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0])\nk.set_point_f32(p.ptnum, \"hit\", hit.position[1])\nk.set_point_i32(p.ptnum, \"prim\", hit.prim)\nk.set_point_f32(p.ptnum, \"top\", g.bounds().max[1])\n")
node_commands.display_selected(dw)
let dmesh = dw.geometry else trap("distances")
assert(math.abs(dmesh.attribute_value(dmesh.find_attribute("far", 0), 0, 0) - (math.sqrt(25.0 + 2.25) - 1.5)) < 0.01)
assert(math.abs(dmesh.attribute_value(dmesh.find_attribute("hit", 0), 0, 0)) < 0.01 and math.abs(dmesh.attribute_value(dmesh.find_attribute("top", 0), 0, 0) - 3.0) < 1e-5)
# A ramp: a row with the default data; the cook reads it; new data
# changes the result.
code_node.set_code(w, id, code_cook.body_slot, "p.P[1] = k.ramp(\"shape\", 0.25)\n")
let ramps = code_cook.ramps_of(w.network.require(id).texts[code_cook.ramps_slot])
assert(ramps.length == 1 and ramps[0].name == "shape" and ramps[0].channels == 1)
assert(math.abs((w.geometry else trap("ramp")).bounds().maximum.y - 0.25) < 1e-5)
let ramp_rows = code_node.spare_rows(w.network.require(id))
assert(ramp_rows.length == 1 and ramp_rows[0].kind == "ramp")
code_node.set_ramp(w, id, 0, [1.0, 1.0, 2.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0])
assert(math.abs((w.geometry else trap("ramp down")).bounds().maximum.y - 0.75) < 1e-5)
code_node.set_code(w, id, code_cook.body_slot, "p.P[1] = k.ramp(\"shape\", 0.25) * 2.0\np.Cd = k.ramp_color(\"tint\", 1.0)\n")
let kept = code_cook.ramps_of(w.network.require(id).texts[code_cook.ramps_slot])
assert(kept.length == 2 and kept[0].data[4] == 1.0 and kept[1].channels == 3)
# printf output reaches the node's console; a warning the node's warning.
code_node.set_code(w, id, code_cook.body_slot, "if p.ptnum < 2:\n k.printf(\"point %d at %.2f\\n\", (f32)p.ptnum, p.P[0])\nk.warning(\"just checking\")\n")
let said = w.network.require(id)
assert(said.failure == "" and said.console.starts_with("point 0 at ") and said.console.contains("point 1 at "))
assert(said.warning.starts_with("just checking"))
# k.error fails the node at its line, naming the element.
code_node.set_code(w, id, code_cook.body_slot, "if p.ptnum == 3:\n k.error(\"bad point\")\n")
assert(w.network.require(id).failure.contains("line 2, column ") and w.network.require(id).failure.contains("bad point (point 3)"))
# Names written by code drive a Blast's group: @name=quad_2 deletes a
# quarter of the default grid's 16 faces; an object path still filters.
let nw = Workspace()
node_commands.delete_selected(nw)
node_commands.add_node(nw, "Grid", 0.0, 0.0)
node_commands.add_node(nw, "Code", 0.0, 100.0)
let namer = nw.network.selected
node_commands.set_parameter(nw, code_cook.run_over_slot, 3.0)
code_node.set_code(nw, namer, code_cook.body_slot, "let c = f.center()\nlet quadrant = (0 if c[0] < 0.0 else 1) + (0 if c[2] < 0.0 else 2)\nf.set_text(\"name\", k.sprintf(\"quad_%d\", (f32)quadrant))\n")
node_commands.display_selected(nw)
assert(nw.network.require(namer).failure == "" and (nw.geometry else trap("named")).face_count() == 16)
node_commands.add_node(nw, "Blast", 0.0, 200.0)
node_commands.set_text(nw, 0, "@name=quad_2")
node_commands.display_selected(nw)
assert((nw.geometry else trap("blasted")).face_count() == 12)
node_commands.set_parameter(nw, 0, 1.0)
assert((nw.geometry else trap("isolated")).face_count() == 4)
# P and Cd written by one snippet both land.
code_node.set_code(w, id, code_cook.body_slot, "p.P = p.P + [0.0, 1.0, 0.0, 0.0]\np.Cd = [1.0, 0.0, 0.0, 0.0]\n")
let both = (w.geometry else trap("both")).bounds()
assert(math.abs(both.minimum.y - 1.0) < 1e-6 and math.abs(both.maximum.y - 1.0) < 1e-6)
# An error fails the node at its line; the editor's mark reads it.
code_node.set_code(w, id, code_cook.body_slot, "let a = 1.0\np.P[1] = b\n")
assert(code.failure.contains("line 2, column "))
let place = code_node.failure_place(code.failure)
assert(place.length == 3 and place[0] == 0 and place[1] == 2)
# A fault names the element.
code_node.set_code(w, id, code_cook.body_slot, "let i = p.ptnum\nassert(i < 3)\n")
assert(code.failure.contains("line 2, column ") and code.failure.ends_with("(point 3)"))
# Time: the node follows the timeline.
code_node.set_code(w, id, code_cook.body_slot, "p.P[1] = (f32)k.frame\n")
assert(code.time_dependent and code.failure == "")
timeline_commands.set_frame(w, 5.0)
assert(math.abs((w.geometry else trap("frame 5")).bounds().maximum.y - 5.0) < 1e-6)
timeline_commands.set_frame(w, 9.0)
assert(math.abs((w.geometry else trap("frame 9")).bounds().maximum.y - 9.0) < 1e-6)
code_node.set_code(w, id, code_cook.body_slot, "p.P[1] = 1.0\n")
assert(not code.time_dependent)
# Primitives: a color each.
w.network.selected = id
node_commands.set_parameter(w, code_cook.run_over_slot, 3.0)
code_node.set_code(w, id, code_cook.body_slot, "f.Cd = [0.0, 1.0, 0.0, 0.0] if f.primnum % 2 == 0 else [1.0, 0.0, 0.0, 0.0]\n")
assert(code.failure == "")
# Detail: once, writing a detail attribute.
node_commands.set_parameter(w, code_cook.run_over_slot, 0.0)
code_node.set_code(w, id, code_cook.body_slot, "k.set_detail_f32(\"answer\", 42.0)\n")
assert(code.failure == "")
# A header helper used by the body.
node_commands.set_parameter(w, code_cook.run_over_slot, 1.0)
code_node.set_code(w, id, code_cook.header_slot, "func lift(y: f32) -> f32:\n return y + 3.0\n")
code_node.set_code(w, id, code_cook.body_slot, "p.P[1] = lift(p.P[1])\n")
assert(code.failure == "" and math.abs((w.geometry else trap("lifted 3")).bounds().maximum.y - 3.0) < 1e-6)
# Undo takes back the last edit of the code.
w.undo()
assert(w.network.require(id).texts[code_cook.body_slot].starts_with("k.set_detail_f32"))
w.redo()
# Saved and read back, many lines and all.
let folder = files.create_temporary_directory(".")
let path = paths.join(folder, "code.prisma")
project_file.save(path, w, [1.0, 0.0, 0.0])
let back = project_file.load(path).graph.require(id)
assert(back.texts[code_cook.body_slot] == "p.P[1] = lift(p.P[1])\n" and back.texts[code_cook.header_slot].starts_with("func lift"))
files.remove_tree(folder)
print("PASS code node: starter, parameters, errors and faults at their lines, time, primitives, detail, header, undo, project")