Python example: mrpt_gui_example.py

Opens a 3D window with mrpt.gui and populates its scene.

Modules: mrpt.gui, mrpt.viz | Requires: gui

 1#!/usr/bin/env python3
 2"""
 3Opens a 3D window with mrpt.gui and populates its scene.
 4
 5Demonstrates:
 6  - CDisplayWindowGUI: create, set its 3D scene, camera control
 7  - Adding objects (CGridPlaneXY, CPointCloudColoured, CAxis) to the scene
 8  - spinOnce(): processing window events and repainting from your own loop
 9
10Pass --interactive to keep the window open until it is closed; otherwise it
11shows a few frames and quits.
12
13NOTE: This example requires a display (X11/Wayland). Without one, it exits
14gracefully without rendering.
15"""
16
17import argparse
18import math
19import os
20import sys
21import time
22
23parser = argparse.ArgumentParser()
24parser.add_argument("--interactive", action="store_true",
25                    help="Keep the window open until it is closed")
26args = parser.parse_args()
27
28if not (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")):
29    print("No display: skipping this example.")
30    sys.exit(0)
31
32from mrpt.gui import CDisplayWindowGUI
33from mrpt.viz import Scene, CGridPlaneXY, CPointCloudColoured, CAxis
34
35# ---------------------------------------------------------------------------
36# Create window
37# ---------------------------------------------------------------------------
38win = CDisplayWindowGUI("mrpt_gui_example: 3D viewer", 800, 600)
39print(f"Window created: {win}")
40print(f"  isOpen = {win.isOpen()}")
41
42# Camera setup (the mouse also moves it: drag to rotate, wheel to zoom)
43cam = win.camera()
44cam.setElevationDegrees(30)
45cam.setAzimuthDegrees(45)
46cam.setZoomDistance(10)
47cam.setCameraPointing(0, 0, 0)
48
49# ---------------------------------------------------------------------------
50# Populate the scene
51# ---------------------------------------------------------------------------
52scene = Scene()
53
54# Floor grid
55scene.insert(CGridPlaneXY())
56
57# Coordinate axes
58axes = CAxis()
59axes.setAxisLimits(-3, -3, -3, 3, 3, 3)
60scene.insert(axes)
61
62# A small coloured point cloud
63pts = CPointCloudColoured()
64for i in range(100):
65    angle = 2 * math.pi * i / 100
66    r = 2.0
67    pts.push_back(
68        r * math.cos(angle),
69        r * math.sin(angle),
70        math.sin(2 * angle),          # z varies
71        abs(math.cos(angle)),         # R
72        abs(math.sin(angle)),         # G
73        0.5,                          # B
74    )
75pts.setPointSize(4.0)
76scene.insert(pts)
77
78win.background_scene = scene
79print("Scene populated: grid + axes + 100-point colour cloud")
80print(f"  Camera elevation={cam.elevation_deg:.0f} deg  "
81      f"azimuth={cam.azimuth_deg:.0f} deg  zoom={cam.zoom:.1f}")
82
83# ---------------------------------------------------------------------------
84# GUI loop
85# ---------------------------------------------------------------------------
86frames = 0
87while win.spinOnce():
88    frames += 1
89    if not args.interactive and frames >= 20:
90        break
91    time.sleep(0.02)
92print(f"Rendered {frames} frames.")