How to work with arrays in rosmsgs?

Hi, i am working with visual markers in rviz using Marker.msg.

The markers.msg is as follows:


uint8 ARROW=0
uint8 CUBE=1
uint8 SPHERE=2
uint8 CYLINDER=3
uint8 LINE_STRIP=4
uint8 LINE_LIST=5
uint8 CUBE_LIST=6
uint8 POINTS=8
uint8 ADD=0
uint8 MODIFY=0
uint8 DELETE=2
std_msgs/Header header
string ns
int32 id
int32 type
int32 action
geometry_msgs/Pose pose
geometry_msgs/Vector3 scale
std_msgs/ColorRGBA color
duration lifetime
bool frame_locked
geometry_msgs/Point[] points
std_msgs/ColorRGBA[] colors
string text
string mesh_resource
bool mesh_use_embedded_materials

geometry_msgs/Point.msg is as follows:

# This contains the position of a point in free space
float64 x
float64 y
float64 z

Now the geometry_msgs/Point[] points from Marker.msg is made of array of Point.msg. Now say i wanted to store the values (x=1,y=1,z=1) and (x=2,y=2,z=2) into the points array mentioned in Marker.msg, how would i do it? I am unsure how to work with arrays in messages.

Can someone share the basic idea / code on how to go about it?

Thanks in advace

Hi @Joseph1001 ,

Doing this is very simple.

If you are using C++ use a suitable container like list or vector with the correct datatype.
So, basically, a geometry_msgs/Point is a 3-value float64 data.
So you can initialize an array of Points as vector<geometry_msgs::Point> list_of_points;
Use vector to access elements by index. Use list if you just want to push data into an array and if you don’t want to edit the items in the array.

If you are using python - things get simpler - everything is just a list of dicts or just dicts or lists.
Accessing an array is just by calling the id of the data.
So, in case of array of Points, you will have a list or tuple of Points. Each Point would be a dict (since you have x, y, z structure) so you would essentially have a list of dicts (in most cases) or tuple of dicts.


1 Like

Hello @Joseph1001,

this is a minimal example in python and ROS1. I have not tested this particular snipped myself, and I would say it’s probably not best practices to write the code as I did. But it should get you started and you can later improve upon it in several ways.

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import rospy
from visualization_msgs.msg import Marker
from std_msgs.msg import ColorRGBA
from geometry_msgs.msg import Point
from geometry_msgs.msg import Vector3

marker_pub = rospy.Publisher("marker", Marker, queue_size=1)

rviz_points = Marker()

rviz_points.header.frame_id = "map"
rviz_points.ns = "points" = 1

rviz_points.type = Marker.POINTS
rviz_points.action = Marker.ADD

rviz_points.color = ColorRGBA(1.0, 1.0, 0.0, 1.0)
rviz_points.scale = Vector3(0.2, 0.2, 0.2)

# Add as many points as required, or use a loop instead
rviz_points.points.append(Point(0, 0, 0))
rviz_points.points.append(Point(1, 5, 0))
rviz_points.points.append(Point(1, 7, 0))

while not rospy.is_shutdown():

Hope it helps,


1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.