Skip to content

Commit

Permalink
[Feature] Blueprint-based spawn_actor function for CarlaDataProvider (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
Daraan and glopezdiest committed Apr 30, 2024
1 parent 9d54467 commit a6a9a31
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
* [CARLA ScenarioRunner 0.9.5](#carla-scenariorunner-095)
* [CARLA ScenarioRunner 0.9.2](#carla-scenariorunner-092)

## Upcoming
* Improvements to the CarlaDataProvider:
- Added `spawn_actor` for a blueprint based actor creation similar to `World.spawn_actor`

## CARLA ScenarioRunner 0.9.15
### :rocket: New Features
* Add waypoint reached threshold so that the precision of the actor reaching to waypoints can be adjusted based on object types.
Expand Down
53 changes: 53 additions & 0 deletions srunner/scenariomanager/carla_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,59 @@ def handle_actor_batch(batch, tick=True):
actors = list(CarlaDataProvider._world.get_actors(actor_ids))
return actors

@staticmethod
def spawn_actor(bp, spawn_point, must_spawn=False, track_physics=None, attach_to=None, attachment_type=carla.AttachmentType.Rigid):
# type: (carla.ActorBlueprint, carla.Waypoint | carla.Transform, bool, bool | None, carla.Actor | None, carla.AttachmentType) -> carla.Actor | None
"""
The method will spawn and return an actor.
The actor will need an available blueprint to be created.
It can also be attached to a parent with a certain attachment type.
Args:
bp (carla.ActorBlueprint): The blueprint of the actor to spawn.
spawn_point (carla.Transform): The spawn point of the actor.
must_spawn (bool, optional):
If True, the actor will be spawned or an exception will be raised.
If False, the function returns None if the actor could not be spawned.
Defaults to False.
track_physics (bool | None, optional):
If True, `get_location`, `get_transform` and `get_velocity`
can be used for this actor.
If None, the actor will be tracked if it is a Vehicle or Walker.
Defaults to None.
attach_to (carla.Actor | None, optional):
The parent object that the spawned actor will follow around.
Defaults to None.
attachment_type (carla.AttachmentType, optional):
Determines how fixed and rigorous should be the changes in position
according to its parent object.
Defaults to carla.AttachmentType.Rigid.
Returns:
carla.Actor | None: The spawned actor if successful, None otherwise.
Raises:
RuntimeError: if `must_spawn` is True and the actor could not be spawned.
"""
if isinstance(spawn_point, carla.Waypoint):
spawn_point = spawn_point.transform
world = CarlaDataProvider.get_world()
if must_spawn:
actor = world.spawn_actor(bp, spawn_point, attach_to, attachment_type)
else:
actor = world.try_spawn_actor(bp, spawn_point, attach_to, attachment_type)
if actor is None:
return None
# Register for cleanup
CarlaDataProvider._carla_actor_pool[actor.id] = actor
if track_physics is None:
# Decide
track_physics = isinstance(actor, (carla.Vehicle, carla.Walker))
if track_physics:
# Register for physics
CarlaDataProvider.register_actor(actor, spawn_point)
return actor

@staticmethod
def request_new_actor(model, spawn_point, rolename='scenario', autopilot=False,
random_location=False, color=None, actor_category="car",
Expand Down

0 comments on commit a6a9a31

Please sign in to comment.