dymokomi/luced-3d
Source of release 0.11.1
import probe
import time
from luce_ui.ui import Event
from luce_window.input import EventKind
from app import Luce3D
from luce_3d.graphics import Vector3, Matrix4, Group, AmbientLight, DirectionalLight
from geometry_data import GeometryData
from overlay_lines import OverlayLines
from scene_projection import append_scene, append_overlays
import modeling_commands
import node_commands
import selection_commands
pub func main(arguments: list[str]) -> int!:
let editor = Luce3D(width = int(arguments[15]) if arguments.length > 16 else 2200, height = int(arguments[16]) if arguments.length > 16 else 1400)
if arguments.length > 17 and arguments[17] == "viewport":
for id in ["parameters", "outliner", "network", "geometry_spreadsheet"]:
let panel = editor.panels.dock.find(id) else trap("missing capture panel")
editor.panels.dock.remove(panel)
if arguments.length > 5 and arguments[5] == "background":
editor.workspace.enable_compute()
if arguments.length > 4:
editor.workspace.set_display_mode(int(probe.parse_value(arguments[4])))
if arguments.length > 2 and arguments[1] in ["cad", "cad_wire", "analytic"]:
let w = editor.workspace
node_commands.delete_selected(w)
node_commands.add_node(w, "File", 40.0, 40.0)
node_commands.set_text(w, 0, arguments[2])
if arguments.length > 21 and probe.parse_value(arguments[21]) != 0.0:
node_commands.set_parameter(w, 0, probe.parse_value(arguments[21]))
if arguments.length > 6 and arguments[6] != "":
node_commands.add_node(w, "Blast", 220.0, 100.0)
node_commands.set_text(w, 0, arguments[6])
node_commands.set_parameter(w, 0, 1.0)
if arguments[1] != "analytic":
node_commands.add_node(w, "Tessellate", 40.0, 160.0)
if arguments.length > 3:
node_commands.set_parameter(w, 1, probe.parse_value(arguments[3]))
node_commands.add_node(w, "Transform", 40.0, 280.0)
node_commands.set_parameter(w, 4, 0.0 if arguments[2].lower().ends_with(".obj") else -90.0)
if arguments.length > 11 and arguments[11] != "": node_commands.set_parameter(w, 4, probe.parse_value(arguments[11]))
if arguments[1] == "cad_wire":
w.set_display_mode(4)
w.frame_cube()
w.view.distance *= 0.75
w.view.update_camera()
if arguments[1] == "analytic":
node_commands.select_node(w, 2)
print(f"analytic preview: {w.notice}")
editor.panels.refresh()
if arguments.length > 1 and arguments[1] == "outliner":
let w = editor.workspace
node_commands.set_output(w, 1, -1)
node_commands.add_node(w, "Group", 220.0, 70.0)
let vehicle = w.network.selected
node_commands.rename(w, "Assembly")
node_commands.enter_group(w, vehicle)
node_commands.add_node(w, "Cube", 40.0, 60.0)
node_commands.rename(w, "Body")
node_commands.add_node(w, "Group", 240.0, 60.0)
let detail = w.network.selected
node_commands.rename(w, "Details")
node_commands.set_parameter(w, 1, 3.0)
node_commands.enter_group(w, detail)
node_commands.add_node(w, "Sphere", 40.0, 60.0)
node_commands.rename(w, "Sphere")
node_commands.leave_group(w)
node_commands.leave_group(w)
w.network.selected = -1
node_commands.add_node(w, "Torus", 430.0, 70.0)
node_commands.add_node(w, "Transform", 430.0, 180.0)
node_commands.set_parameter(w, 1, -3.0)
node_commands.rename(w, "Independent island")
node_commands.select_node(w, vehicle)
editor.panels.refresh()
if arguments.length > 1 and arguments[1] == "occlusion":
let w = editor.workspace
node_commands.add_node(w, "PolyBevel", 40.0, 170.0)
node_commands.add_node(w, "Edit", 40.0, 270.0)
selection_commands.choose_component(w, 2, false)
modeling_commands.model(w, "Delete")
w.frame_cube()
editor.panels.refresh()
if arguments.length > 1 and arguments[1] == "edit":
let w = editor.workspace
node_commands.add_node(w, "Transform", 40.0, 200.0)
node_commands.set_parameter(w, 5, 18.0)
node_commands.add_node(w, "Edit", 40.0, 330.0)
selection_commands.choose_component(w, 2, false)
modeling_commands.extrude(w)
selection_commands.choose_component(w, 0, false)
modeling_commands.extrude(w)
selection_commands.choose_component(w, 2, false)
modeling_commands.choose_tool(w, "Move")
selection_commands.choose_component(w, 2, false)
w.frame_cube()
editor.panels.refresh()
if arguments.length > 1 and arguments[1] in ["modeling", "spreadsheet"]:
let w = editor.workspace
node_commands.add_node(w, "AttributeRandomize", 40.0, 160.0)
node_commands.add_node(w, "PolyBevel", 40.0, 250.0)
node_commands.set_parameter(w, 0, 0.18)
node_commands.add_node(w, "Edit", 40.0, 340.0)
selection_commands.choose_component(w, 2, false)
node_commands.set_parameter(w, 0, 0.22)
modeling_commands.model(w, "Inset")
selection_commands.choose_component(w, 2, false)
node_commands.set_parameter(w, 0, 0.35)
modeling_commands.extrude(w)
modeling_commands.choose_tool(w, "Move")
selection_commands.choose_component(w, 2, false)
w.frame_cube()
editor.panels.refresh()
if arguments[1] == "spreadsheet":
let panel = editor.panels.dock.find("geometry_spreadsheet") else trap("sheet")
editor.panels.dock.select(panel)
# The parameter pane's states: a disabled row and a tooltip, the value
# ladder, an open menu.
if arguments.length > 1 and arguments[1].starts_with("param_"):
let w = editor.workspace
let kind = "CopyTransform" if arguments[1] == "param_copy" else ("Mirror" if arguments[1] == "param_menu" else "Transform")
node_commands.add_node(w, kind, 40.0, 200.0)
if kind == "CopyTransform":
node_commands.set_parameter(w, 0, 1.0)
editor.panels.refresh()
var frames = 0
var computing_frames = 0
let started = time.now()
var phase = ""
let orbit = arguments.length > 7 and arguments[7] == "orbit"
let final_frame = 336 if orbit else 5
var frame_total = 0.0
var frame_worst = 0.0
# A scheduled input source keeps the loop awake. Invalidating only inside
# on_frame measures the idle wait policy as well as rendering.
var orbit_timer = -1
if orbit:
orbit_timer = editor.app.every(0.001, func () -> unit!:
if frames > 0:
editor.workspace.view.orbit(0.5, 0.0)
editor.panels.view.layout().invalidate())
if arguments.length > 1 and arguments[1].starts_with("gizmo_"):
let w = editor.workspace
node_commands.add_node(w, "Transform", 40.0, 220.0)
editor.panels.view.tools.gizmos.mode = "Rotate" if arguments[1] == "gizmo_rotate" else ("Scale" if arguments[1] == "gizmo_scale" else ("Pivot" if arguments[1] == "gizmo_pivot" else "Move"))
editor.panels.refresh()
let observed = editor.app.on_frame(func (elapsed: float) -> unit!:
if let compute = editor.workspace.compute:
if compute.busy:
let current = compute.service.phase()
if current != phase:
phase = current
print(f"compute {float(time.now() - started) / 1000000000.0}s: {phase}")
computing_frames += 1
editor.panels.view.layout().invalidate()
return
if frames == 0:
if editor.workspace.notice != "": trap(editor.workspace.notice)
print(f"Background compute: {computing_frames} live frames in {float(time.now() - started) / 1000000000.0}s; {editor.workspace.notice}")
editor.workspace.frame_cube()
if frames == 0 and arguments.length > 10 and arguments[1] in ["cad", "cad_wire", "analytic"]:
if arguments[1] != "analytic":
let visible = editor.workspace.geometry else trap("capture has no polygon geometry; check the group expression")
assert(visible.face_count() > 0)
editor.workspace.frame_cube()
if arguments.length > 18 and int(probe.parse_value(arguments[18])) >= 0:
let w = editor.workspace
let whole = w.geometry else trap("patch capture requires polygons")
let owner = whole.find_attribute("cad_face", 2)
if owner < 0: trap("patch capture requires original CAD face ownership")
let patch = int(probe.parse_value(arguments[18]))
let neighbors = arguments.length > 20 and arguments[20] == "neighbors"
let kept: list[bool] = []
if neighbors:
let seam: list[bool] = []
for edge in 0..<whole.edge_count(): seam.append(false)
for face in 0..<whole.face_count():
let parent = int(whole.attribute_value(owner,face,0))
while kept.length <= parent: kept.append(false)
if parent != patch: continue
for corner in 0..<whole.face_size(face): seam[whole.face_edge(face,corner)] = true
for face in 0..<whole.face_count():
for corner in 0..<whole.face_size(face):
if seam[whole.face_edge(face,corner)]:
kept[int(whole.attribute_value(owner,face,0))] = true
break
let remove: list[int] = []
for face in 0..<whole.face_count():
let parent = int(whole.attribute_value(owner,face,0))
if not (kept[parent] if neighbors else parent == patch): remove.append(face)
var isolated = whole.without_faces(remove)
if arguments.length > 19 and arguments[19] == "neutral":
for domain in 0..<4:
if isolated.find_attribute("Cd", domain) >= 0: isolated = isolated.without_attribute("Cd", domain)
if isolated.face_count() == 0: trap("requested CAD patch has no polygons")
w.geometry = isolated
w.scene.clear()
# Inspection lighting must reveal underside patches too. The
# mesh, normals, depth test and actual wire edges are unchanged.
w.scene.add(AmbientLight(intensity = 0.6))
w.scene.add(DirectionalLight(direction = Vector3(3.0, 5.0, 2.0), intensity = 0.45))
w.scene.add(DirectionalLight(direction = Vector3(-3.0, -5.0, -2.0), intensity = 0.45))
let branch = Group()
let data = GeometryData(isolated)
_ = append_scene(data, branch, mode = w.view.display_mode)
w.scene.add(branch)
let wires = OverlayLines.make()
let normals = OverlayLines.make()
append_overlays(data, Matrix4(), wires, normals)
w.batches.wire_batch = wires.batch()
w.batches.normal_batch = normals.batch()
w.batches.patch_batch = none
w.view.grid_visible = false
let bounds = isolated.bounds()
w.view.target = bounds.minimum.add(bounds.maximum).multiply_scalar(0.5)
w.view.distance = bounds.maximum.subtract(bounds.minimum).length() * 1.8
print(f"Isolated assembled patch {patch}, neighbors={neighbors}: {isolated.face_count()} polygons, {isolated.edge_count()} real edges")
editor.workspace.view.distance *= probe.parse_value(arguments[8])
editor.workspace.view.yaw = probe.parse_value(arguments[9])
editor.workspace.view.pitch = probe.parse_value(arguments[10])
if arguments.length > 14 and arguments[12] != "":
editor.workspace.view.target = Vector3(probe.parse_value(arguments[12]), probe.parse_value(arguments[13]), probe.parse_value(arguments[14]))
editor.workspace.view.update_camera()
frames += 1
# Capture changes the drawable configuration, so exclude its next
# interval from the 300 measured callbacks.
if orbit and frames > 35 and frames <= 335:
frame_total += elapsed * 1000.0
if elapsed * 1000.0 > frame_worst: frame_worst = elapsed * 1000.0
if frames == 2 and arguments.length > 1 and arguments[1].starts_with("param_"):
probe_parameters(editor, arguments[1])
if frames == 2:
editor.panels.graph.frame_all()
if arguments.length > 1 and arguments[1] == "shading":
if let show = editor.panels.view.on_shading:
show(false)
if arguments.length > 1 and arguments[1] == "menu":
editor.panels.graph.pointer_x = 160.0
editor.panels.graph.pointer_y = 80.0
editor.panels.open_search()
if frames == final_frame - 1:
if arguments.length > 1 and arguments[1] == "menu":
editor.panels.open_search()
probe.begin("Luced 3D")
editor.panels.view.layout().invalidate()
elif frames >= final_frame:
if editor.workspace.notice != "": trap(editor.workspace.notice)
if orbit: print(f"Native orbit: {frame_total / 300.0} ms mean, {frame_worst} ms worst; callbacks, not input-to-photon")
editor.app.stop())
editor.app.run()
observed.disconnect()
if orbit: editor.app.cancel(orbit_timer)
probe.save(arguments[0])
probe.end()
editor.close()
return 0
## Drive the parameter pane as a user would for a capture.
func probe_parameters(editor: Luce3D, scene: str) -> unit!:
let panel = editor.panels.inspector.parameters
let origin = panel.layout().bounds()
let first = panel.row_bounds(0)
if scene == "param_copy":
panel.set_tip_delay(0.0)
editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = origin.x + 30.0, y = origin.y + first.y + 8.0))
elif scene == "param_ladder":
let field = panel.field_bounds(0, 0)
let x = origin.x + field.x + 20.0
let y = origin.y + field.y + 8.0
editor.app.dispatch(Event(kind = EventKind.pointer_down, x = x, y = y, button = 2))
editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = x + 40.0, y = y + 24.0, button = 2))
elif scene == "param_menu":
let axis = panel.row_bounds(0)
editor.app.dispatch(Event(kind = EventKind.pointer_down, x = origin.x + panel.label_width() + 40.0, y = origin.y + axis.y + 8.0))
editor.app.dispatch(Event(kind = EventKind.pointer_up, x = origin.x + panel.label_width() + 40.0, y = origin.y + axis.y + 8.0))