carla/PythonAPI/util/extract_spawn_points.py

94 lines
2.7 KiB
Python
Raw Normal View History

2021-10-11 21:56:37 +08:00
""" CARLA map spawn points extractor """
from __future__ import print_function
import argparse
import logging
2021-10-14 00:28:06 +08:00
import glob
import os
import sys
try:
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
2021-10-11 21:56:37 +08:00
import carla
def extract(args):
try:
client = carla.Client(args.host, args.port, worker_threads=1)
client.set_timeout(2.0)
world = client.get_world()
try:
_map = world.get_map()
except RuntimeError as error:
logging.info('RuntimeError: %s', error)
sys.exit(1)
if not _map.get_spawn_points():
logging.info('There are no spawn points available in your map/town.')
logging.info('Please add some Vehicle Spawn Point to your UE4 scene.')
sys.exit(1)
spawn_points = _map.get_spawn_points()
2021-10-16 01:26:54 +08:00
with open(args.output_dir + "/spawn_points.csv", "w", encoding='utf8') as file:
2021-10-11 21:56:37 +08:00
index = 0
for index, spawn_point in enumerate(spawn_points):
2021-10-16 01:26:54 +08:00
file.write(f'{index},{spawn_point.location.x},{spawn_point.location.y},{spawn_point.location.z}\n')
2021-10-11 21:56:37 +08:00
finally:
world = None
# ==============================================================================
# -- main() --------------------------------------------------------------------
# ==============================================================================
def main():
argparser = argparse.ArgumentParser(
description='CARLA map spawn points extractor')
argparser.add_argument(
'--host',
metavar='H',
default='127.0.0.1',
help='IP of the host server (default: 127.0.0.1)')
argparser.add_argument(
'-p', '--port',
metavar='P',
default=2000,
type=int,
help='TCP port to listen to (default: 2000)')
2021-10-14 00:28:06 +08:00
argparser.add_argument(
'-o', '--output-dir',
required=True,
2021-10-14 00:28:06 +08:00
help='Output directory path for extraction result')
2021-10-11 21:56:37 +08:00
args = argparser.parse_args()
2021-10-14 00:28:06 +08:00
if args.output_dir is None or not os.path.exists(args.output_dir):
print('output directory not found.')
2021-10-11 21:56:37 +08:00
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
logging.info('listening to server %s:%s', args.host, args.port)
print(__doc__)
try:
extract(args)
2021-10-14 00:28:06 +08:00
except:
print('\nAn error has occurred in extraction.')
2021-10-11 21:56:37 +08:00
if __name__ == '__main__':
2021-10-14 00:28:06 +08:00
try:
main()
except KeyboardInterrupt:
print('\nCancelled by user. Bye!')
except RuntimeError as e:
print(e)