Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Blueprint-based spawn_actor function for CarlaDataProvider #1071

Merged
merged 6 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading