Skip to content

Commit

Permalink
Move "is_initialized" flag for 3D scene from server to client (#3452)
Browse files Browse the repository at this point in the history
* add is_initialized to js

* remove "is_initialized" from scene.py

* minor refactor for consistency

* remove "is_initialized" from scene_view.py

* cleanup

---------

Co-authored-by: Falko Schindler <falko@zauberzeug.com>
  • Loading branch information
afullerx and falkoschindler committed Aug 16, 2024
1 parent 628b1b8 commit 7081253
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 16 deletions.
3 changes: 3 additions & 0 deletions nicegui/elements/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default {
this.objects = new Map();
this.objects.set("scene", this.scene);
this.draggable_objects = [];
this.is_initialized = false;

window["scene_" + this.$el.id] = this.scene; // NOTE: for selenium tests only

Expand Down Expand Up @@ -214,6 +215,7 @@ export default {

methods: {
create(type, id, parent_id, ...args) {
if (!this.is_initialized) return;
let mesh;
if (type == "group") {
mesh = new THREE.Group();
Expand Down Expand Up @@ -459,6 +461,7 @@ export default {
this.camera.updateProjectionMatrix();
},
init_objects(data) {
this.is_initialized = true;
for (const [
type,
id,
Expand Down
8 changes: 0 additions & 8 deletions nicegui/elements/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing_extensions import Self

from .. import binding
from ..awaitable_response import AwaitableResponse, NullResponse
from ..dataclasses import KWONLY_SLOTS
from ..element import Element
from ..events import (
Expand Down Expand Up @@ -113,7 +112,6 @@ def __init__(self,
self._props['click_events'] = click_events
self._drag_start_handlers = [on_drag_start] if on_drag_start else []
self._drag_end_handlers = [on_drag_end] if on_drag_end else []
self.is_initialized = False
self.on('init', self._handle_init)
self.on('click3d', self._handle_click)
self.on('dragstart', self._handle_drag)
Expand Down Expand Up @@ -170,7 +168,6 @@ def __getattribute__(self, name: str) -> Any:
return attribute

def _handle_init(self, e: GenericEventArguments) -> None:
self.is_initialized = True
with self.client.individual_target(e.args['socket_id']):
self.move_camera(duration=0)
self.run_method('init_objects', [obj.data for obj in self.objects.values()])
Expand All @@ -182,11 +179,6 @@ async def initialized(self) -> None:
await self.client.connected()
await event.wait()

def run_method(self, name: str, *args: Any, timeout: float = 1, check_interval: float = 0.01) -> AwaitableResponse:
if not self.is_initialized:
return NullResponse()
return super().run_method(name, *args, timeout=timeout, check_interval=check_interval)

def _handle_click(self, e: GenericEventArguments) -> None:
arguments = SceneClickEventArguments(
sender=self,
Expand Down
8 changes: 0 additions & 8 deletions nicegui/elements/scene_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from typing_extensions import Self

from ..awaitable_response import AwaitableResponse, NullResponse
from ..element import Element
from ..events import GenericEventArguments, SceneClickEventArguments, SceneClickHit, handle_event
from .scene import Scene, SceneCamera
Expand Down Expand Up @@ -43,7 +42,6 @@ def __init__(self,
self._props['camera_type'] = self.camera.type
self._props['camera_params'] = self.camera.params
self._click_handlers = [on_click] if on_click else []
self.is_initialized = False
self.on('init', self._handle_init)
self.on('click3d', self._handle_click)

Expand All @@ -53,7 +51,6 @@ def on_click(self, callback: Callable[..., Any]) -> Self:
return self

def _handle_init(self, e: GenericEventArguments) -> None:
self.is_initialized = True
with self.client.individual_target(e.args['socket_id']):
self.move_camera(duration=0)

Expand All @@ -64,11 +61,6 @@ async def initialized(self) -> None:
await self.client.connected()
await event.wait()

def run_method(self, name: str, *args: Any, timeout: float = 1, check_interval: float = 0.01) -> AwaitableResponse:
if not self.is_initialized:
return NullResponse()
return super().run_method(name, *args, timeout=timeout, check_interval=check_interval)

def _handle_click(self, e: GenericEventArguments) -> None:
arguments = SceneClickEventArguments(
sender=self,
Expand Down

0 comments on commit 7081253

Please sign in to comment.