LinesΒΆ
Make a ball with some random line segments and splines.
1import time
2
3import numpy as np
4
5import viser
6
7
8def main() -> None:
9 server = viser.ViserServer()
10
11 # Line segments.
12 #
13 # This will be much faster than creating separate scene objects for
14 # individual line segments or splines.
15 N = 2000
16 points = np.random.normal(size=(N, 2, 3)) * 3.0
17 colors = np.random.randint(0, 255, size=(N, 2, 3))
18 server.scene.add_line_segments(
19 "/line_segments",
20 points=points,
21 colors=colors,
22 line_width=3.0,
23 )
24
25 # Spline helpers.
26 #
27 # If many lines are needed, it'll be more efficient to batch them in
28 # `add_line_segments()`.
29 for i in range(10):
30 points = np.random.normal(size=(30, 3)) * 3.0
31 server.scene.add_spline_catmull_rom(
32 f"/catmull/{i}",
33 positions=points,
34 tension=0.5,
35 line_width=3.0,
36 color=np.random.uniform(size=3),
37 segments=100,
38 )
39
40 control_points = np.random.normal(size=(30 * 2 - 2, 3)) * 3.0
41 server.scene.add_spline_cubic_bezier(
42 f"/cubic_bezier/{i}",
43 positions=points,
44 control_points=control_points,
45 line_width=3.0,
46 color=np.random.uniform(size=3),
47 segments=100,
48 )
49
50 while True:
51 time.sleep(10.0)
52
53
54if __name__ == "__main__":
55 main()