|
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 |
|
@@ -294,6 +351,8 @@ def _transform_link_geometry(self, link, transformation, collision=True): |
294 | 351 | # some links have only collision geometry, not visual. These meshes have not been loaded. |
295 | 352 | if item.native_geometry: |
296 | 353 | self._apply_transformation_on_transformed_link(item, transformation) |
| 354 | + for item in self.attached_items.get(link.name, {}).values(): |
| 355 | + self._apply_transformation_on_transformed_link(item, transformation) |
297 | 356 |
|
298 | 357 | def update_tool(self, joint_state=None, visual=True, collision=True, transformation=None): |
299 | 358 | """Triggers the update of the robot geometry of the tool. |
@@ -338,6 +397,13 @@ def draw_collision(self): |
338 | 397 | for native_geometry in self._iter_geometry(self.attached_tool_model, 'collision'): |
339 | 398 | yield native_geometry |
340 | 399 |
|
| 400 | + def draw_attached_meshes(self): |
| 401 | + """Draws all meshes attached to the robot model.""" |
| 402 | + for items in self.attached_items.values(): |
| 403 | + for item in items.values(): |
| 404 | + for native_mesh in item.native_geometry: |
| 405 | + yield native_mesh |
| 406 | + |
341 | 407 | @staticmethod |
342 | 408 | def _iter_geometry(model, geometry_type): |
343 | 409 | for link in model.iter_links(): |
|
0 commit comments