def get_shape_viz(self, index): """Retrieves a shape's visual information. :param index: 0-based index of the shape element to retrieve (compound shapes contain more than one shape element) :return: SShapeVizInfo. """ info = sim.simGetShapeViz(shapeHandle=self._handle, index=index) vertices = np.array(info.vertices, dtype=float).reshape(-1, 3) indices = np.array(info.indices, dtype=float).reshape(-1, 3) normals = np.array(info.normals, dtype=float).reshape(-1, 3) colors = np.array(info.colors, dtype=float) texture = np.array(info.texture, dtype=np.uint8).reshape( info.textureRes[1], info.textureRes[0], 4) textureCoords = np.array(info.textureCoords, dtype=float).reshape( -1, 2) res = SShapeVizInfo( vertices=vertices, indices=indices, normals=normals, shading_angle=info.shadingAngle, colors=colors, texture=texture, texture_id=info.textureId, texture_coords=textureCoords, texture_apply_mode=info.textureApplyMode, texture_options=info.textureOptions, ) return res
def get_shape_viz(self, index, world_frame=False): """Retrieves a shape's visual information. :param index: 0-based index of the shape element to retrieve (compound shapes contain more than one shape element) :param world_frame: Whether to represent the mesh in the world frame. :return: SShapeVizInfo. """ info = sim.simGetShapeViz(shapeHandle=self._handle, index=index) if info == 0: return None vertices = np.array(info.vertices, dtype=np.float64).reshape(-1, 3) indices = np.array(info.indices, dtype=np.int64).reshape(-1, 3) normals = np.array(info.normals, dtype=np.float64).reshape(-1, 3) normals_w_ones = np.concatenate( (normals, np.ones_like(normals[:, -1:])), -1) vertex_normals = np.zeros([vertices.shape[0], 4], dtype=np.float64) np.add.at(vertex_normals, indices.reshape(-1), normals_w_ones) vertex_normals = vertex_normals[..., 0:3] / vertex_normals[..., -1:] if world_frame: inv_ext_mat_homo = self.get_matrix() rot_mat = inv_ext_mat_homo[0:3, 0:3] trans = inv_ext_mat_homo[0:3, -1] vertices = np.transpose( np.matmul(rot_mat, np.transpose(vertices, (1, 0))), (1, 0)) + trans vertex_normals = np.transpose( np.matmul(rot_mat, np.transpose(vertex_normals, (1, 0))), (1, 0)) colors = np.array(info.colors, dtype=float) try: # ToDo: work out why this happens, for some reason sometimes the shapes don't match texture = np.array(info.texture, dtype=np.uint8).reshape(info.textureRes[1], info.textureRes[0], 4) textureCoords = np.array(info.textureCoords, dtype=float).reshape(-1, 2) except: texture = np.array([], dtype=np.uint8) textureCoords = np.array([], dtype=float) res = SShapeVizInfo( vertices=vertices, indices=indices, normals=vertex_normals, shading_angle=info.shadingAngle, colors=colors, texture=texture, texture_id=info.textureId, texture_coords=textureCoords, texture_apply_mode=info.textureApplyMode, texture_options=info.textureOptions, ) return res