Skip to content

Commit ec74e6a

Browse files
Improve performance of 3D scenes by initializing all objects at once (#3022)
* introduce "init" message to send 3d objects more efficiently * send data for all objects at once
1 parent d6236c0 commit ec74e6a

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

nicegui/elements/scene.js

+30
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,36 @@ export default {
435435
}
436436
this.camera.updateProjectionMatrix();
437437
},
438+
init_objects(data) {
439+
for (const [
440+
type,
441+
id,
442+
parent_id,
443+
args,
444+
name,
445+
color,
446+
opacity,
447+
side,
448+
x,
449+
y,
450+
z,
451+
R,
452+
sx,
453+
sy,
454+
sz,
455+
visible,
456+
draggable,
457+
] of data) {
458+
this.create(type, id, parent_id, ...args);
459+
this.name(id, name);
460+
this.material(id, color, opacity, side);
461+
this.move(id, x, y, z);
462+
this.rotate(id, R);
463+
this.scale(id, sx, sy, sz);
464+
this.visible(id, visible);
465+
this.draggable(id, draggable);
466+
}
467+
},
438468
},
439469

440470
props: {

nicegui/elements/scene.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ def _handle_init(self, e: GenericEventArguments) -> None:
170170
self.is_initialized = True
171171
with self.client.individual_target(e.args['socket_id']):
172172
self.move_camera(duration=0)
173-
for obj in self.objects.values():
174-
obj.send()
173+
self.run_method('init_objects', [obj.data for obj in self.objects.values()])
175174

176175
async def initialized(self) -> None:
177176
"""Wait until the scene is initialized."""

nicegui/elements/scene_object3d.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ def with_name(self, name: str) -> Self:
4242
self._name()
4343
return self
4444

45-
def send(self) -> None:
46-
"""Send the object to the client."""
47-
self._create()
48-
self._name()
49-
self._material()
50-
self._move()
51-
self._rotate()
52-
self._scale()
53-
self._visible()
54-
self._draggable()
45+
@property
46+
def data(self) -> List[Any]:
47+
"""Data to be sent to the frontend."""
48+
return [
49+
self.type, self.id, self.parent.id, self.args,
50+
self.name,
51+
self.color, self.opacity, self.side_,
52+
self.x, self.y, self.z,
53+
self.R,
54+
self.sx, self.sy, self.sz,
55+
self.visible_,
56+
self.draggable_,
57+
]
5558

5659
def __enter__(self) -> Self:
5760
self.scene.stack.append(self)

0 commit comments

Comments
 (0)