@@ -681,6 +681,59 @@ def handle_actor_batch(batch, tick=True):
681
681
actors = list (CarlaDataProvider ._world .get_actors (actor_ids ))
682
682
return actors
683
683
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
+
684
737
@staticmethod
685
738
def request_new_actor (model , spawn_point , rolename = 'scenario' , autopilot = False ,
686
739
random_location = False , color = None , actor_category = "car" ,
0 commit comments