code: def main(args):
      # Temporal folder to save intermediate files.
      tmpdir = tempfile.mkdtemp()
  
      # ----------------
      # carla simulation
      # ----------------
      carla_simulation = CarlaSimulation(
          args.host, args.port, args.step_length
      )
  
      world = carla_simulation.client.get_world()
      current_map = world.get_map()
  
      xodr_file = os.path.join(tmpdir, current_map.name + ".xodr")
      current_map.save_to_disk(xodr_file)
  
      # ---------------
      # sumo simulation
      # ---------------
      net_file = os.path.join(tmpdir, current_map.name + ".net.xml")
      netconvert_carla(xodr_file, net_file, guess_tls=True)
  
      basedir = os.path.dirname(os.path.realpath(__file__))
      cfg_file = os.path.join(tmpdir, current_map.name + ".sumocfg")
      vtypes_file = os.path.join(
          basedir, "examples", "carlavtypes.rou.xml"
      )
      viewsettings_file = os.path.join(
          basedir, "examples", "viewsettings.xml"
      )
      write_sumocfg_xml(
          cfg_file,
          net_file,
          vtypes_file,
          viewsettings_file,
          args.additional_traci_clients,
      )
  
      sumo_net = sumolib.net.readNet(net_file)
      sumo_simulation = SumoSimulation(
          cfg_file,
          args.step_length,
          host=args.sumo_host,
          port=args.sumo_port,
          sumo_gui=args.sumo_gui,
          client_order=args.client_order,
      )
  
      # ---------------
      # synchronization
      # ---------------
      synchronization = SimulationSynchronization(
          sumo_simulation,
          carla_simulation,
          args.tls_manager,
          args.sync_vehicle_color,
          args.sync_vehicle_lights,
      )
  
      try:
          # ----------
          # Blueprints
          # ----------
          with open("data/vtypes.json") as f:
              vtypes = json.load(f)["carla_blueprints"]
  
          blueprints = vtypes.keys()
  
          filterv = re.compile(args.filterv)
          blueprints = list(filter(filterv.search, blueprints))
  
          if args.safe:
              blueprints = [
                  x
                  for x in blueprints
                  if vtypes[x]["vClass"]
                  not in ("motorcycle", "bicycle")
              ]
              blueprints = [
                  x for x in blueprints if not x.endswith("microlino")
              ]
              blueprints = [
                  x for x in blueprints if not x.endswith("carlacola")
              ]
              blueprints = [
                  x for x in blueprints if not x.endswith("cybertruck")
              ]
              blueprints = [
                  x for x in blueprints if not x.endswith("t2")
              ]
              blueprints = [
                  x for x in blueprints if not x.endswith("sprinter")
              ]
              blueprints = [
                  x for x in blueprints if not x.endswith("firetruck")
              ]
              blueprints = [
                  x for x in blueprints if not x.endswith("ambulance")
              ]
  
          if not blueprints:
              raise RuntimeError(
                  "No blueprints available due to user restrictions."
              )
  
          if args.number_of_walkers > 0:
              logging.warning(
                  "Pedestrians are not supported yet. No walkers will be spawned."
              )
  
          # --------------
          # Spawn vehicles
          # --------------
          # Spawns sumo NPC vehicles.
          sumo_edges = sumo_net.getEdges()
  
          for i in range(args.number_of_vehicles):
              type_id = random.choice(blueprints)
              vclass = vtypes[type_id]["vClass"]
  
              allowed_edges = [
                  e for e in sumo_edges if e.allows(vclass)
              ]
              if allowed_edges:
                  edge = random.choice(allowed_edges)
  
                  traci.route.add("route_{}".format(i), [edge.getID()])
                  traci.vehicle.add(
                      "sumo_{}".format(i),
                      "route_{}".format(i),
                      typeID=type_id,
                  )
              else:
                  logging.error(
                      "Could not found a route for %s. No vehicle will be spawned in sumo",
                      type_id,
                  )
  
          while True:
              start = time.time()
  
              synchronization.tick()
  
              # Updates vehicle routes
              for vehicle_id in traci.vehicle.getIDList():
                  route = traci.vehicle.getRoute(vehicle_id)
                  index = traci.vehicle.getRouteIndex(vehicle_id)
                  vclass = traci.vehicle.getVehicleClass(vehicle_id)
  
                  if index == (len(route) - 1):
                      current_edge = sumo_net.getEdge(route[index])
                      available_edges = list(
                          current_edge.getAllowedOutgoing(vclass).keys()
                      )
                      if available_edges:
                          next_edge = random.choice(available_edges)
  
                          new_route = [
                              current_edge.getID(),
                              next_edge.getID(),
                          ]
                          traci.vehicle.setRoute(vehicle_id, new_route)
  
              end = time.time()
              elapsed = end - start
              if elapsed < args.step_length:
                  time.sleep(args.step_length - elapsed)
  
      except KeyboardInterrupt:
          logging.info("Cancelled by user.")
  
      finally:
          synchronization.close()
  
          if os.path.exists(tmpdir):
              shutil.rmtree(tmpdir)
  
file_path: carla/Co-Simulation/Sumo/spawn_npc_sumo.py
imports: import argparse
  import json
  import logging
  import random
  import re
  import shutil
  import tempfile
  import time
  import lxml.etree
  import glob
  import os
  import sys
  import sumolib
  import traci
  from sumo_integration.carla_simulation import CarlaSimulation
  from sumo_integration.sumo_simulation import SumoSimulation
  from run_synchronization import SimulationSynchronization
  from util.netconvert_carla import netconvert_carla
label_desc: Use image node_id 2 for calling a global function with example usage: main(args) without return types