import bpy

# this creates a menu item i can search for. idk where it lives as long as i can search for it
class unfreezeOp(bpy.types.Operator):
    """Override object data of linked libraries"""
    bl_idname = "tibi.unfreeze_links"
    bl_label = "Unfreeze Linked Libraries"

    def execute(self, context):
        # this is for creating the "context override" to trick blender into thinking its on the outliner. idk how this works or why it has to be like this.
        # iterates over every viewer on the screen i guess, so i have to have an outliner open somewhere
        area  = next(area for area in bpy.context.window.screen.areas if area.type == 'OUTLINER')

        with bpy.context.temp_override(
            window=bpy.context.window,
            area=area,
            region=next(region for region in area.regions if region.type == 'WINDOW'),
            screen=bpy.context.window.screen
        ):  # this is where the commands go for the context
            bpy.ops.outliner.liboverride_operation(type='OVERRIDE_LIBRARY_CREATE_HIERARCHY', selection_set='SELECTED')
        # end of context override
                    
        #bpy.ops.object.make_local(type = 'SELECT_OBJECT')
        #bpy.ops.view3d.snap_selected_to_cursor(use_offset = False)
        
        area  = next(area for area in bpy.context.window.screen.areas if area.type == 'VIEW_3D')

        with bpy.context.temp_override(
            window=bpy.context.window,
            area=area,
            region=next(region for region in area.regions if region.type == 'WINDOW'),
            screen=bpy.context.window.screen
        ):  # this is where the commands go for the context
            bpy.ops.view3d.snap_selected_to_cursor(use_offset = False)
        # end of context override    
        
        return {'FINISHED'}

# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
    icon = 'FREEZE'
    self.layout.operator(unfreezeOp.bl_idname, text="Unfreeze Linked Libraries", icon = icon)


def register():
    bpy.utils.register_class(unfreezeOp)
    bpy.types.VIEW3D_MT_object.append(menu_func_export)

def unregister():
    bpy.utils.unregister_class(unfreezeOp)
    bpy.types.VIEW3D_MT_object.remove(menu_func_export)

if __name__ == "__main__":
    register()