# links selected collections from the outliner to a scene. note it will link all the hierarchy down from a given collection to the root of the scene, in addition to the selected subcollections

import bpy

# the operator's function must be defined outside of the operator bc this is the only way to pass the scene argument to it
def ColToScene(context):
    # getting every collection type item in the selected items
    ids = bpy.context.selected_ids
    colls = [col for col in ids if col.rna_type.name == 'Collection']
    # context.scene is the current scene passed from the operator
    for col in colls:
        # print (col.name)
        # print (sel_scene.name)
        try:
            context.scene.collection.children.link(col)
        except:
            # if any of the collections are already in the scene, just move on
            continue

class ColToSceneOp(bpy.types.Operator):
    """Link collections to a scene"""
    bl_label = "Link Collections to Scene"
    bl_idname = "tibi.coltoscene"
    # operator idnames must have at least one . and be all lowercase. for some reason.
    # bl_options = {'REGISTER', 'UNDO'}
    
    # @classmethod
    # def poll(cls, context):
    #     return getattr(context, "scene", False)
       
    def execute(self, context):
        ColToScene(context)
        return {'FINISHED'}

class scenesMenu(bpy.types.Menu):
    bl_label = "Scenes"
    bl_idname = "tibi.scenes_menu"
    bl_icon = 'SCENE_DATA'
                
    def draw(self, context):
            layout = self.layout
            layout.operator_context = 'INVOKE_REGION_WIN'
            
            if not bpy.data.scenes:
                return
            
            layout.separator()
            
            # create a menu with all the scenes. make the current scene non-clickable (label), assign the operator to all the other scenes
            for i in range(len(bpy.data.scenes)):
                sel_scene = bpy.data.scenes[i]
                if sel_scene.name == bpy.context.scene.name:
                    icon = 'SCENE_DATA'
                    layout.label(text = sel_scene.name, icon = icon)        
                else:
                    icon = 'NONE'
                    # this passes the data from context.scene in ColToScene to sel_scene, like setting an argument before running the operator
                    layout.context_pointer_set(name="scene", data=sel_scene)
                    layout.operator(ColToSceneOp.bl_idname, text = sel_scene.name)
                    

def draw_item(self, context):
    layout = self.layout
    icon = 'SCENE_DATA'
    layout.menu(scenesMenu.bl_idname, icon = icon)

def register():
    bpy.utils.register_class(ColToSceneOp)
    bpy.utils.register_class(scenesMenu)
    # add the scenes list to the outliner context menu as a submenu, only for clicking on collections
    bpy.types.OUTLINER_MT_collection.append(draw_item)

def unregister():
    bpy.types.OUTLINER_MT_collection.remove(draw_item)
    bpy.utils.unregister_class(ColToSceneOp)
    bpy.utils.unregister_class(scenesMenu)
            
if __name__ == "__main__":
    register()

    # The menu can also be called from scripts
    # bpy.ops.wm.call_menu(name=scenesMenu.bl_idname)