Improved creation of json with 'json' module

This commit is contained in:
bernat 2020-04-15 13:36:21 +02:00
parent ff6d7d79e4
commit 244a85a9a1
1 changed files with 15 additions and 21 deletions

View File

@ -56,28 +56,22 @@ def generate_json_package(folder, package_name, use_carla_materials):
# write the json # write the json
if (len(maps) > 0): if (len(maps) > 0):
# write the json # build all the maps in .json format
f = open("%s/%s.json" % (folder, package_name), "w") json_maps = []
f.write("{\n") for map_name in maps:
f.write(" \"props\": [],\n")
f.write(" \"maps\": [\n")
for i, map_name in enumerate(maps):
path = map_name[0].replace('\\', '/') path = map_name[0].replace('\\', '/')
name = map_name[1] name = map_name[1]
f.write(" {\n") json_maps.append({
f.write(" \"name\": \"%s\",\n" % name) 'name': name,
f.write(" \"source\": \"%s/%s.fbx\",\n" % (path, name)) 'source': '%s/%s.fbx' % (path, name),
f.write(" \"xodr\": \"%s/%s.xodr\",\n" % (path, name)) 'xodr': '%s/%s.xodr' % (path, name),
if (use_carla_materials): 'use_carla_materials': use_carla_materials
f.write(" \"use_carla_materials\": true\n") })
else: # build and write the .json
f.write(" \"use_carla_materials\": false\n") f = open("%s/%s.json" % (folder, package_name), "w")
if (i == len(maps) - 1): my_json = {'maps': json_maps, 'props': []}
f.write(" }\n") serialized = json.dumps(my_json, sort_keys=False, indent=3)
else: f.write(serialized)
f.write(" },\n")
f.write(" ]\n")
f.write("}\n")
f.close() f.close()
# add # add
json_files.append([folder, "%s.json" % package_name]) json_files.append([folder, "%s.json" % package_name])
@ -385,7 +379,7 @@ def main():
json_list = get_packages_json_list(import_folder) json_list = get_packages_json_list(import_folder)
if (len(json_list) == 0): if (len(json_list) == 0):
json_list = generate_json_package(import_folder, args.package, args.no_carla_materials) json_list = generate_json_package(import_folder, args.package, args.no_carla_materials)
# import_assets_from_json_list(json_list) import_assets_from_json_list(json_list)
if __name__ == '__main__': if __name__ == '__main__':
main() main()