Add urdf to world using a python script while gazebo is running (/world/my_world/create service + EntityFactory message)
Hi
I'm trying to add a cube to my gazebo scene programmatically while gazebo is running.
I found that ignition has the service /world/my_world/create
service that accepts new urdf/sdf files.
However, I'm struggling to send the correct request in python, since I don't know how to install the ignition messages on my system and how the request has to be structured.
Here is a tutorial in C++: https://gazebosim.org/api/gazebo/6.9/...
Here is the terminal call which spawns a sphere in the world:
ign service -s /world/empty/create \
--reqtype ignition.msgs.EntityFactory \
--reptype ignition.msgs.Boolean \
--timeout 300 \
--req 'sdf: '\
'"<?xml version=\"1.0\" ?>'\
'<sdf version=\"1.6\">'\
'<model name=\"spawned_model\">'\
'<link name=\"link\">'\
'<visual name=\"visual\">'\
'<geometry><sphere><radius>1.0</radius></sphere></geometry>'\
'</visual>'\
'<collision name=\"visual\">'\
'<geometry><sphere><radius>1.0</radius></sphere></geometry>'\
'</collision>'\
'</link>'\
'</model>'\
'</sdf>" '\
'pose: {position: {z: 10}} '\
'name: "new_name" '\
'allow_renaming: true'
My plan:
- Install ignition messages (But how?)
Setup ign bridge to forward message /world/my_world/create from ign to ROS2 (in launch file)
ros_ign_bridge (clock -> ROS 2)
Node( package="ros_ign_bridge", executable="parameter_bridge", output="log", arguments=[ "/world/my_world/create@ignition.msgs.EntityFactory@ignition.msgs.EntityFactory", "--ros-args", "--log-level", log_level, ], parameters=[{"use_sim_time": use_sim_time}], ),
Use python script to spawn cube.urdf in gazebo world
Importing the library:
from ignition-messages import EntityFactory # What is the correct syntax/name of the library?
Publishing the message: How do I construct a valid message?
my_sdf = """
<?xml version="1.0" ?>
<sdf version='1.7'>
<model name='sphere'>
<link name='link'>
<pose>0 0 0.5 0 0 0</pose>
<visual name='visual'>
<geometry><sphere><radius>1</radius></sphere></geometry>
</visual>
<collision name='collision'>
<geometry><sphere><radius>1</radius></sphere></geometry>
</collision>
</link>
</model>
</sdf>"""
publisher = self.create_publisher(EntityFactory, '/world/my_world/create', 1)
msg = EntityFactory()
while rclpy.ok():
msg.data = 'Hello World: ' # How do I construct a valid message?
msg.model = my_sdf
publisher.publish(msg)
This is the message definition: https://github.com/gazebosim/gz-msgs/...
Thanks in advance for your help.