0
$\begingroup$

I'm appending some collection from other blend file using bpy.data.libraries.load(filepath, link=False). I want to collapse all collection after appending is done.

From https://blender.stackexchange.com/a/159001/115433 I've got code to work separately but when I add code with appending code, only collection before appending gets collapsed. Newly appended collection does not get collapsed.

import bpy

class DIAMOND_OT_collapse(bpy.types.Operator):
    bl_idname = "diamond.collapse"
    bl_label = "collapse"
    
    def collapse1(self,context):
        areas = [a for a in bpy.context.screen.areas if a.type == 'OUTLINER']
        print(areas)
        for area in areas:
            area.tag_redraw()
            bpy.ops.outliner.show_one_level({'area': area}, open = False)
            area.tag_redraw()
            
    def collapse2(self,context):
        screen = context.screen
        collection = context.collection
        collection = bpy.data.collections["Collection 4"]
        view_layer = context.view_layer

        outliners = [a for a in screen.areas if a.type == 'OUTLINER']
        c = context.copy()
        c["collection"] = collection
        for ol in outliners:
            regions = [r for r in ol.regions if r.type == 'WINDOW']
            print(regions)
            c["area"] = ol
            c["region"] = regions[0]
            bpy.ops.outliner.show_one_level(c, open=False)
            ol.tag_redraw()

    def execute(self, context):
        self.collapse1(context)    
        return {'FINISHED'}


class Import_Scene_Collection(bpy.types.Operator):
    bl_idname = "diamond.import_scene"
    bl_label = "Import Quad Scene"

    def change_collection_visibility(self,context):
        children = context.view_layer.layer_collection.children
        col1,col2 = children.get('Collection 2'),children.get('Collection 3')
        
        col1.hide_viewport = True
        col2.hide_viewport = True
    
    def change_viewport_shading(self,context):
        areas = [a for a in context.screen.areas if a.type == 'VIEW_3D']
        for area in areas:        
            for space in area.spaces:
                if space.type == 'VIEW_3D':
                    space.shading.type = 'MATERIAL'
    
    def import_quad_scene(self,context):
        link = False
        filepath = "path/to/dummy.blend"

        #import saved scene collection
        with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to):
            data_to.collections = [c for c in data_from.collections]

        # link collection to scene collection
        for coll in data_to.collections:
            if coll is not None:
               context.scene.collection.children.link(coll)
        
        context.scene.view_layers.update()
    
    def execute(self, context):
        
        self.import_quad_scene(context)
#        self.change_collection_visibility(context)
#        self.change_viewport_shading(context)
        
        bpy.ops.diamond.collapse()
        return {'FINISHED'}

    
class ADDONNAME_PT_main_panel(bpy.types.Panel):
    bl_label = "Diamond Quad Base"
    bl_idname = "ADDONNAME_PT_main_panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Diamond"

    def draw(self, context):
        layout = self.layout

        row = layout.row()
        row.operator(Import_Scene_Collection.bl_idname)
        row.operator(DIAMOND_OT_collapse.bl_idname)  

classes = [ADDONNAME_PT_main_panel,DIAMOND_OT_collapse,Import_Scene_Collection]

def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()

I have attached dummy blend file from which you can import collections :

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.