0
$\begingroup$

This is my current code:

import bpy

class AppendCollectionOperator(bpy.types.Operator):
    bl_idname = "object.append_collection_operator"
    bl_label = "Append Collection from .blend"
    
    filepath: bpy.props.StringProperty(subtype='FILE_PATH')
    collection_name: bpy.props.StringProperty()

    def execute(self, context):
        # Append .blend file
        with bpy.data.libraries.load(self.filepath) as (data_from, data_to):
            data_to.collections = [name for name in data_from.collections]

        # Link the collection to the scene
        for coll in data_to.collections:
            bpy.context.collection.children.link(coll)

        return {'FINISHED'}

class AppendCollectionPanel(bpy.types.Panel):
    bl_label = "Append Collection Panel"
    bl_idname = "PT_AppendCollectionPanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Tools'

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

        # Filepath input
        layout.prop(context.scene, "blend_filepath", text="Blend File Path")

        # Collection dropdown
        layout.prop_search(context.scene, "collection_name", bpy.data, "collections", text="Collection")

        # Append button
        layout.operator("object.append_collection_operator", text="Append Collection")

    def update_collections(self, context):
        # Update collections list based on blend_filepath
        filepath = context.scene.blend_filepath
        with bpy.data.libraries.load(filepath) as (data_from, data_to):
            data_to.collections = [name for name in data_from.collections]

        self.layout.prop(context.scene, "collection_name", text="Collection")

def register():
    bpy.utils.register_class(AppendCollectionOperator)
    bpy.utils.register_class(AppendCollectionPanel)

    bpy.types.Scene.blend_filepath = bpy.props.StringProperty(subtype='FILE_PATH', update=AppendCollectionPanel.update_collections)
    bpy.types.Scene.collection_name = bpy.props.StringProperty()

def unregister():
    bpy.utils.unregister_class(AppendCollectionOperator)
    bpy.utils.unregister_class(AppendCollectionPanel)

    del bpy.types.Scene.blend_filepath
    del bpy.types.Scene.collection_name

if __name__ == "__main__":
    register()
$\endgroup$
9
  • $\begingroup$ You really should show the error message you’re getting, not just the code you’re using. Also, why make an add-on for this? Why not just use Blender’s asset system, or the built in collection importer? $\endgroup$
    – TheLabCat
    Commented Jun 28 at 1:11
  • 1
    $\begingroup$ Python: Traceback (most recent call last): File "\Text", line 12, in execute OSError: load: failed to open blend file $\endgroup$ Commented Jun 28 at 2:47
  • $\begingroup$ Blender's asset system is too large or the collection imports are too repetitive. $\endgroup$ Commented Jun 28 at 4:38
  • $\begingroup$ The asset system is too… “large”? How so? Do you have too many assets? To effectively scroll through them? $\endgroup$
    – TheLabCat
    Commented Jun 28 at 5:44
  • $\begingroup$ Go ahead and edit the error into the question. After pasting, select it and use the code block formatting button to make it show up nicely. $\endgroup$
    – TheLabCat
    Commented Jun 28 at 5:46

0

You must log in to answer this question.

Browse other questions tagged .