Line visualization¶
Create line segments and smooth splines for wireframe and path visualization.
This example demonstrates viser’s line rendering capabilities, which are essential for visualizing paths, trajectories, wireframes, and vector fields. Lines are commonly used in robotics for path planning, in computer graphics for wireframe models, and in data visualization for connections between points.
Line types available:
viser.SceneApi.add_line_segments()
for straight line segments (most efficient for many lines)viser.SceneApi.add_spline_catmull_rom()
for smooth curves through control pointsviser.SceneApi.add_spline_cubic_bezier()
for precise curve control with Bezier handles
The example shows performance best practices: batching many line segments into a single call is much more efficient than creating individual scene objects for each line.
Source: examples/01_scene/03_lines.py

Code¶
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 points=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 points=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()