dymokomi/luced-3d
Source of release 0.11.5
## Cooking a Code node (code_node.luc has the node's commands): its
## body and header run by luce-geocore's Code over the input, with the
## spare parameters' values and the timeline's clock. Kept apart from the
## commands so network.luc can cook without importing the workspace.
from luce_geocore.geocore import Code, GeometrySet
from geometry_data import GeometryData
pub let kind_name = "Code"
pub let body_slot = 1
pub let header_slot = 2
pub let spares_slot = 3
## Text 4: the ramps, a line each (name, channels, the data).
pub let ramps_slot = 4
pub let run_over_slot = 0
pub let count_slot = 1
## Voxels: whether every voxel of the grid's box runs (Houdini's dense
## VDB activation), not only the active ones.
pub let dense_slot = 2
## The Run Over code of a Volume Wrangle.
pub let voxels = 4 + 1
pub let first_spare_slot = 16
## Value slots a Code node has (spare parameters fill them past 16).
pub let value_count = 64
## Houdini's Run Over menu, in order (the codes luce-geocore's Code takes).
pub func run_over_names() -> str:
return "Detail (only once)|Points|Vertices|Primitives|Numbers|Voxels"
## What a new Code node says: a Point Wrangle's first line.
pub func starter() -> str:
return "# p is this point; k the node (parameters, frame, time).\np.P[1] += math32.sin(p.P[0] * 4.0) * 0.1\n"
## Text slots a Script node keeps its text and file parameters' values in.
pub let first_text_slot = 5
pub let text_slots = 8
## One spare parameter (Code and Script nodes): its name, first value slot,
## components (1–4) and whether it is whole; `kind` says how its row shows
## it ("" a number or vector, or toggle, color, menu, text, file), `choices`
## a menu's items. A text or file parameter's slot is a text slot and it has
## no components.
pub class Spare:
pub let name: str
pub let slot: int
pub let components: int
pub let integer: bool
pub let kind: str
pub let choices: str
pub func init(self, name: str, slot: int, components: int, integer: bool, kind: str = "", choices: str = ""):
self.name = name
self.slot = slot
self.components = components
self.integer = integer
self.kind = kind
self.choices = choices
## Whether its value is text (a text or file parameter).
pub func textual(self) -> bool:
return self.kind == "text" or self.kind == "file"
## The spares a node's text 3 lists (a line each: name, slot, components,
## whole, then kind and choices, tab apart).
pub func spares_of(text: str) -> list[Spare]:
let result: list[Spare] = []
for line in text.split("\n"):
let parts = line.split("\t")
if parts.length != 4 and parts.length != 6:
continue
let slot = int(parts[1]) catch failure:
recover -1
let components = int(parts[2]) catch failure:
recover 0
let kind = parts[4] if parts.length == 6 else ""
let spare = Spare(parts[0], slot, components, parts[3] == "1", kind, parts[5] if parts.length == 6 else "")
if spare.textual():
if slot >= first_text_slot and slot < first_text_slot + text_slots:
result.append(spare)
continue
if slot < first_spare_slot or components < 1 or components > 4 or slot + components > value_count:
continue
result.append(spare)
return result
## One ramp of the code (k.ramp / k.ramp_color): its name, channels (1
## a float ramp, 3 a color ramp) and data (luce-std ramp's flat form).
pub class Ramp:
pub let name: str
pub let channels: int
pub let data: list[float]
pub func init(self, name: str, channels: int, data: list[float]):
self.name = name
self.channels = channels
self.data = data
## The ramps a node's text 4 lists (a line each: name, channels, data
## comma apart, tab apart).
pub func ramps_of(text: str) -> list[Ramp]:
let result: list[Ramp] = []
for line in text.split("\n"):
let parts = line.split("\t")
if parts.length != 3:
continue
let channels = int(parts[1]) catch failure:
recover 0
let data: list[float] = []
for piece in parts[2].split(","):
let value = float(piece) catch failure:
recover 0.0
data.append(value)
if channels == 1 or channels == 3:
result.append(Ramp(parts[0], channels, data))
return result
pub func ramps_text(ramps: list[Ramp]) -> str:
let lines: list[str] = []
for r in ramps:
let values: list[str] = []
for v in r.data:
values.append(f"{v}")
lines.append(f"{r.name}\t{r.channels}\t{values.join(",")}")
return lines.join("\n")
## A new ramp's data: 0 to 1 for a float ramp, black to white for color.
pub func default_ramp(channels: int) -> list[float]:
if channels == 3:
return [1.0, 3.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
return [1.0, 1.0, 2.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0]
pub func spares_text(spares: list[Spare]) -> str:
let lines: list[str] = []
for s in spares:
let line = f"{s.name}\t{s.slot}\t{s.components}\t{"1" if s.integer else "0"}"
lines.append(line if s.kind == "" else f"{line}\t{s.kind}\t{s.choices}")
return lines.join("\n")
## Cook: the code over the input, with the parameters' values now and the
## timeline's frame, time and rate (`clock`); `second` is what k.input(1)
## reads (empty when unconnected).
pub func cook(slots: list[float], texts: list[str], data: GeometryData, clock: list[float], second: GeometryData? = none) -> GeometryData!:
let names: list[str] = []
let given: list[float] = []
for s in spares_of(texts[spares_slot]):
if s.components == 1:
names.append(s.name)
given.append(slots[s.slot])
continue
for c in 0..<s.components:
names.append(f"{s.name}[{c}]")
given.append(slots[s.slot + c])
# A ramp goes as "<name>#ramp" holding its length, then its data.
for r in ramps_of(texts[ramps_slot]):
names.append(r.name + "#ramp")
given.append(float(r.data.length))
for v in r.data:
names.append("")
given.append(v)
let frame = clock[0] if clock.length == 3 else 1.0
let time = clock[1] if clock.length == 3 else 0.0
let fps = clock[2] if clock.length == 3 else 24.0
var other: GeometrySet? = none
if let given_second = second:
other = given_second.geometry
# Voxels: text 0 names the grid to run over (Houdini's Volume Wrangle
# picks it by group); the other modes take it as their group.
let run_over = int(slots[run_over_slot])
let on_voxels = run_over == voxels
let run = Code.cook(data.geometry, run_over, texts[header_slot], texts[body_slot], "" if on_voxels else texts[0], int(slots[15]),
names, given, frame, time, fps, int(slots[count_slot]), other, texts[0].trim() if on_voxels else "", slots[dense_slot] != 0.0)
let result = GeometryData(geometry = run.geometry())
result.console = run.output()
# k.warning: the first, and how many more.
if run.warning_count() > 0:
result.warning = run.warning() + (f" (and {run.warning_count() - 1} more)" if run.warning_count() > 1 else "")
return result