Python example: mrpt_serialization_example.py
Serializes MRPT objects to bytes and back with mrpt.serialization.
Modules: mrpt.rtti, mrpt.serialization, mrpt.poses
1#!/usr/bin/env python3
2"""
3Serializes MRPT objects to bytes and back with mrpt.serialization.
4
5Demonstrates:
6 - objectToBytes / bytesToObject: round-trip binary serialisation via Python bytes
7 - CSerializable base class
8 - Uses mrpt.poses.CPose3D as a concrete serialisable object
9"""
10
11import mrpt.rtti # must be imported first to register TRuntimeClassId (used by GetRuntimeClass)
12from mrpt.serialization import objectToBytes, bytesToObject
13from mrpt.poses import CPose3D
14
15# ---------------------------------------------------------------------------
16# Create a pose and serialise it to a Python bytes object
17# ---------------------------------------------------------------------------
18pose = CPose3D(1.0, 2.0, 3.0, 0.1, 0.2, 0.3)
19print(f"Original pose: {pose}")
20
21raw = objectToBytes(pose)
22print(f"Serialised to {len(raw)} bytes")
23assert isinstance(raw, bytes)
24assert len(raw) > 0
25
26# ---------------------------------------------------------------------------
27# Deserialise back and verify
28# ---------------------------------------------------------------------------
29pose2 = bytesToObject(raw)
30print(f"Deserialised: {pose2}")
31
32# The returned object is a shared_ptr<CSerializable>; cast via GetRuntimeClass
33cls_name = pose2.GetRuntimeClass().className
34print(f" Runtime class: {cls_name}")
35assert "CPose3D" in cls_name, f"Expected CPose3D, got {cls_name}"
36print(f" (full className: {cls_name})")
37print(" Round-trip OK ✓")