Skip to content

Commit a6a9a31

Browse files
Daraanglopezdiest
andauthored
[Feature] Blueprint-based spawn_actor function for CarlaDataProvider (#1071)
* blueprint-based spawn_actor function for CDP * Auto-Option to decide tracking * Updated changelog * spawned actors are tracked if they are Vehicle | Walker * Updated spawn_actor description --------- Co-authored-by: glopezdiest <58212725+glopezdiest@users.noreply.github.com>
1 parent 9d54467 commit a6a9a31

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Docs/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
* [CARLA ScenarioRunner 0.9.5](#carla-scenariorunner-095)
1313
* [CARLA ScenarioRunner 0.9.2](#carla-scenariorunner-092)
1414

15+
## Upcoming
16+
* Improvements to the CarlaDataProvider:
17+
- Added `spawn_actor` for a blueprint based actor creation similar to `World.spawn_actor`
18+
1519
## CARLA ScenarioRunner 0.9.15
1620
### :rocket: New Features
1721
* Add waypoint reached threshold so that the precision of the actor reaching to waypoints can be adjusted based on object types.

srunner/scenariomanager/carla_data_provider.py

+53
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,59 @@ def handle_actor_batch(batch, tick=True):
681681
actors = list(CarlaDataProvider._world.get_actors(actor_ids))
682682
return actors
683683

684+
@staticmethod
685+
def spawn_actor(bp, spawn_point, must_spawn=False, track_physics=None, attach_to=None, attachment_type=carla.AttachmentType.Rigid):
686+
# type: (carla.ActorBlueprint, carla.Waypoint | carla.Transform, bool, bool | None, carla.Actor | None, carla.AttachmentType) -> carla.Actor | None
687+
"""
688+
The method will spawn and return an actor.
689+
The actor will need an available blueprint to be created.
690+
It can also be attached to a parent with a certain attachment type.
691+
692+
Args:
693+
bp (carla.ActorBlueprint): The blueprint of the actor to spawn.
694+
spawn_point (carla.Transform): The spawn point of the actor.
695+
must_spawn (bool, optional):
696+
If True, the actor will be spawned or an exception will be raised.
697+
If False, the function returns None if the actor could not be spawned.
698+
Defaults to False.
699+
track_physics (bool | None, optional):
700+
If True, `get_location`, `get_transform` and `get_velocity`
701+
can be used for this actor.
702+
If None, the actor will be tracked if it is a Vehicle or Walker.
703+
Defaults to None.
704+
attach_to (carla.Actor | None, optional):
705+
The parent object that the spawned actor will follow around.
706+
Defaults to None.
707+
attachment_type (carla.AttachmentType, optional):
708+
Determines how fixed and rigorous should be the changes in position
709+
according to its parent object.
710+
Defaults to carla.AttachmentType.Rigid.
711+
712+
Returns:
713+
carla.Actor | None: The spawned actor if successful, None otherwise.
714+
715+
Raises:
716+
RuntimeError: if `must_spawn` is True and the actor could not be spawned.
717+
"""
718+
if isinstance(spawn_point, carla.Waypoint):
719+
spawn_point = spawn_point.transform
720+
world = CarlaDataProvider.get_world()
721+
if must_spawn:
722+
actor = world.spawn_actor(bp, spawn_point, attach_to, attachment_type)
723+
else:
724+
actor = world.try_spawn_actor(bp, spawn_point, attach_to, attachment_type)
725+
if actor is None:
726+
return None
727+
# Register for cleanup
728+
CarlaDataProvider._carla_actor_pool[actor.id] = actor
729+
if track_physics is None:
730+
# Decide
731+
track_physics = isinstance(actor, (carla.Vehicle, carla.Walker))
732+
if track_physics:
733+
# Register for physics
734+
CarlaDataProvider.register_actor(actor, spawn_point)
735+
return actor
736+
684737
@staticmethod
685738
def request_new_actor(model, spawn_point, rolename='scenario', autopilot=False,
686739
random_location=False, color=None, actor_category="car",

0 commit comments

Comments
 (0)