def merge_objects(self, objects: List[Shape]) -> Shape: """Merges several shapes into a compound shape (or simple shape). :param objects: The list of shapes to group. :return: A single merged shape. """ handles = [o.get_handle() for o in objects] # FIXME: sim.simGroupShapes(merge=True) won't return correct handle, # so we use name to find correct handle of the merged shape. name = objects[-1].get_name() sim.simGroupShapes(handles, merge=True) return Shape(name)
def import_mesh(filename: str, scaling_factor=1.0, keep_identical_vertices=False, ignore_up_vector=False) -> 'Shape': """Imports a mesh from a file. :param filename: The location of the file to import. :param scaling_factor: The scaling factor to apply to the imported vertices :param keep_identical_vertices: Keep identical vertices. :param ignore_up_vector: Ignore up-vector coded in file. :return: The grouped Shape object. """ if not os.path.isfile(filename): raise ValueError('Filename does not exist: ' + filename) options = 0 if keep_identical_vertices: options |= 1 if ignore_up_vector: options |= 128 # fileformat is 0 as it is automatically detected. # identicalVerticeTolerance has no effect. Setting to zero. verticies, indices, names = sim.simImportMesh( 0, filename, options, 0, scaling_factor) mesh_objects = [] for v, i, n in zip(verticies, indices, names): mesh_ob = Shape.create_mesh(v, i) mesh_objects.append(mesh_ob) grouped = mesh_objects[0] if len(mesh_objects) > 1: handles = [o.get_handle() for o in mesh_objects] handle = sim.simGroupShapes(handles) grouped = Shape(handle) return grouped
def randomize_object(self): handle = self.cube.get_handle() sim.simRemoveObject(handle) sizes = [max(random() * 0.1, 0.02), 0.05] objects = list() position = [0, 0, 0] mass = 0.1 # Create cube with random size s = sizes[0] objects.append( Shape.create(type=PrimitiveShape.CUBOID, size=[s, s, s], position=position, mass=mass, color=[random() for _ in range(3)])) index = sample(range(len(position) - 1), 1)[0] sign = sample([1, -1], 1)[0] position[index] += sum(sizes) * 0.5 * sign s = sizes[-1] # Create cube with fix size objects.append( Shape.create(type=PrimitiveShape.CUBOID, size=[s, s, s], position=position, mass=mass, color=[random() for _ in range(3)])) handles = [o.get_handle() for o in objects] handle = sim.simGroupShapes(handles) self.cube = Shape(handle) self.graspable_objects = [ self.cube, ]
def group(shapes: List['Shape']) -> 'Shape': """Groups a set of shapes into a compound shape. :return: A compound shape. """ handles = [shape.get_handle() for shape in shapes] if len(handles) == 1: return shapes[0] return Shape(sim.simGroupShapes(handles))
def group_objects(self, objects: List[Shape]) -> Shape: """Groups several shapes into a compound shape (or simple shape). :param objects: The list of shapes to group. :return: A single grouped shape. """ handles = [o.get_handle() for o in objects] handle = sim.simGroupShapes(handles) return Shape(handle)