In Python, can I initialize arrays in message without importing element types like C++ resize()?
In C++, I can initialize arrays in message with resize()
, so I don't have to find, include or initialize element types of allay.
In Python, I initialize arrays in message as follows.
from A_msg.msg import A
from B_msg.msg import B
from C_msg.msg import C
msg_a = A()
msg_a.bs = [B()]
msg_a.bs[0].cs = [C()]
msg_a.bs[0].cs[0].data = 0
It takes time and effort to find and import element types, especially when message has multiple layers of array and contains multiples types over multiple layers.
Can I initialize arrays in message without importing element types like C++ as follow?
from A_msg.msg importA
msg_a = A()
msg_a.bs.resize(1)
msg_a.bs[0].cs.resize(1)
msg_a.bs[0].cs[0].data = 0
The default constructor of a ros message already initialize all the fields of the message to 0 (or empty array), do you have a message that isn't propperly intialized ? Is
msg_a = A()
not working ? For example with a PolygonStamped msg, this :Will return a PoseStamped object with all the values to 0 (and an the geometry_msgs/Point32 array will be empty too).
Messages is properly initialized. But I have difficulty in initialize sub messages contained in array of the message without importing sub messages' types.
B.polygon.points.append(1)
In your above example, it appears to add a integer "1" into Point32 array, don't it? Is it valid operation?
I ran the below code and got error.
code:
error:
Oups my bad you are right, apparently it doesn't complain if you simply print. It was just to illustrate that when you call the message constructor everything is reset. I still don't understand your issue to be honest, you don't need to import all the sub messages but only the one you want to use with your array. In my case I should have done :
Here you don't need to import a Polygon msg but directly the message you need (Point). Sorry for the bad example.