Type system
Rosbags ships its own pure python typesystem rosbags.typesys. It uses parse trees to represent message definitions internally. It ships its own .idl and .msg definition parser to convert message definition files into the internal format.
Out of the box it supports the message types defined by the standard ROS2 distribution. Message types can be parsed and added on the fly during runtime without an additional build step.
Message instances
The type system generates a dataclass for each message type. These dataclasses give direct read write access to all mutable fields of a message. Fields should be mutated with care as no type checking is applied during runtime.
Note
Limitation: While the type system parses message definitions with array bounds and/or default values, neither bounds nor default values are enforced or assigned to message instances.
Included message types
builtin_interfaces
DurationTime
diagnostic_msgs
DiagnosticArrayDiagnosticStatusKeyValue
geometry_msgs
AccelAccelStampedAccelWithCovarianceAccelWithCovarianceStampedInertiaInertiaStampedPointPoint32PointStampedPolygonPolygonStampedPosePose2DPoseArrayPoseStampedPoseWithCovariancePoseWithCovarianceStampedQuaternionQuaternionStampedTransformTransformStampedTwistTwistStampedTwistWithCovarianceTwistWithCovarianceStampedVector3Vector3StampedWrenchWrenchStamped
libstatistics_collector
DummyMessage
lifecycle_msgs
StateTransitionTransitionDescriptionTransitionEvent
rcl_interfaces
FloatingPointRangeIntegerRangeListParametersResultLogParameterParameterDescriptorParameterEventParameterEventDescriptorsParameterTypeParameterValueSetParametersResult
rmw_dds_common
GidNodeEntitiesInfoParticipantEntitiesInfo
rosgraph_msgs
Clock
sensor_msgs
BatteryStateCameraInfoChannelFloat32CompressedImageFluidPressureIlluminanceImageImuJointStateJoyJoyFeedbackJoyFeedbackArrayLaserEchoLaserScanMagneticFieldMultiDOFJointStateMultiEchoLaserScanNavSatFixNavSatStatusPointCloudPointCloud2PointFieldRangeRegionOfInterestRelativeHumidityTemperatureTimeReference
shape_msgs
MeshMeshTrianglePlaneSolidPrimitive
statistics_msgs
MetricsMessageStatisticDataPointStatisticDataType
std_msgs
BoolByteByteMultiArrayCharColorRGBAEmptyFloat32Float32MultiArrayFloat64Float64MultiArrayHeaderInt16Int16MultiArrayInt32Int32MultiArrayInt64Int64MultiArrayInt8Int8MultiArrayMultiArrayDimensionMultiArrayLayoutStringUInt16UInt16MultiArrayUInt32UInt32MultiArrayUInt64UInt64MultiArrayUInt8UInt8MultiArray
stereo_msgs
DisparityImage
tf2_msgs
TF2ErrorTFMessage
trajectory_msgs
JointTrajectoryJointTrajectoryPointMultiDOFJointTrajectoryMultiDOFJointTrajectoryPoint
unique_identifier_msgs
UUID
visualization_msgs
ImageMarkerInteractiveMarkerInteractiveMarkerControlInteractiveMarkerFeedbackInteractiveMarkerInitInteractiveMarkerPoseInteractiveMarkerUpdateMarkerMarkerArrayMenuEntry
Extending the type system
Adding custom message types consists of two steps. First, message definitions are converted into parse trees using get_types_from_idl() or get_types_from_msg(), and second the types are registered in the type system via register_types(). The following example shows how to add messages type definitions from .msg and .idl files:
from pathlib import Path
from rosbags.typesys import get_types_from_idl, get_types_from_msg, register_types
idl_text = Path('foo_msgs/msg/Foo.idl').read_text()
msg_text = Path('bar_msgs/msg/Bar.msg').read_text()
# plain dictionary to hold message definitions
add_types = {}
# add all definitions from one idl file
add_types.update(get_types_from_idl(idl_text))
# add definition from one msg file
add_types.update(get_types_from_msg(msg_text, 'bar_msgs/msg/Bar'))
# make types available to rosbags serializers/deserializers
register_types(add_types)