Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.5

0.11.5 / src / app.luc

## The application: the window and its panels, settings kept between
## runs, the compute worker's poll every frame, and the error handler.
from luce_ui.ui import Application, Font
from luce_cad.cad_geometry import CadGeometry
import theme
from workspace import Workspace
from panels import Panels
from interop import Connection
from ui_state import UIState, settings_path
import node_commands

pub class Luce3D:
    pub let app: Application
    pub let workspace: Workspace
    pub let panels: Panels
    let ticking: Connection
    let compute_timer: int
    let settings: UIState

    pub func init(self, persist: bool = false, width: int = 1400, height: int = 900) -> unit!:
        # Geometry files carry CAD models too (the Cache node).
        _ = CadGeometry.register_codec()
        self.settings = UIState(settings_path() if persist else "")
        self.workspace = Workspace()
        self.panels = Panels(self.workspace, Font())
        self.settings.restore(self.panels)
        self.app = Application(self.panels.body, title = "Luced 3D", width = self.settings.width if persist else width, height = self.settings.height if persist else height, theme = theme.editor_theme())
        self.panels.projects.app = self.app
        self.app.set_close_handler(self.panels.projects.can_quit)
        self.app.focus(self.panels.view.layout())
        self.app.set_commands(self.panels.actions.all)
        self.ticking = self.app.on_frame(self.panels.poll)
        self.compute_timer = self.app.every(0.016, func () -> unit!:
            self.panels.poll(0.016)
            self.settings.poll(self.panels, 0.016))
        self.app.set_error_handler(self.panels.report_error)
        self.panels.inspector.on_browse = self.browse_geometry
        self.panels.inspector.on_render = self.panels.render
        if self.settings.warning != "": self.panels.report_error(self.settings.warning)

    ## A path row's Browse: a geometry file for a File node, any file for
    ## another node's path row (a Script node's k.file), into text slot `slot`.
    func browse_geometry(self, slot: int) -> unit!:
        let geometry = slot == 0
        let path = self.app.open_file("Open geometry (.obj, .fbx, .ply, .spz, .splat, .usd, .step, .stp)" if geometry else "Open a file")
        if path != "":
            node_commands.set_text(self.workspace, slot, path)
            self.panels.refresh()

    pub func run(self, arguments: list[str]) -> unit!:
        self.workspace.enable_compute()
        # A project named on the command line that cannot be opened is
        # reported, and the editor opens without it.
        for argument in arguments:
            if argument == "--smoke" or argument == "--empty": continue
            self.panels.projects.load_from(argument) catch failure:
                print(f"luced-3d: could not open {argument}: {failure.message}")
        self.app.run(frame_limit = 3 if "--smoke" in arguments else 0)

    pub func close(self):
        self.settings.save(self.panels) catch failure:
            _ = failure
        self.app.cancel(self.compute_timer)
        if let compute = self.workspace.compute:
            compute.close()
        self.ticking.disconnect()
        self.panels.close()
        self.app.close()