|
8 | 8 | from compas.geometry import Scale |
9 | 9 | from compas.geometry import Transformation |
10 | 10 | from compas.robots import Geometry |
| 11 | +from compas.robots.model.link import Item |
11 | 12 |
|
12 | 13 |
|
13 | 14 | __all__ = [ |
@@ -75,6 +76,7 @@ def __init__(self, model): |
75 | 76 | self.create() |
76 | 77 | self.scale_factor = 1. |
77 | 78 | self.attached_tool_model = None |
| 79 | + self.attached_items = {} |
78 | 80 |
|
79 | 81 | def attach_tool_model(self, tool_model): |
80 | 82 | """Attach a tool to the robot artist. |
@@ -114,6 +116,61 @@ def detach_tool_model(self): |
114 | 116 | """ |
115 | 117 | self.attached_tool_model = None |
116 | 118 |
|
| 119 | + def attach_mesh(self, mesh, name, link=None, frame=None): |
| 120 | + """Rigidly attaches a compas mesh to a given link. |
| 121 | +
|
| 122 | + Parameters |
| 123 | + ---------- |
| 124 | + mesh : :class:`compas.datastructures.Mesh` |
| 125 | + The mesh to attach to the robot model. |
| 126 | + name : :obj:`str` |
| 127 | + The identifier of the mesh. |
| 128 | + link : :class:`compas.robots.Link` |
| 129 | + The link within the robot model or tool model to attach the mesh to. Optional. |
| 130 | + Defaults to the model's end effector link. |
| 131 | + frame : :class:`compas.geometry.Frame` |
| 132 | + The frame of the mesh. Defaults to :meth:`compas.geometry.Frame.worldXY`. |
| 133 | +
|
| 134 | + Returns |
| 135 | + ------- |
| 136 | + ``None`` |
| 137 | + """ |
| 138 | + if not link: |
| 139 | + link = self.model.get_end_effector_link() |
| 140 | + transformation = Transformation.from_frame(frame) if frame else Transformation() |
| 141 | + |
| 142 | + sample_geometry = None |
| 143 | + |
| 144 | + while sample_geometry is None: |
| 145 | + sample_geometry = link.collision[0] if link.collision else link.visual[0] if link.visual else None |
| 146 | + link = self.model.get_link_by_name(link.parent_joint.parent.link) |
| 147 | + |
| 148 | + native_mesh = self.create_geometry(mesh) |
| 149 | + init_transformation = transformation * sample_geometry.init_transformation |
| 150 | + self.transform(native_mesh, sample_geometry.current_transformation * init_transformation) |
| 151 | + |
| 152 | + item = Item() |
| 153 | + item.native_geometry = [native_mesh] |
| 154 | + item.init_transformation = init_transformation |
| 155 | + item.current_transformation = sample_geometry.current_transformation |
| 156 | + |
| 157 | + self.attached_items.setdefault(link.name, {})[name] = item |
| 158 | + |
| 159 | + def detach_mesh(self, name): |
| 160 | + """Removes attached collision meshes with a given name. |
| 161 | +
|
| 162 | + Parameters |
| 163 | + ---------- |
| 164 | + name : :obj:`str` |
| 165 | + The identifier of the mesh. |
| 166 | +
|
| 167 | + Returns |
| 168 | + ------- |
| 169 | + ``None`` |
| 170 | + """ |
| 171 | + for _, items in self.attached_items: |
| 172 | + items.pop(name, None) |
| 173 | + |
117 | 174 | def create(self, link=None, context=None): |
118 | 175 | """Recursive function that triggers the drawing of the robot model's geometry. |
119 | 176 |
|
@@ -258,6 +315,8 @@ def _transform_link_geometry(self, link, transformation, collision=True): |
258 | 315 | # some links have only collision geometry, not visual. These meshes have not been loaded. |
259 | 316 | if item.native_geometry: |
260 | 317 | self._apply_transformation_on_transformed_link(item, transformation) |
| 318 | + for item in self.attached_items.get(link.name, {}).values(): |
| 319 | + self._apply_transformation_on_transformed_link(item, transformation) |
261 | 320 |
|
262 | 321 | def update_tool(self, joint_state=None, visual=True, collision=True, transformation=None): |
263 | 322 | """Triggers the update of the robot geometry of the tool. |
@@ -302,6 +361,13 @@ def draw_collision(self): |
302 | 361 | for native_geometry in self._iter_geometry(self.attached_tool_model, 'collision'): |
303 | 362 | yield native_geometry |
304 | 363 |
|
| 364 | + def draw_attached_meshes(self): |
| 365 | + """Draws all meshes attached to the robot model.""" |
| 366 | + for items in self.attached_items.values(): |
| 367 | + for item in items.values(): |
| 368 | + for native_mesh in item.native_geometry: |
| 369 | + yield native_mesh |
| 370 | + |
305 | 371 | @staticmethod |
306 | 372 | def _iter_geometry(model, geometry_type): |
307 | 373 | for link in model.iter_links(): |
|
0 commit comments