import bpy import os import sys import glob # settings directory_im = 'C:\\Users\\tristan\\Documents\\troca\\' files = glob.glob(directory_im + "*.rdc") # create collection underneath parent coll def create_coll(parent_layer_collection, collection_name): new_col = bpy.data.collections.new(collection_name) parent_layer_collection.collection.children.link(new_col) new_child_layer_coll = parent_layer_collection.children.get(new_col.name) bpy.context.view_layer.active_layer_collection = new_child_layer_coll return new_child_layer_coll # merge all the objects of two different collections def move_collection(src_collection, dst_collection): if len(src_collection.objects[:]) > 0: for o in src_collection.objects[:]: dst_collection.objects.link(o) src_collection.objects.unlink(o) if len(src_collection.objects[:]) == 0: print("merged " + src_collection.name + " with " + new_collection.name) bpy.data.collections.remove(src_collection) # create a master collection to batch import the files to master_collection = bpy.context.view_layer.active_layer_collection new_collection = create_coll(master_collection, "rdc").collection list_collection = [] import_count = 0 # iterate over files for f in files: head, tail = os.path.split(f) collection_name = tail.replace('.rdc', '') # create a new LayerCollection to import the objects to myCol = create_coll(master_collection, collection_name) # run the importer, it will work within the new sub-collection print("Importing " + str(f)) bpy.ops.import_rdc.google_maps(filepath=(f), filter_glob=".rdc", max_blocks=-1) list_collection.append(myCol.collection) bpy.context.scene.update_tag() print("Imported " + str(f) + "\n") import_count += 1 num = 0 # identify low LOD tiles bpy.ops.object.select_all(action='DESELECT') for o in bpy.data.objects: if o.dimensions[0] > 1 or o.dimensions[1] > 1: num += 1 print("deleted num " + str(num) + " " + o.name + ": " + str(o.dimensions[0]) + "\t" + str(o.dimensions[1])) o.select_set(True) # and delete them bpy.ops.object.delete() print("deleted " + str(num) + " large objects") done = 0 # move first imported collection into main collection move_collection(list_collection[0], new_collection) list_collection = list_collection[1:] # while we still have collections to merge while len(list_collection) > 0: bpy.ops.object.select_all(action='DESELECT') bpy.context.view_layer.objects.active = list_collection[0].objects[0] list_collection[0].objects[0].select_set(True) new_collection.objects[0].select_set(True) # try merging the collections try: print("trying to merge " + list_collection[0].name + " with " + new_collection.name) bpy.ops.object.lily_capture_merger() bpy.context.scene.update_tag() move_collection(list_collection[0], new_collection) if len(list_collection) == 1: list_collection = [] else: list_collection = list_collection[1:] print("succeeded") #print("merged " + list_collection[0].name + " with " + new_collection.name) except: # if that doesn't work, send it to the back of the list print("could not merge " + list_collection[0].name + " with " + new_collection.name) if len(list_collection) == 1: list_collection = [] else: list_collection.append(list_collection[0]) list_collection = list_collection[1:] done += 1 """ for c in bpy.data.collections: #for o in c.objects: # delete duplicates arr = [] for i in range(2, len(c.objects), 2): arr.append(c.objects[i]) for obj in arr: num += 1 print("deleted num " + str(num) + " " + obj.name + ": " + str(obj.dimensions[0]) + "\t" + str(obj.dimensions[1])) obj.select_set(True) bpy.ops.object.delete() """