# Spawn robot model using rospy in Gazebo

I am trying to spawn my robot model using rospy in my python interface to Gazebo. In the launch file, I spawn the robot using:

<node
name="urdf_spawner"
pkg="gazebo_ros"
type="spawn_model"
respawn="false"
output="screen"
ns="$$(arg robot_name)" args="-urdf -model$$(arg robot_model) -param robot_description -z 1.015"/>


What would be the python way of doing the same? I'd be happy to share more details if needed. Thanks in advance!

I haven't tried it myself but found something similar. Give it a try and let me know as well if it works for you. Good luck!

This is the code that they used.

import roslaunch

package = 'rqt_gui'
executable = 'rqt_gui'
node = roslaunch.core.Node(package, executable)

launch = roslaunch.scriptapi.ROSLaunch()
launch.start()

process = launch.launch(node)
print(process.is_alive())
process.stop()


In this, they used rqt_gui but not gazebo. I have a feeling that you need to use OS module to run gazebo using python but not sure how.

• Thanks for the reply! I ended up launching the ROS node using a launch file only, and defined a function in Python which simply sends the command to the terminal to launch this file (providing the path and launch file name as the arguments) - hence being able to initialize the node whenever needed in my script! Aug 2 at 15:34

For resetting the robot to the initial pose - I was able to fix this by using

import rospy
from gazebo_msgs.srv import SetModelConfiguration

set_model = rospy.ServiceProxy("/gazebo/set_model_configuration", SetModelConfiguration)

joint_names = ['waist', 'shoulder', 'elbow', 'wrist_angle', 'left_finger', 'right_finger'] #example
joint_positions = [0.0, 0.0, 0.0, 0.0, 0.0195, -0.0195]

set_model.call(model_name=robot_name,
urdf_param_name=robot_description,
joint_names=joint_names,
joint_positions=joint_positions)


Note, it is important to pause the Gazebo physics before calling set_model and unpause once done.

When launching the robot for the first time in the Gazebo world, I use the gazebo_ros package to spawn the robot using a node of type spawn_model and pass the desired spawn coordinates.