2019-06-19 02:01:13 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
|
|
|
|
# Barcelona (UAB).
|
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the MIT license.
|
|
|
|
# For a copy, see <https://opensource.org/licenses/MIT>.
|
|
|
|
|
2019-06-21 22:52:27 +08:00
|
|
|
"""Import Assets to Carla"""
|
2019-06-19 02:01:13 +08:00
|
|
|
|
2019-06-21 22:52:27 +08:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
from contextlib import contextmanager
|
2019-06-26 20:47:23 +08:00
|
|
|
import errno
|
2019-06-19 02:01:13 +08:00
|
|
|
import fnmatch
|
|
|
|
import json
|
2019-06-21 22:52:27 +08:00
|
|
|
import os
|
|
|
|
import shutil
|
2019-06-19 02:01:13 +08:00
|
|
|
import subprocess
|
|
|
|
|
2019-07-01 17:47:32 +08:00
|
|
|
# Global variables
|
|
|
|
IMPORT_SETTING_FILENAME = "importsetting.json"
|
|
|
|
SCRIPT_NAME = os.path.basename(__file__)
|
|
|
|
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
|
2019-07-09 20:15:51 +08:00
|
|
|
# Go two directories above the current script
|
|
|
|
CARLA_ROOT_PATH = os.path.normpath(SCRIPT_DIR + '/../..')
|
2019-07-01 17:47:32 +08:00
|
|
|
|
|
|
|
|
2019-06-19 02:01:13 +08:00
|
|
|
def get_packages_json_list(folder):
|
2019-06-21 22:52:27 +08:00
|
|
|
"""Returns a list with the paths of each package's json
|
|
|
|
files that has been found recursively in the input folder.
|
|
|
|
"""
|
2019-06-19 02:01:13 +08:00
|
|
|
json_files = []
|
|
|
|
|
2019-06-26 20:47:23 +08:00
|
|
|
for root, _, filenames in os.walk(folder):
|
2019-06-19 02:01:13 +08:00
|
|
|
for filename in fnmatch.filter(filenames, "*.json"):
|
|
|
|
json_files.append([root, filename])
|
|
|
|
|
|
|
|
return json_files
|
|
|
|
|
2019-06-21 22:52:27 +08:00
|
|
|
|
2019-06-19 02:01:13 +08:00
|
|
|
def invoke_commandlet(name, arguments):
|
2019-06-21 22:52:27 +08:00
|
|
|
"""Generic function for running a commandlet with its arguments."""
|
2019-06-19 02:01:13 +08:00
|
|
|
if os.name == "nt":
|
|
|
|
sys_name = "Win64"
|
|
|
|
elif os.name == "posix":
|
|
|
|
sys_name = "Linux"
|
|
|
|
ue4_path = os.environ["UE4_ROOT"]
|
|
|
|
editor_path = "%s/Engine/Binaries/%s/UE4Editor" % (ue4_path, sys_name)
|
2019-07-01 17:47:32 +08:00
|
|
|
uproject_path = os.path.join(CARLA_ROOT_PATH, "Unreal", "CarlaUE4", "CarlaUE4.uproject")
|
2019-06-19 02:01:13 +08:00
|
|
|
full_command = "%s %s -run=%s %s" % (editor_path, uproject_path, name, arguments)
|
2019-06-27 21:05:39 +08:00
|
|
|
print("\n[" + str(SCRIPT_NAME) + "] Running command:\n$ " + full_command + '\n')
|
2019-06-19 02:01:13 +08:00
|
|
|
subprocess.check_call([full_command], shell=True)
|
|
|
|
|
2019-06-21 22:52:27 +08:00
|
|
|
|
2019-06-19 02:01:13 +08:00
|
|
|
def generate_import_setting_file(package_name, json_dirname, props, maps):
|
2019-06-21 22:52:27 +08:00
|
|
|
"""Creates the PROPS and MAPS import_setting.json file needed
|
|
|
|
as an argument for using the ImportAssets commandlet
|
|
|
|
"""
|
2019-06-21 00:32:56 +08:00
|
|
|
importfile = os.path.join(os.getcwd(), IMPORT_SETTING_FILENAME)
|
2019-06-19 02:01:13 +08:00
|
|
|
if os.path.exists(importfile):
|
|
|
|
os.remove(importfile)
|
|
|
|
|
|
|
|
with open(importfile, "w+") as fh:
|
|
|
|
import_groups = []
|
|
|
|
file_names = []
|
|
|
|
import_settings = []
|
|
|
|
import_settings.append({
|
|
|
|
"bImportMesh": 1,
|
|
|
|
"bConvertSceneUnit": 1,
|
|
|
|
"bConvertScene": 1,
|
|
|
|
"bCombineMeshes": 1,
|
|
|
|
"bImportTextures": 1,
|
|
|
|
"bImportMaterials": 1,
|
|
|
|
"bRemoveDegenerates": 1,
|
|
|
|
"AnimSequenceImportData": {},
|
|
|
|
"SkeletalMeshImportData": {},
|
|
|
|
"TextureImportData": {},
|
|
|
|
"StaticMeshImportData": {
|
|
|
|
"bRemoveDegenerates": 1,
|
|
|
|
"bAutoGenerateCollision": 0,
|
|
|
|
"bCombineMeshes": 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
for prop in props:
|
|
|
|
props_dest = "/" + "/".join(["Game", package_name, "Static", prop["tag"], prop["name"]])
|
|
|
|
|
|
|
|
file_names = [os.path.join(json_dirname, prop["source"])]
|
|
|
|
import_groups.append({
|
|
|
|
"ImportSettings": import_settings,
|
|
|
|
"FactoryName": "FbxFactory",
|
|
|
|
"DestinationPath": props_dest,
|
|
|
|
"bReplaceExisting": "true",
|
|
|
|
"FileNames": file_names
|
|
|
|
})
|
|
|
|
|
2019-07-09 01:34:52 +08:00
|
|
|
for umap in maps:
|
|
|
|
maps_dest = "/" + "/".join(["Game", package_name, "Maps", umap["name"]])
|
2019-06-19 02:01:13 +08:00
|
|
|
|
2019-07-09 01:34:52 +08:00
|
|
|
file_names = [os.path.join(json_dirname, umap["source"])]
|
2019-06-19 02:01:13 +08:00
|
|
|
import_groups.append({
|
|
|
|
"ImportSettings": import_settings,
|
|
|
|
"FactoryName": "FbxFactory",
|
|
|
|
"DestinationPath": maps_dest,
|
|
|
|
"bReplaceExisting": "true",
|
|
|
|
"FileNames": file_names
|
|
|
|
})
|
|
|
|
|
|
|
|
fh.write(json.dumps({"ImportGroups": import_groups}))
|
|
|
|
fh.close()
|
|
|
|
return importfile
|
|
|
|
|
2019-06-21 22:52:27 +08:00
|
|
|
|
2019-06-21 00:32:56 +08:00
|
|
|
def generate_package_file(package_name, props, maps):
|
2019-06-21 22:52:27 +08:00
|
|
|
"""Creates the PackageName.Package.json file for the package."""
|
2019-06-19 02:01:13 +08:00
|
|
|
output_json = {}
|
2019-06-21 00:32:56 +08:00
|
|
|
|
2019-06-19 02:01:13 +08:00
|
|
|
output_json["props"] = []
|
|
|
|
for prop in props:
|
|
|
|
name = prop["name"]
|
|
|
|
size = prop["size"]
|
2019-07-01 17:47:32 +08:00
|
|
|
source_name = os.path.basename(prop["source"]).split('.')
|
|
|
|
if len(source_name) < 2:
|
|
|
|
print("[Warning] File name '" + prop["source"] + "' contains multiple dots ('.')")
|
|
|
|
|
|
|
|
source_name = '.'.join([source_name[0], source_name[0]])
|
2019-06-19 02:01:13 +08:00
|
|
|
|
2019-07-01 17:47:32 +08:00
|
|
|
path = "/" + "/".join(["Game", package_name, "Static", prop["tag"], prop["name"], source_name])
|
2019-06-19 02:01:13 +08:00
|
|
|
|
|
|
|
output_json["props"].append({
|
|
|
|
"name": name,
|
2019-06-21 00:32:56 +08:00
|
|
|
"path": path,
|
2019-06-19 02:01:13 +08:00
|
|
|
"size": size,
|
|
|
|
})
|
|
|
|
|
|
|
|
output_json["maps"] = []
|
2019-07-09 01:34:52 +08:00
|
|
|
for umap in maps:
|
|
|
|
path = "/" + "/".join(["Game", package_name, "Maps", umap["name"]])
|
|
|
|
use_carla_materials = umap["use_carla_materials"] if "use_carla_materials" in umap else False
|
2019-06-21 22:52:27 +08:00
|
|
|
output_json["maps"].append({
|
2019-07-09 01:34:52 +08:00
|
|
|
"name": umap["name"],
|
2019-06-21 22:52:27 +08:00
|
|
|
"path": path,
|
2019-06-21 00:32:56 +08:00
|
|
|
"use_carla_materials": use_carla_materials
|
|
|
|
})
|
2019-06-19 02:01:13 +08:00
|
|
|
|
2019-07-01 17:47:32 +08:00
|
|
|
package_config_path = os.path.join(CARLA_ROOT_PATH, "Unreal", "CarlaUE4", "Content", package_name, "Config")
|
2019-06-21 00:32:56 +08:00
|
|
|
if not os.path.exists(package_config_path):
|
2019-06-21 22:52:27 +08:00
|
|
|
try:
|
|
|
|
os.makedirs(package_config_path)
|
|
|
|
except OSError as exc:
|
2019-06-26 20:47:23 +08:00
|
|
|
if exc.errno != errno.EEXIST:
|
2019-06-21 22:52:27 +08:00
|
|
|
raise
|
2019-06-19 02:01:13 +08:00
|
|
|
|
2019-06-21 00:32:56 +08:00
|
|
|
with open(os.path.join(package_config_path, package_name + ".Package.json"), "w+") as fh:
|
2019-06-26 20:47:23 +08:00
|
|
|
json.dump(output_json, fh, indent=4)
|
2019-06-19 02:01:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
def import_assets(package_name, json_dirname, props, maps):
|
2019-06-21 22:52:27 +08:00
|
|
|
"""Same commandlet is used for importing assets and also maps."""
|
2019-06-19 02:01:13 +08:00
|
|
|
commandlet_name = "ImportAssets"
|
|
|
|
|
|
|
|
# Import Props
|
|
|
|
import_setting_file = generate_import_setting_file(package_name, json_dirname, props, maps)
|
2019-07-02 22:45:04 +08:00
|
|
|
commandlet_arguments = "-importSettings=\"%s\" -nosourcecontrol -replaceexisting" % import_setting_file
|
2019-06-21 00:32:56 +08:00
|
|
|
invoke_commandlet(commandlet_name, commandlet_arguments)
|
2019-06-19 02:01:13 +08:00
|
|
|
os.remove(import_setting_file)
|
|
|
|
|
2019-06-21 22:52:27 +08:00
|
|
|
# Move maps XODR files if any
|
|
|
|
for umap in maps:
|
|
|
|
# Make sure XODR info is full and the file exists
|
2019-06-26 20:47:23 +08:00
|
|
|
if "xodr" in umap and umap["xodr"] and os.path.isfile(os.path.join(json_dirname, umap["xodr"])):
|
|
|
|
# Make sure the `.xodr` file have the same name than the `.umap`
|
|
|
|
xodr_path = os.path.abspath(os.path.join(json_dirname, umap["xodr"]))
|
|
|
|
umap_name = umap["name"]
|
|
|
|
xodr_name = '.'.join([umap_name, "xodr"])
|
|
|
|
|
|
|
|
xodr_folder_destin = os.path.join(
|
2019-07-01 17:47:32 +08:00
|
|
|
CARLA_ROOT_PATH,
|
2019-06-26 20:47:23 +08:00
|
|
|
"Unreal",
|
|
|
|
"CarlaUE4",
|
|
|
|
"Content",
|
|
|
|
package_name,
|
|
|
|
"Maps",
|
|
|
|
umap_name,
|
|
|
|
"OpenDrive")
|
|
|
|
|
|
|
|
if not os.path.exists(xodr_folder_destin):
|
|
|
|
os.makedirs(xodr_folder_destin)
|
|
|
|
|
|
|
|
xodr_path_destin = os.path.join(
|
|
|
|
xodr_folder_destin,
|
|
|
|
xodr_name)
|
|
|
|
|
|
|
|
print('Copying "' + xodr_path + '" to "' + xodr_path_destin + '"')
|
2019-06-21 22:52:27 +08:00
|
|
|
shutil.copy2(xodr_path, xodr_path_destin)
|
2019-06-19 02:01:13 +08:00
|
|
|
|
2019-06-26 20:47:23 +08:00
|
|
|
# Create package file
|
|
|
|
generate_package_file(package_name, props, maps)
|
|
|
|
|
|
|
|
|
2019-06-19 02:01:13 +08:00
|
|
|
def import_assets_from_json_list(json_list):
|
2019-06-21 23:19:37 +08:00
|
|
|
maps = []
|
2019-07-01 17:47:32 +08:00
|
|
|
package_name = ""
|
2019-06-19 02:01:13 +08:00
|
|
|
for dirname, filename in json_list:
|
|
|
|
# Read json file
|
|
|
|
with open(os.path.join(dirname, filename)) as json_file:
|
|
|
|
data = json.load(json_file)
|
2019-07-09 01:34:52 +08:00
|
|
|
# Take all the fbx registered in the provided json files
|
2019-06-19 02:01:13 +08:00
|
|
|
# and place it inside unreal in the provided path (by the json file)
|
|
|
|
maps = data["maps"]
|
|
|
|
props = data["props"]
|
|
|
|
package_name = filename.replace(".json", "")
|
|
|
|
|
|
|
|
import_assets(package_name, dirname, props, maps)
|
2019-06-21 23:19:37 +08:00
|
|
|
move_uassets(package_name, maps)
|
2019-06-19 02:01:13 +08:00
|
|
|
|
2019-07-04 00:16:34 +08:00
|
|
|
if not package_name:
|
|
|
|
print("No Packages JSONs found, nothing to import. Skipping package.")
|
|
|
|
continue
|
2019-07-01 17:47:32 +08:00
|
|
|
|
2019-06-21 22:52:27 +08:00
|
|
|
|
2019-06-21 23:19:37 +08:00
|
|
|
def move_uassets(package_name, maps):
|
2019-07-09 01:34:52 +08:00
|
|
|
for umap in maps:
|
|
|
|
origin_path = os.path.join(CARLA_ROOT_PATH, "Unreal", "CarlaUE4", "Content", package_name, "Maps", umap["name"])
|
2019-07-01 17:47:32 +08:00
|
|
|
dest_base_path = os.path.join(CARLA_ROOT_PATH, "Unreal", "CarlaUE4", "Content", package_name, "Static")
|
2019-06-21 23:19:37 +08:00
|
|
|
|
|
|
|
# Create the 3 posible destination folder path
|
2019-07-09 01:34:52 +08:00
|
|
|
marking_dir = os.path.join(dest_base_path, "MarkingNode", umap["name"])
|
|
|
|
road_dir = os.path.join(dest_base_path, "RoadNode", umap["name"])
|
|
|
|
terrain_dir = os.path.join(dest_base_path, "TerrainNode", umap["name"])
|
2019-06-21 23:19:37 +08:00
|
|
|
|
|
|
|
# Create folders if they do not exist
|
|
|
|
if not os.path.exists(marking_dir):
|
|
|
|
os.makedirs(marking_dir)
|
|
|
|
if not os.path.exists(road_dir):
|
|
|
|
os.makedirs(road_dir)
|
|
|
|
if not os.path.exists(terrain_dir):
|
|
|
|
os.makedirs(terrain_dir)
|
|
|
|
|
2019-07-09 01:34:52 +08:00
|
|
|
# Move uassets to corresponding folder
|
2019-06-21 23:19:37 +08:00
|
|
|
for filename in os.listdir(origin_path):
|
|
|
|
if "MarkingNode" in filename:
|
2019-06-22 02:55:31 +08:00
|
|
|
shutil.move(os.path.join(origin_path, filename), os.path.join(marking_dir, filename))
|
2019-06-21 23:19:37 +08:00
|
|
|
if "RoadNode" in filename:
|
2019-06-22 02:55:31 +08:00
|
|
|
shutil.move(os.path.join(origin_path, filename), os.path.join(road_dir, filename))
|
2019-06-21 23:19:37 +08:00
|
|
|
if "TerrainNode" in filename:
|
2019-06-22 02:55:31 +08:00
|
|
|
shutil.move(os.path.join(origin_path, filename), os.path.join(terrain_dir, filename))
|
2019-06-21 23:19:37 +08:00
|
|
|
|
|
|
|
|
2019-06-19 02:01:13 +08:00
|
|
|
def main():
|
2019-07-01 17:47:32 +08:00
|
|
|
import_folder = os.path.join(CARLA_ROOT_PATH, "Import")
|
2019-06-19 02:01:13 +08:00
|
|
|
json_list = get_packages_json_list(import_folder)
|
|
|
|
import_assets_from_json_list(json_list)
|
|
|
|
|
2019-06-21 22:52:27 +08:00
|
|
|
|
2019-06-19 02:01:13 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|