def spawn_vehicles( world: carla.World, # pylint: disable=no-member num_vehicles: int, ) -> Sequence[carla.Vehicle]: # pylint: disable=no-member """Spawns `vehicles` randomly in spawn points. Args: world: The world object associated with the simulation. num_vehicles: The number of vehicles to spawn. Returns: The list of vehicles actors. """ # Blueprints library. bl = world.get_blueprint_library() # List of spawn points. spawn_points = world.get_map().get_spawn_points() # Output container actors = list() for _ in range(num_vehicles): # Fetch random blueprint. vehicle_bp = random.choice(bl.filter("vehicle.*")) # Attempt to spawn vehicle in random location. actor = world.try_spawn_actor(vehicle_bp, random.choice(spawn_points)) if actor is not None: # Enable autopilot. actor.set_autopilot(True) # Append actor to the list. actors.append(actor) logging.debug("Spawned {} other vehicles".format(len(actors))) return actors
def spawn_pedestrians( world: carla.World, # pylint: disable=no-member num_pedestrians: int, speeds: Sequence[float] = (1.0, 1.5, 2.0), ) -> Sequence[carla.Vehicle]: # pylint: disable=no-member """Spawns `pedestrians` in random locations. Args: world: The world object associated with the simulation. num_pedestrians: The number of pedestrians to spawn. speeds: The valid set of speeds for the pedestrians. Returns: The list of pedestrians actors. """ # Blueprints library. bl = world.get_blueprint_library() # Output container actors = list() for n in range(num_pedestrians): # Fetch random blueprint. pedestrian_bp = random.choice(bl.filter("walker.pedestrian.*")) # Make pedestrian invicible. pedestrian_bp.set_attribute("is_invincible", "true") while len(actors) != n: # Get random location. spawn_point = carla.Transform() # pylint: disable=no-member spawn_point.location = world.get_random_location_from_navigation() if spawn_point.location is None: continue # Attempt to spawn vehicle in random location. actor = world.try_spawn_actor(pedestrian_bp, spawn_point) player_control = carla.WalkerControl() player_control.speed = 3 pedestrian_heading = 90 player_rotation = carla.Rotation(0, pedestrian_heading, 0) player_control.direction = player_rotation.get_forward_vector() if actor is not None: actor.apply_control(player_control) actors.append(actor) logging.debug("Spawned {} pedestrians".format(len(actors))) return actors
def spawn_actor(world: carla.World, blueprint: carla.ActorBlueprint, spawn_point: carla.Transform, attach_to: carla.Actor = None, attachment_type=carla.AttachmentType.Rigid) -> carla.Actor: """Tries to spawn an actor in a CARLA simulator. :param world: a carla.World instance. :param blueprint: specifies which actor has to be spawned. :param spawn_point: where to spawn the actor. A transform specifies the location and rotation. :param attach_to: whether the spawned actor has to be attached (linked) to another one. :param attachment_type: the kind of the attachment. Can be 'Rigid' or 'SpringArm'. :return: a carla.Actor instance. """ actor = world.try_spawn_actor(blueprint, spawn_point, attach_to, attachment_type) if actor is None: raise ValueError( f'Cannot spawn actor. Try changing the spawn_point ({spawn_point.location}) to something else.' ) return actor