Python example: mrpt_gui_example.py

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

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

 1#!/usr/bin/env python3
 2"""
 3Opens a 3D window with mrpt.gui and populates its scene.
 4
 5Demonstrates:
 6  - CDisplayWindow3D: create, scene access, camera control
 7  - Adding objects (CGridPlaneXY, CPointCloudColoured) to the scene
 8  - Non-interactive usage: open, add objects, repaint, close immediately.
 9
10NOTE: This example requires a display (X11/Wayland).  In headless / CI
11environments it will exit gracefully without rendering.
12"""
13
14import sys
15try:
16    from mrpt.gui import CDisplayWindow3D
17    from mrpt.viz import (
18        Scene,
19        CGridPlaneXY,
20        CPointCloudColoured,
21        CAxis,
22    )
23    from mrpt.math import TPoint3D
24except ImportError as e:
25    print(f"Import failed (headless?): {e}")
26    sys.exit(0)
27
28# ---------------------------------------------------------------------------
29# Create window
30# ---------------------------------------------------------------------------
31win = CDisplayWindow3D("mrpt_gui_example — 3D viewer", 800, 600)
32print(f"Window created: {win}")
33print(f"  isOpen = {win.isOpen()}")
34
35# Camera setup
36win.setCameraElevationDeg(30)
37win.setCameraAzimuthDeg(45)
38win.setCameraZoom(10)
39win.setCameraPointingToPoint(0, 0, 0)
40
41# ---------------------------------------------------------------------------
42# Populate the scene
43# ---------------------------------------------------------------------------
44scene = win.get3DSceneAndLock()
45
46# Floor grid
47grid = CGridPlaneXY()
48scene.insert(grid)
49
50# Coordinate axes
51axes = CAxis()
52axes.setAxisLimits(-3, -3, -3, 3, 3, 3)
53scene.insert(axes)
54
55# A small coloured point cloud
56pts = CPointCloudColoured()
57import math
58for i in range(100):
59    angle = 2 * math.pi * i / 100
60    r = 2.0
61    pts.push_back(
62        r * math.cos(angle),
63        r * math.sin(angle),
64        math.sin(2 * angle),          # z varies
65        abs(math.cos(angle)),         # R
66        abs(math.sin(angle)),         # G
67        0.5,                          # B
68    )
69pts.setPointSize(4.0)
70scene.insert(pts)
71
72win.unlockAccess3DScene()
73win.forceRepaint()
74
75print("Scene populated: grid + axes + 100-point colour cloud")
76print(f"  Camera elevation={win.getCameraElevationDeg():.0f}°  "
77      f"azimuth={win.getCameraAzimuthDeg():.0f}°  "
78      f"zoom={win.getCameraZoom():.1f}")
79print("  (window would stay open interactively; closing immediately in this demo)")