diff --git a/srunner/scenariomanager/scenarioatomics/atomic_behaviors.py b/srunner/scenariomanager/scenarioatomics/atomic_behaviors.py index 56b03de0a..173d1672e 100644 --- a/srunner/scenariomanager/scenarioatomics/atomic_behaviors.py +++ b/srunner/scenariomanager/scenarioatomics/atomic_behaviors.py @@ -3471,7 +3471,7 @@ def update(self): sensor.stop() sensor.destroy() self._collision_sensor_list.remove(sensor) - actor.destroy() + CarlaDataProvider.remove_actor_by_id(actor.id) self._actor_list.remove(actor) # Spawn new actors if needed @@ -3519,7 +3519,7 @@ def terminate(self, new_status): actor.set_target_velocity(carla.Vector3D(0,0,0)) actor.set_target_angular_velocity(carla.Vector3D(0,0,0)) try: - actor.destroy() + CarlaDataProvider.remove_actor_by_id(actor.id) except RuntimeError: pass # Actor was already destroyed @@ -3635,7 +3635,7 @@ def update(self): continue sink_distance = self._sink_location.distance(location) if sink_distance < self._sink_dist: - actor.destroy() + CarlaDataProvider.remove_actor_by_id(actor.id) self._actor_list.remove(actor_data) else: actor.apply_control(controller.run_step()) @@ -3668,7 +3668,7 @@ def terminate(self, new_status): actor.set_target_velocity(carla.Vector3D(0,0,0)) actor.set_target_angular_velocity(carla.Vector3D(0,0,0)) try: - actor.destroy() + CarlaDataProvider.remove_actor_by_id(actor.id) except RuntimeError: pass # Actor was already destroyed @@ -3750,7 +3750,7 @@ def update(self): continue sink_distance = self._sink_location.distance(location) if sink_distance < self._sink_dist: - actor.destroy() + CarlaDataProvider.remove_actor_by_id(actor.id) self._actor_list.remove(actor_data) else: actor.apply_control(controller.run_step()) @@ -3783,7 +3783,7 @@ def terminate(self, new_status): actor.set_target_velocity(carla.Vector3D(0,0,0)) actor.set_target_angular_velocity(carla.Vector3D(0,0,0)) try: - actor.destroy() + CarlaDataProvider.remove_actor_by_id(actor.id) except RuntimeError: pass # Actor was already destroyed @@ -3889,7 +3889,7 @@ def update(self): continue sink_distance = self._sink_location.distance(location) if sink_distance < self._sink_dist: - actor.destroy() + CarlaDataProvider.remove_actor_by_id(actor.id) self._actor_data.remove(actor_data) else: actor.apply_control(controller.run_step()) @@ -3926,7 +3926,7 @@ def terminate(self, new_status): actor.set_target_velocity(carla.Vector3D(0,0,0)) actor.set_target_angular_velocity(carla.Vector3D(0,0,0)) try: - actor.destroy() + CarlaDataProvider.remove_actor_by_id(actor.id) except RuntimeError: pass # Actor was already destroyed @@ -4725,7 +4725,7 @@ def update(self): def _destroy_walker(self, walker, controller): controller.stop() controller.destroy() - walker.destroy() + CarlaDataProvider.remove_actor_by_id(walker.id) def terminate(self, new_status): """ @@ -4805,7 +4805,7 @@ def _destroy_walker(self, walker, controller): controller.stop() controller.destroy() if walker: - walker.destroy() + CarlaDataProvider.remove_actor_by_id(walker.id) def terminate(self, new_status): """ diff --git a/srunner/scenarios/background_activity.py b/srunner/scenarios/background_activity.py index fdacf5663..5c9d39f37 100644 --- a/srunner/scenarios/background_activity.py +++ b/srunner/scenarios/background_activity.py @@ -2549,7 +2549,7 @@ def _destroy_actor(self, actor): self._remove_actor_info(actor) try: actor.set_autopilot(False, self._tm_port) - actor.destroy() + CarlaDataProvider.remove_actor_by_id(actor.id) except RuntimeError: pass diff --git a/srunner/scenarios/basic_scenario.py b/srunner/scenarios/basic_scenario.py index 39c3adf10..3d44d2e65 100644 --- a/srunner/scenarios/basic_scenario.py +++ b/srunner/scenarios/basic_scenario.py @@ -171,6 +171,14 @@ def _initialize_actors(self, config): for new_actor in new_actors: self.other_actors.append(new_actor) + + def _destroy_other_actors(self): + """ + Remove all actors upon deletion + """ + for actor in self.other_actors: + if CarlaDataProvider.remove_actor_by_id(actor.id) is None: + actor.destroy() def _setup_scenario_trigger(self, config): """ diff --git a/srunner/scenarios/cut_in_with_static_vehicle.py b/srunner/scenarios/cut_in_with_static_vehicle.py index 01d345bfd..b7575783f 100644 --- a/srunner/scenarios/cut_in_with_static_vehicle.py +++ b/srunner/scenarios/cut_in_with_static_vehicle.py @@ -95,8 +95,7 @@ def _initialize_actors(self, config): # Move to the side side_wp = blocker_wp.get_left_lane() if self._direction == 'left' else blocker_wp.get_right_lane() if not side_wp: - for actor in self.other_actors: - actor.destroy() + self._destroy_other_actors() raise ValueError("Couldn't find a proper position for the cut in vehicle") if i == 1: @@ -106,8 +105,7 @@ def _initialize_actors(self, config): blocker_actor = CarlaDataProvider.request_new_actor( 'vehicle.*', side_wp.transform, 'scenario', attribute_filter=self._attributes) if not blocker_actor: - for actor in self.other_actors: - actor.destroy() + self._destroy_other_actors() raise ValueError("Couldn't spawn an actor") blocker_actor.apply_control(carla.VehicleControl(hand_brake=True)) @@ -119,8 +117,7 @@ def _initialize_actors(self, config): # Move to the front next_wps = blocker_wp.next(self._vehicle_gap) if not next_wps: - for actor in self.other_actors: - actor.destroy() + self._destroy_other_actors() raise ValueError("Couldn't find a proper position for the cut in vehicle") blocker_wp = next_wps[0] @@ -132,8 +129,7 @@ def _initialize_actors(self, config): while dist < self._adversary_end_distance: next_wps = next_wp.next(step) if not next_wps: - for actor in self.other_actors: - actor.destroy() + self._destroy_other_actors() raise ValueError("Couldn't find a proper position for the cut in vehicle") next_wp = next_wps[0] self._plan.append([next_wp, RoadOption.STRAIGHT]) @@ -143,15 +139,13 @@ def _initialize_actors(self, config): # Spawn the cut in vehicle side_wp = blocker_wp.get_left_lane() if self._direction == 'left' else blocker_wp.get_right_lane() if not side_wp: - for actor in self.other_actors: - actor.destroy() + self._destroy_other_actors() raise ValueError("Couldn't find a proper position for the cut in vehicle") self._adversary_actor = CarlaDataProvider.request_new_actor( 'vehicle.*', side_wp.transform, 'scenario', attribute_filter=self._attributes) if not self._adversary_actor: - for actor in self.other_actors: - actor.destroy() + self._destroy_other_actors() raise ValueError("Couldn't spawn an actor") self._adversary_actor.set_simulate_physics(False) @@ -165,8 +159,7 @@ def _initialize_actors(self, config): # Move to the front next_wps = blocker_wp.next(self._vehicle_gap) if not next_wps: - for actor in self.other_actors: - actor.destroy() + self._destroy_other_actors() raise ValueError("Couldn't find a proper position for the cut in vehicle") blocker_wp = next_wps[0] @@ -175,16 +168,14 @@ def _initialize_actors(self, config): # Move to the side side_wp = blocker_wp.get_left_lane() if self._direction == 'left' else blocker_wp.get_right_lane() if not side_wp: - for actor in self.other_actors: - actor.destroy() + self._destroy_other_actors() raise ValueError("Couldn't find a proper position for the cut in vehicle") # Spawn the actor blocker_actor = CarlaDataProvider.request_new_actor( 'vehicle.*', side_wp.transform, 'scenario', attribute_filter=self._attributes) if not blocker_actor: - for actor in self.other_actors: - actor.destroy() + self._destroy_other_actors() raise ValueError("Couldn't spawn an actor") blocker_actor.apply_control(carla.VehicleControl(hand_brake=True)) @@ -196,8 +187,7 @@ def _initialize_actors(self, config): # Move to the front next_wps = blocker_wp.next(self._vehicle_gap) if not next_wps: - for actor in self.other_actors: - actor.destroy() + self._destroy_other_actors() raise ValueError("Couldn't find a proper position for the cut in vehicle") blocker_wp = next_wps[0] diff --git a/srunner/scenarios/object_crash_intersection.py b/srunner/scenarios/object_crash_intersection.py index 8c1d65e00..41b694dc2 100644 --- a/srunner/scenarios/object_crash_intersection.py +++ b/srunner/scenarios/object_crash_intersection.py @@ -417,7 +417,7 @@ def __del__(self): def _replace_walker(self, adversary): """As the adversary is probably, replace it with another one""" type_id = adversary.type_id - adversary.destroy() + CarlaDataProvider.remove_actor_by_id(adversary.id) spawn_transform = self.ego_vehicles[0].get_transform() spawn_transform.location.z -= 50 adversary = CarlaDataProvider.request_new_actor(type_id, spawn_transform) diff --git a/srunner/scenarios/object_crash_vehicle.py b/srunner/scenarios/object_crash_vehicle.py index 485705ade..096039bb5 100644 --- a/srunner/scenarios/object_crash_vehicle.py +++ b/srunner/scenarios/object_crash_vehicle.py @@ -266,7 +266,7 @@ def _initialize_actors(self, config): self._adversary_transform = self._get_sidewalk_transform(walker_wp, offset) adversary = CarlaDataProvider.request_new_actor('walker.*', self._adversary_transform) if adversary is None: - blocker.destroy() + CarlaDataProvider.remove_actor_by_id(blocker.id) self._number_of_attempts -= 1 move_dist = self._retry_dist print("Failed to spawn an adversary") @@ -350,7 +350,7 @@ def __del__(self): def _replace_walker(self, adversary): """As the adversary is probably, replace it with another one""" type_id = adversary.type_id - adversary.destroy() + CarlaDataProvider.remove_actor_by_id(adversary.id) spawn_transform = self.ego_vehicles[0].get_transform() spawn_transform.location.z -= 50 adversary = CarlaDataProvider.request_new_actor(type_id, spawn_transform) @@ -557,7 +557,7 @@ def __del__(self): def _replace_walker(self, walker): """As the adversary is probably, replace it with another one""" type_id = walker.type_id - walker.destroy() + CarlaDataProvider.remove_actor_by_id(walker.id) spawn_transform = self.ego_vehicles[0].get_transform() spawn_transform.location.z -= 50 walker = CarlaDataProvider.request_new_actor(type_id, spawn_transform) diff --git a/srunner/scenarios/parking_exit.py b/srunner/scenarios/parking_exit.py index acbb69643..da5f6dad2 100644 --- a/srunner/scenarios/parking_exit.py +++ b/srunner/scenarios/parking_exit.py @@ -140,7 +140,7 @@ def _initialize_actors(self, config): actor_behind = CarlaDataProvider.request_new_actor( 'vehicle.*', behind_points[0].transform, rolename='scenario no lights', attribute_filter=self._bp_attributes) if actor_behind is None: - actor_front.destroy() + CarlaDataProvider.remove_actor_by_id(actor_front.id) raise ValueError("Couldn't spawn the vehicle behind the parking point") actor_behind.apply_control(carla.VehicleControl(hand_brake=True)) self.other_actors.append(actor_behind) diff --git a/srunner/scenarios/pedestrian_crossing.py b/srunner/scenarios/pedestrian_crossing.py index 6cb70c2de..5a01a9f7f 100644 --- a/srunner/scenarios/pedestrian_crossing.py +++ b/srunner/scenarios/pedestrian_crossing.py @@ -130,8 +130,7 @@ def _initialize_actors(self, config): spawn_transform = self._get_walker_transform(start_wp, walker_data) walker = CarlaDataProvider.request_new_actor('walker.*', spawn_transform) if walker is None: - for walker in self.other_actors: - walker.destroy() + self._destroy_other_actors() raise ValueError("Failed to spawn an adversary") walker.set_location(spawn_transform.location + carla.Location(z=-200)) @@ -220,7 +219,7 @@ def __del__(self): def _replace_walker(self, walker): """As the adversary is probably, replace it with another one""" type_id = walker.type_id - walker.destroy() + CarlaDataProvider.remove_actor_by_id(walker.id) spawn_transform = self.ego_vehicles[0].get_transform() spawn_transform.location.z -= 50 walker = CarlaDataProvider.request_new_actor(type_id, spawn_transform)