|
14 | 14 | 'BaseRobotModelArtist' |
15 | 15 | ] |
16 | 16 |
|
| 17 | +from compas.robots.model.link import Item |
| 18 | + |
17 | 19 |
|
18 | 20 | class AbstractRobotModelArtist(object): |
19 | 21 | def transform(self, geometry, transformation): |
@@ -75,6 +77,7 @@ def __init__(self, model): |
75 | 77 | self.create() |
76 | 78 | self.scale_factor = 1. |
77 | 79 | self.attached_tool_model = None |
| 80 | + self.attached_items = {} |
78 | 81 |
|
79 | 82 | def attach_tool_model(self, tool_model): |
80 | 83 | """Attach a tool to the robot artist. |
@@ -114,6 +117,45 @@ def detach_tool_model(self): |
114 | 117 | """ |
115 | 118 | self.attached_tool_model = None |
116 | 119 |
|
| 120 | + def attach_mesh(self, mesh, link=None): |
| 121 | + """Rigidly attaches a compas mesh to a given link. |
| 122 | +
|
| 123 | + Parameters |
| 124 | + ---------- |
| 125 | + mesh : :class:`compas.datastructures.Mesh` |
| 126 | + The mesh to attach to the robot model. |
| 127 | + link : :class:`compas.robots.Link` |
| 128 | + The link within the robot model or tool model to attach the mesh to. Optional. |
| 129 | + Defaults to the model's end effector link. |
| 130 | +
|
| 131 | + Returns |
| 132 | + ------- |
| 133 | + ``None`` |
| 134 | + """ |
| 135 | + if not link: |
| 136 | + link = self.model.get_end_effector_link() |
| 137 | + |
| 138 | + frame = link.parent_joint.origin.copy() |
| 139 | + initial_transformation = Transformation.from_frame_to_frame(Frame.worldXY(), frame) |
| 140 | + |
| 141 | + sample_geometry = link.collision[0] if link.collision else link.visual[0] if link.visual else None |
| 142 | + |
| 143 | + if hasattr(sample_geometry, 'current_transformation'): |
| 144 | + relative_transformation = sample_geometry.current_transformation |
| 145 | + else: |
| 146 | + relative_transformation = Transformation() |
| 147 | + |
| 148 | + transformation = relative_transformation.concatenated(initial_transformation) |
| 149 | + native_mesh = self.create_geometry(mesh) |
| 150 | + self.transform(native_mesh, transformation) |
| 151 | + |
| 152 | + item = Item() |
| 153 | + item.native_geometry = [native_mesh] |
| 154 | + item.init_transformation = transformation # !!! |
| 155 | + item.current_transformation = Transformation() # !!! |
| 156 | + |
| 157 | + self.attached_items.setdefault(link.name, []).append(item) |
| 158 | + |
117 | 159 | def create(self, link=None, context=None): |
118 | 160 | """Recursive function that triggers the drawing of the robot model's geometry. |
119 | 161 |
|
@@ -258,6 +300,8 @@ def _transform_link_geometry(self, link, transformation, collision=True): |
258 | 300 | # some links have only collision geometry, not visual. These meshes have not been loaded. |
259 | 301 | if item.native_geometry: |
260 | 302 | self._apply_transformation_on_transformed_link(item, transformation) |
| 303 | + for item in self.attached_items.get(link.name, []): |
| 304 | + self._apply_transformation_on_transformed_link(item, transformation) |
261 | 305 |
|
262 | 306 | def update_tool(self, joint_state=None, visual=True, collision=True, transformation=None): |
263 | 307 | """Triggers the update of the robot geometry of the tool. |
@@ -302,6 +346,13 @@ def draw_collision(self): |
302 | 346 | for native_geometry in self._iter_geometry(self.attached_tool_model, 'collision'): |
303 | 347 | yield native_geometry |
304 | 348 |
|
| 349 | + def draw_attached_meshes(self): |
| 350 | + """Draws all meshes attached to the robot model.""" |
| 351 | + for items in self.attached_items.values(): |
| 352 | + for item in items: |
| 353 | + for native_mesh in item.native_geometry: |
| 354 | + yield native_mesh |
| 355 | + |
305 | 356 | @staticmethod |
306 | 357 | def _iter_geometry(model, geometry_type): |
307 | 358 | for link in model.iter_links(): |
|
0 commit comments