fix(tools/gltf_auto_export) : fix export of collections in the main scenes (#39)

* Recursively traverses the main scene collections to create the hollow scene, so that objects in sub-collections are properly exported
* fixes #36
This commit is contained in:
Alix Bott 2023-11-11 21:25:17 +01:00 committed by GitHub
parent 79f29776c2
commit 111f0d0226
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 27 deletions

View File

@ -286,7 +286,6 @@ def generate_hollow_scene(scene, library_collections):
root_collection = scene.collection root_collection = scene.collection
temp_scene = bpy.data.scenes.new(name="temp_scene") temp_scene = bpy.data.scenes.new(name="temp_scene")
copy_root_collection = temp_scene.collection copy_root_collection = temp_scene.collection
scene_objects = [o for o in root_collection.objects]
# we set our active scene to be this one : this is needed otherwise the stand-in empties get generated in the wrong scene # we set our active scene to be this one : this is needed otherwise the stand-in empties get generated in the wrong scene
bpy.context.window.scene = temp_scene bpy.context.window.scene = temp_scene
@ -296,31 +295,43 @@ def generate_hollow_scene(scene, library_collections):
print("FOUND COLLECTION") print("FOUND COLLECTION")
# once it's found, set the active layer collection to the one we found # once it's found, set the active layer collection to the one we found
bpy.context.view_layer.active_layer_collection = found bpy.context.view_layer.active_layer_collection = found
#original_names = {} #original_names = {}
original_names = [] original_names = []
for object in scene_objects:
if object.instance_type == 'COLLECTION' and (object.instance_collection.name in library_collections):
collection_name = object.instance_collection.name
#original_names[object.name] = object.name# + "____bak" # copies the contents of a collection into another one while replacing library instances with empties
#print("custom properties", object, object.keys(), object.items()) def copy_hollowed_collection_into(source_collection, destination_collection):
#for k, e in object.items(): for object in source_collection.objects:
# print("custom properties ", k, e) if object.instance_type == 'COLLECTION' and (object.instance_collection.name in library_collections):
print("object location", object.location) collection_name = object.instance_collection.name
original_name = object.name
original_names.append(original_name)
object.name = original_name + "____bak" #original_names[object.name] = object.name# + "____bak"
empty_obj = make_empty3(original_name, object.location, object.rotation_euler, object.scale, copy_root_collection) #print("custom properties", object, object.keys(), object.items())
"""we inject the collection/blueprint name, as a component called 'BlueprintName', but we only do this in the empty, not the original object""" #for k, e in object.items():
empty_obj['BlueprintName'] = '"'+collection_name+'"' # print("custom properties ", k, e)
empty_obj['SpawnHere'] = '' print("object location", object.location)
original_name = object.name
original_names.append(original_name)
for k, v in object.items(): object.name = original_name + "____bak"
empty_obj[k] = v empty_obj = make_empty3(original_name, object.location, object.rotation_euler, object.scale, destination_collection)
else: """we inject the collection/blueprint name, as a component called 'BlueprintName', but we only do this in the empty, not the original object"""
copy_root_collection.objects.link(object) empty_obj['BlueprintName'] = '"'+collection_name+'"'
empty_obj['SpawnHere'] = ''
for k, v in object.items():
empty_obj[k] = v
else:
destination_collection.objects.link(object)
# for every sub-collection of the source, copy its content into a new sub-collection of the destination
for collection in source_collection.children:
copy_collection = bpy.data.collections.new(collection.name + "____collection_export")
copy_hollowed_collection_into(collection, copy_collection)
destination_collection.children.link(copy_collection)
copy_hollowed_collection_into(root_collection, copy_root_collection)
# bpy.data.scenes.remove(temp_scene) # bpy.data.scenes.remove(temp_scene)
# objs = bpy.data.objects # objs = bpy.data.objects
@ -330,13 +341,17 @@ def generate_hollow_scene(scene, library_collections):
# clear & remove "hollow scene" # clear & remove "hollow scene"
def clear_hollow_scene(temp_scene, original_scene, original_names): def clear_hollow_scene(temp_scene, original_scene, original_names):
# reset original names # reset original names
root_collection = original_scene.collection root_collection = original_scene.collection
scene_objects = [o for o in root_collection.objects]
for object in scene_objects: def restore_original_names(collection):
if object.instance_type == 'COLLECTION': for object in collection.objects:
if object.name.endswith("____bak"): if object.instance_type == 'COLLECTION':
object.name = object.name.replace("____bak", "") if object.name.endswith("____bak"):
object.name = object.name.replace("____bak", "")
for child_collection in collection.children:
restore_original_names(child_collection)
restore_original_names(root_collection)
# remove empties (only needed when we go via ops ????) # remove empties (only needed when we go via ops ????)
root_collection = temp_scene.collection root_collection = temp_scene.collection