simvx.core.scene_tree¶
SceneTree: Central manager for the node tree, groups, input routing, and UI focus.
Module Contents¶
Classes¶
A transient one-shot timer owned by the :class: |
|
Central manager for the node tree, groups, input routing, and UI focus. |
Data¶
API¶
- simvx.core.scene_tree.RESERVED_AUTOLOAD_EVENTS¶
‘events’
- simvx.core.scene_tree.log¶
‘getLogger(…)’
- class simvx.core.scene_tree.SceneTreeTimer(seconds: float)[source]¶
A transient one-shot timer owned by the :class:
SceneTree.Created via :meth:
SceneTree.create_timer. Emitstimeoutonce after its delay elapses, then the tree drops it. Lighter than a :class:~simvx.core.Timernode: it lives only on the tree’s timer list, not in the scene graph.Initialization
- __slots__¶
(‘timeout’, ‘_time_left’)
- class simvx.core.scene_tree.SceneTree(screen_size=None, *, isolated_input: bool = False)[source]¶
Central manager for the node tree, groups, input routing, and UI focus.
Owns the root node and drives per-frame
process/physics_process/drawtraversals. Also manages pause state, scene changes, and the UI input pipeline (mouse, keyboard, popups).Initialization
- classmethod current() simvx.core.scene_tree.SceneTree | None[source]¶
Return the most recently activated SceneTree, or
None.Activation happens automatically on
set_root/change_scene. Used byInputSimulatorto deliver scene-tree + UI-tree events without requiring callers to thread a tree reference through.Held weakly: returns
Noneonce the tree has no remaining strong references (e.g. a test dropped it), which is the correct “no active tree” semantics.
- property events: simvx.core.event_bus.EventBus[source]¶
Engine-provided typed event bus.
Use
tree.events.subscribe(EventCls, handler)to register a handler andtree.events.publish(event)(orpublish_deferred) to dispatch. The bus surviveschange_scene()– subscriptions held by autoloads or other long-lived objects keep firing across scene swaps. Deferred events queued during a frame are dispatched at the start of the nextprocess()tick, before any node_processruns.
- property audio_backend: simvx.core.audio_protocol.AudioBackend | None[source]¶
The active audio backend, or
Noneif none was initialised.Returns the union :class:
AudioBackendtype for backwards compatibility: callers that only need one facet should prefer the narrowed :attr:audio_playback/ :attr:audio_streaming/- Attr:
audio_busesproperties so the type checker enforces the boundary. Reaching for the underscore-prefixed attribute is engine-private and may change.
The engine sets this during
App.runviamake_backend. Tests and headless harnesses that don’t initialise audio seeNone.
- install_audio_backend(backend: simvx.core.audio_protocol.AudioPlaybackBackend) None[source]¶
Install the audio backend for this tree: the one canonical path.
This is the only supported way to attach a backend; assigning the private
_audio_backendslot is no longer part of the contract. Used byApp.run/WebAppat startup and by tests that inject a- Class:
NullAudioBackendor a mock.
Semantics mirror startup driver selection in other engines (Godot’s
--audio-driver, pyglet’saudiooption): a backend is installed once, before the tree starts producing sound. Re-installing shuts the previous backend down first so device handles don’t leak.Facet conformance is enforced where it is consumed, not here: the
- Attr:
audio_playback/ :attr:audio_streaming/ :attr:audio_busesaccessors narrow viaisinstanceand returnNonefor a backend that doesn’t implement that facet, and callers raise- Class:
AudioCapabilityErroronNone. Gating here as well would duplicate that boundary and reject legitimate partial backends.
- property audio_playback: simvx.core.audio_protocol.AudioPlaybackBackend | None[source]¶
The active backend narrowed to :class:
AudioPlaybackBackend, orNone.Always equals :attr:
audio_backendcast to the playback facet when a backend is present: every shipped backend implements playback, including the silent :class:NullAudioBackend.
- property audio_streaming: simvx.core.audio_protocol.AudioStreamingBackend | None[source]¶
The active backend narrowed to :class:
AudioStreamingBackend, orNone.Returns
Nonewhen the active backend doesn’t implement streaming (the Null backend, any future no-device test stub). Callers that need streaming (:class:AudioSynthdriver, AudioWorklet feeds) should raise :class:AudioCapabilityErroronNone.
- property audio_buses: simvx.core.audio_protocol.AudioBusBackend | None[source]¶
The active backend narrowed to :class:
AudioBusBackend, orNone.Always equals :attr:
audio_backendcast to the bus facet when a backend is present: every shipped backend implements bus + capability advertisement.
- audio_listener_3d() simvx.core.audio_listener.AudioListener3D | None[source]¶
The active 3D audio listener, lazy-creating one if none exists.
Returns the most recently entered :class:
AudioListener3Din the scene. If none has been added, auto-creates a fallback parented to the activeCamera3Dwith a one-time warning. ReturnsNoneonly if there’s no camera either.Audio players call this every frame, so the auto-creation is cheap once it’s happened (the cached listener is returned).
- audio_listener_2d() simvx.core.audio_listener.AudioListener2D | None[source]¶
The active 2D audio listener, lazy-creating one if none exists.
Same contract as :meth:
audio_listener_3dbut for 2D.
- activate_input()[source]¶
Context manager to make this tree’s Input/InputMap the active ones.
Use this when processing the tree so that game code calling
Input.is_action_pressed(...)sees this tree’s input state._in_processingmarks the span soInputMap.add_actioncan tell a node-callback registration (lands in this tree’s map, works on every backend) from a late out-of-band call (see the web-export warning).
- property is_running: bool[source]¶
Whether the tree is actively being ticked by its driving app.
Set to False by :meth:
quit(or the graphics backend’s appquit()), signalling the main loop to exit at the end of the current frame.
- property now: float[source]¶
Monotonically-increasing scene time, in seconds.
Accumulates
dtat the start of every :meth:tickcall (after any :attr:simvx.graphics.App.time_scalescaling has been applied), so slow-motion and hitstop also slow this clock. Frozen while- Attr:
pausedis True. Resets to0.0only by constructing a freshSceneTree.
Use for time-based animation, slow-mo gating, and any “how long has the scene been running” query: preferable to per-node
self._time += dtaccumulators because every consumer reads the same monotonic value.
- quit() None[source]¶
Request a clean shutdown of the running tree.
Emits :attr:
quit_requestedand flips :attr:is_runningto False. The driving app polls this state and exits its loop at the end of the current frame. Safe to call from node callbacks or signal handlers.
- set_root(root: simvx.core.node.Node)[source]¶
Set the root node of the scene tree.
Before the root enters the tree, any
input_actionsdeclared on the root (class- or instance-leveldict[str, list]) is bulk- registered with this tree’sInputMap. This is the canonical replacement for the wrapper-class +on_readyboilerplate and surviveschange_sceneswaps – every new root’s actions are re-registered automatically.
- change_scene(new_root: simvx.core.node.Node)[source]¶
Swap the active root with
new_root.The old root receives
_exit_tree;new_rootthen runs the full_enter_tree/_ready_recursivepath, identical to the initial root. Autoloads are left in place and their groups and unique-name entries are re-registered on the rebuilt tree. Pending deletes, UI popup state, and the active 2D camera are cleared.Use this for title → gameplay → game-over navigation. See
- Doc:
../patternsfor a full example.
- flush_layout() None[source]¶
Reflow every dirty container now, root-first, and expire the rect cache.
Synchronous layout settle: after this returns, container children have their final positions/sizes for this frame, so
get_global_rectis valid immediately (no one-tick lag). Called at end-of-tick and, crucially, right when an overlay/popup opens so a freshly-shown dialog can be hit-tested and focus-elected on the same tick. Idempotent and O(1) when no container is dirty.
- tick(dt: float)[source]¶
Run process callbacks and coroutines on all nodes for one frame.
Order: autoloads’
_processfirst (Godot-style global singletons), thenself.events.flush_deferred()so deferred events queued during the previous frame (process, physics, or input) reach all subscribers before scene logic runs, then the scene root’s_process. Anythingemit_deferred’d during this frame’s autoload pass is also drained in the same flush, keeping the scene’s view of the world consistent.
- property physics_world: simvx.core.physics.world.PhysicsWorld[source]¶
The tree’s default PhysicsWorld (new seam), created lazily on first access.
Bodies with no PhysicsRoot ancestor resolve here. Lazy so non-physics scenes never allocate a world. Backend follows the selection precedence (no node override -> project
physics_backendsetting > auto-discovered native > Builtin), gravity -Y. Tree-scoped: it persists acrosschange_scene(likeevents) and is released only byquit()/ GC.
- property has_physics_world: bool[source]¶
True iff a default world has been lazily created (no allocation).
- property physics_world_2d: simvx.core.physics.world2d.Physics2DWorld[source]¶
The tree’s default 2D PhysicsWorld (T2f), created lazily on first access.
The 2D sibling of :attr:
physics_world. 2D bodies with noPhysicsRoot2Dancestor resolve here. Lazy so non-2D-physics scenes never allocate it. Backend follows the selection precedence (no node override -> projectphysics_backendsetting > auto-discovered native > Builtin), gravityVec2(0, -9.81)(Y-up). Tree-scoped: persists acrosschange_scene, released byquit()/ GC.
- register_physics_world(world: simvx.core.physics.world.PhysicsWorld) None[source]¶
Register a PhysicsRoot’s isolated world so physics_tick steps it.
Idempotent: a world already present is not added twice (defends against re-entrant enter_tree). Called from PhysicsRoot.on_enter_tree.
- unregister_physics_world(world: simvx.core.physics.world.PhysicsWorld) None[source]¶
Unregister a PhysicsRoot’s world (PhysicsRoot.on_exit_tree). No-op if absent.
- register_physics_body(world: simvx.core.physics.world.PhysicsWorld, handle: simvx.core.physics.world.BodyHandle, node: simvx.core.node.Node) None[source]¶
Register a body node for post-step transform read-back.
Called by
PhysicsBody3D.on_enter_treefor non-static bodies. Marks the world’s membershipdirtyso the next sync rebuilds the bulk order + buffer. Weak reference: a GC’d node auto-drops.
- unregister_physics_body(world: simvx.core.physics.world.PhysicsWorld, handle: simvx.core.physics.world.BodyHandle) None[source]¶
Unregister a body node (
PhysicsBody3D.on_exit_tree). No-op if absent.Drops the whole per-world
_BodySynconce empty so idle worlds cost nothing.
- register_physics_node(world: simvx.core.physics.world.PhysicsWorld, handle: simvx.core.physics.world.BodyHandle, node: simvx.core.node.Node) None[source]¶
Register a body node in the collision-event handle->node map.
Called by
PhysicsBody3D.on_enter_treefor ALL modes (static included), so :meth:_dispatch_contact_eventscan resolve both sides of a contact pair. Mode-independent: unlikeregister_physics_bodythis map never changes on a mode flip. Weak reference: a GC’d node auto-drops.
- unregister_physics_node(world: simvx.core.physics.world.PhysicsWorld, handle: simvx.core.physics.world.BodyHandle) None[source]¶
Unregister a body node from the collision-event map. No-op if absent.
Drops the whole per-world map once empty so idle worlds cost nothing.
- interpolate_physics(alpha: float) None[source]¶
Write the render pose of every captured new-world body: lerp(prev, cur, alpha).
alphais the fixed-step fractionphysics_accum / physics_dtin [0, 1). Position lerps linearly; rotation slerps (shortest-arc, Quat.slerp). A sleeping body has prev == cur, so it lerps to a constant (correct, cheap). No-op whenphysics_interpolationis False (driver still calls it; the raw write already happened in _sync_physics_world during physics_tick).
- physics_tick(dt: float)[source]¶
Run physics_process callbacks on all nodes, then auto-step physics.
- propagate_input(event: simvx.core.events.TreeInputEvent) None[source]¶
Dispatch an input event to registered
@on_inputhandlers.Looks up handlers via the typed dispatch tables (built when nodes carrying
@on_input-decorated methods enter the tree). The traversal is O(handlers per event), not O(nodes): nodes without any input handlers cost nothing.A handler returning a truthy value marks the event consumed:
on_unhandled_inputonly fires if nothing consumed the event.event.handledis also flipped True so callers can short-circuit.
- input_cast(screen_pos: tuple[float, float] | numpy.ndarray, button: simvx.core.input.enums.MouseButton = MouseButton.LEFT)[source]¶
Cast a ray from screen_pos through the camera into the scene. Finds the nearest pickable CollisionShape3D and delivers an InputEvent to its parent node.
- get_group(name: str) list[simvx.core.node.Node][source]¶
Get all nodes in a group.
- get_first_in_group(name: str) simvx.core.node.Node | None[source]¶
Return one node from
name(any member), orNoneif empty.
- call_group(name: str, method: str, *args) None[source]¶
Call
method(*args)on every node innamethat defines it.Convenience for the common broadcast (
for n in tree.get_group(...): n.method(...)). Nodes lackingmethodare skipped, so mixed-type groups are safe.
- create_timer(seconds: float) simvx.core.scene_tree.SceneTreeTimer[source]¶
Create a one-shot timer that emits
timeoutafterseconds.Returns immediately with a :class:
SceneTreeTimer; connect itstimeoutsignal to run code after the delay. The timer is owned and ticked by the tree (right after autoloads) and discarded once it fires, so it needs no Node in the scene and no manual cleanup. Honourstree.paused. For delays inside a coroutine, preferwait(seconds).
- call_deferred(method: collections.abc.Callable[..., Any], *args: Any) None[source]¶
Queue
method(*args)to run at the end of this frame (escape hatch).The tree-level counterpart to :meth:
Node.call_deferred, for code that has no node to defer from (plugins, autoload helpers, non-node systems). Same discouraged-escape-hatch guidance applies: prefer a safe-by-default path (destroy,Property(coalesce=True),events.publish_deferred) when one exists.methodis a callable, never a string.
- property autoloads: dict[str, simvx.core.node.Node][source]¶
Read-only view of registered autoloads.
- add_autoload(name: str, node: simvx.core.node.Node)[source]¶
Register
nodeas a persistent singleton attached to the tree.The node enters the tree and runs
on_ready()immediately. Unlike a regular child, it is not reachable via the scene root: retrieve it viatree.autoloads[name]. Autoloads survivechange_scene(), making them the canonical home for global state (score, settings, audio manager). See :doc:../patterns.The name
"events"is reserved for the engine-provided- Class:
~simvx.core.event_bus.EventBus; usetree.eventsinstead.
- remove_autoload(name: str)[source]¶
Unregister and tear down an autoload. Calls
_exit_treeon the node.
- get_unique(name: str) simvx.core.node.Node | None[source]¶
Get a unique node by name. Returns None if not found.
- ui_input(mouse_pos: tuple[float, float] | numpy.ndarray = None, button: simvx.core.input.enums.MouseButton | None = None, pressed: bool = True, key: str = '', char: str = '')[source]¶
Route UI input events to controls.
buttonis aMouseButtonfor press/release,Nonefor keyboard / char / pure mouse-move events.