Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Add urdf to world using a python script with the /world/my_world/create service with the EntityFactory message


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++:

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\">'\
'<collision name=\"visual\">'\
'</sdf>" '\
'pose: {position: {z: 10}} '\
'name: "new_name" '\
'allow_renaming: true'

My plan:

  1. Install ignition messages (But how?)
  2. Setup ign bridge to forward message /world/my_world/create from ign to ROS2 (in launch file)

    ros_ign_bridge (clock -> ROS 2)

        parameters=[{"use_sim_time": use_sim_time}],
  3. 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'>
                <collision name='collision'>

            publisher = self.create_publisher(EntityFactory, '/world/my_world/create', 1)

            msg = EntityFactory()
            while rclpy.ok():
       = 'Hello World:  ' # How do I construct a valid message?
                msg.model = my_sdf

This is the message definition:

Thanks in advance for your help.