In ROS1 you can convert a pointCloud2 message to an xyz array with sensor_msgs.point_cloud2.read_points().
Unfortunately, this option has not yet been adapted for ROS2.
What other options are there to obtain this.
Always fun to Google a problem and find my own answers. The comment I left a few years ago (before I personally was using ROS2) just links to the documentation.
Now I'm using ROS2 and I need to read points in Python lol. If you want to use the method, then you need a slightly different import statement. Using from sensor_msgs.msg import PointCloud2 gets you the message definition, but doesn't get you the utility methods. For that, you want:
from sensor_msgs_py import point_cloud2
Now that you've got point_cloud2 you can access the point cloud helper methods. For example, if you want to read the x/y/z and reflectivity values from a pointcloud, then you can:
pc2_data = point_cloud2.read_points_list(pc2_msg, field_names=["x", "y", "z", "reflectivity"], skip_nans=True)
If you want to convert that output from a list of named tuple to a numpy array for further processing, then it's just
import numpy as np
data_array = np.array(pc2_data)
and each column corresponds to the fields you named in the read_points_list method.
read_pointsmethod in the ROS2 Github project. Is there something not working about this? $\endgroup$