Robowflex  v0.1
Making MoveIt Easy
scene.py
Go to the documentation of this file.
1 ## @package scene
2 # Functions for loading and animating scenes in Blender.
3 
4 import bpy
5 import robowflex_visualization as rv
6 
7 
8 ## @brief Container for MoveIt planning scenes in Blender.
9 # This class loads mesh and primitive resources described in a planning scene
10 # and stores them in a Blender collection.
11 class Scene:
12 
13  ## @brief Constructor. Loads scene and creates a collection.
14  #
15  # @param name Name of Blender collection to put scene geometry in.
16  # @param scene_file Scene file YAML package resource URI to load.
17  #
18  def __init__(self, name, scene_file):
19  self.namename = name
20  self.shapesshapes = {}
21 
22  self.collectioncollection = rv.utils.get_collection(name)
23 
24  if not self.collectioncollection:
25  # Create scene
26  self.collectioncollection = rv.utils.make_collection(name)
27  self.load_sceneload_scene(scene_file)
28 
29  else:
30  # Populate based on existing scene
31  for item in self.collectioncollection.objects:
32  self.shapesshapes[item.name] = item
33 
34  ## @brief Loads a YAML moveit_msgs::PlanningScene into Blender.
35  #
36  # @param scene_file YAML package URI to load.
37  #
38  def load_scene(self, scene_file):
39  self.filepathfilepath = resolved = rv.utils.resolve_path(scene_file)
40  self.yamlyaml = rv.utils.read_YAML_data(scene_file)
41 
42  if "world" in self.yamlyaml and self.yamlyaml["world"]["collision_objects"]:
43  for co in self.yamlyaml["world"]["collision_objects"]:
44  self.add_collision_objectadd_collision_object(co)
45 
46  if "robot_state" in self.yamlyaml:
47  if "attached_collision_objects" in self.yamlyaml["robot_state"].keys():
48  for cao in self.yamlyaml["robot_state"]["attached_collision_objects"]:
49  self.add_collision_objectadd_collision_object(cao["object"])
50 
51  ## @brief Adds a collision object (moveit_msgs::CollisionObject) to the scene.
52  #
53  # @param co Collision object.
54  #
55  def add_collision_object(self, co):
56  if 'primitives' in co:
57  shapes = co['primitives']
58  poses = co['primitive_poses']
59  elif 'meshes' in co:
60  shapes = co['meshes']
61  poses = co['mesh_poses']
62 
63  for shape, pose in zip(shapes, poses):
64  self.add_shapeadd_shape(co["id"], shape, pose)
65 
66  ## @brief Adds a shape (either shape_msgs::SolidPrimitive or shape_msgs::Mesh) to the scene.
67  #
68  # @param name Name of the object.
69  # @param shape Shape to add.
70  # @param pose Pose relative to world of the object.
71  #
72  def add_shape(self, name, shape, pose):
73  if not 'color' in shape:
74  shape['color'] = (0.0, 0.9, 0.2) # MoveIt Green.
75 
76  obj = rv.primitives.add_shape(shape)
77  obj.name = name
78 
79  self.shapesshapes[name] = obj
80 
81  rv.utils.deselect_all()
82  rv.utils.set_pose(obj, pose)
83  rv.utils.select_all_children(obj)
84  rv.utils.move_selected_to_collection(self.namename)
85  rv.utils.deselect_all()
86 
87  ## @brief Retrieve a named object in the scene.
88  #
89  # @param name Name of object to retrieve.
90  def get_object(self, name):
91  return self.shapesshapes[name]
Container for MoveIt planning scenes in Blender.
Definition: scene.py:11
def add_collision_object(self, co)
Adds a collision object (moveit_msgs::CollisionObject) to the scene.
Definition: scene.py:55
def load_scene(self, scene_file)
Loads a YAML moveit_msgs::PlanningScene into Blender.
Definition: scene.py:38
def __init__(self, name, scene_file)
Constructor.
Definition: scene.py:18
def get_object(self, name)
Retrieve a named object in the scene.
Definition: scene.py:90
def add_shape(self, name, shape, pose)
Adds a shape (either shape_msgs::SolidPrimitive or shape_msgs::Mesh) to the scene.
Definition: scene.py:72