Get selected actions from Outliner via Python

Hi there,

after searching for a long time and not finding a solution, I hope that someone can help me here.

I would like to be able to select some actions in the outliner (blender file display mode) and receive the list of the selected actions in python.

bpy.context.selected_objects only returns the selected objects in the scene.

for x in bpy.data.actions.keys():
if bpy.data.actions.get(x).select_get():

doesn’t work because .select_get() only seems to work with object.

Does anyone know a solution? Thanks in advance :slight_smile:

There isn’t a good way of getting blend file selection because some data don’t have a selection state. The selection used by the outliner is internal (could potentially access using ctypes).

A more simple way could be to copy the selection to an external buffer and get the references from it.

Example:

import bpy
import os

def blend_file_selection_get():
    area = next((a for a in bpy.context.screen.areas
                if a.type == 'OUTLINER'), None)

    # No outliner, nothing to do.
    if area is None:
        return

    # Copy selection to external buffer.
    bpy.ops.outliner.id_copy({'area': area})

    path = bpy.app.tempdir
    buffer = "\\copybuffer.blend"
    while path:
        if os.path.exists(path + buffer):
            path += buffer
            break
        path = "".join(path).rsplit("\\", 1)[:-1][0]

    if path:
        data = {}
        # Retrieve selection from buffer.
        with bpy.data.libraries.load(path) as (buffer, _):
            for key in dir(buffer):
                objects = getattr(buffer, key)
                if objects:
                    data[key] = objects

        # Clear buffer.
        buffer = bpy.data.libraries.get("copybuffer.blend")
        assert buffer is not None
        bpy.data.batch_remove(ids=(buffer,))

        # Get local data.
        objects = []
        for path, names in data.items():
            dpath = getattr(bpy.data, path)
            for name in names:
                objects.append(dpath.get(name))

        print(objects)

if __name__ == "__main__":
    blend_file_selection_get()
3 Likes

Mhm, I thought so during research. Thanks for the detailed answer :slight_smile: