I want to obtain the same result as ros2 node list
using rclpy
but I haven't found a way yet.
Here is what I have tried.
import rclpy
rclpy.init()
node = rclpy.create_node("list_nodes_example")
# Use the get_node_names_and_namespaces() method to get the list of nodes and their namespaces
node_namespaces = node.get_node_names_and_namespaces()
# Print out the nodes and their namespaces
for name, namespace in node_namespaces:
print(namespace + name)
node.destroy_node()
rclpy.shutdown()
This only show the node it creates instead of all the nodes in the ROS workspace.