Core concepts

Creating a server, adding 3D objects to the scene, and building interactive GUI controls.

We start by creating a viser.ViserServer instance, which automatically opens a web interface at http://localhost:8080. The server provides two main APIs:

Source: examples/00_getting_started/01_core_concepts.py

Core concepts

Code

 1import time
 2
 3import viser
 4
 5
 6def main():
 7    server = viser.ViserServer()
 8
 9    # Add 3D objects to the scene
10    sphere = server.scene.add_icosphere(
11        name="/sphere",
12        radius=0.3,
13        color=(255, 100, 100),
14        position=(0.0, 0.0, 0.0),
15    )
16    box = server.scene.add_box(
17        name="/box",
18        dimensions=(0.4, 0.4, 0.4),
19        color=(100, 255, 100),
20        position=(1.0, 0.0, 0.0),
21    )
22
23    # Create GUI controls
24    sphere_visible = server.gui.add_checkbox("Show sphere", initial_value=True)
25    sphere_color = server.gui.add_rgb("Sphere color", initial_value=(255, 100, 100))
26    box_height = server.gui.add_slider(
27        "Box height", min=-1.0, max=1.0, step=0.1, initial_value=0.0
28    )
29
30    # Connect GUI controls to scene objects
31    @sphere_visible.on_update
32    def _(_):
33        sphere.visible = sphere_visible.value
34
35    @sphere_color.on_update
36    def _(_):
37        sphere.color = sphere_color.value
38
39    @box_height.on_update
40    def _(_):
41        box.position = (1.0, 0.0, box_height.value)
42
43    print("Server running")
44    while True:
45        time.sleep(10.0)
46
47
48if __name__ == "__main__":
49    main()