How to get selection objects list in outliner?

Hi,
it seems, with some changes in blender 3.2.0, script does not work now.
Can you check it?

Hi!

It should now support Blender 3.2.
The download link is the same.

1 Like

I can’t get that working… key mapped to outliner.toggle_hide nothing happens

Something wrong in your setup. It works for me in 3.4.0

What you mean with setup. Installed the script and used the name for the hotkey… not sure what else to do,?

It works for me as expected (with J default keymap)

Thanks for that. Indeed it works in Outliner, but not in viewport when I’m in object mode. Tried different shortcuts. Nothing happens.
But I’m in 3.3 have to check 3.4 so.

Hi.
It seems some changes again in 4.2.
Any ideas how to fix?

Similar to How can I get the currently selected objects in the outliner, if they are hidden? - #9 by Gorgious

import bpy

for area in bpy.context.screen.areas:
    if area.type == "OUTLINER":
        with bpy.context.temp_override(area=area, region=area.regions[-1]):
            print([o for o in bpy.context.selected_ids if isinstance(o, bpy.types.Object)])

Hello,

I made a thread on blender.stackexchange (solved) to show what a problem to get proper selected_ids from Outliner.
If you want to get selected_ids from 3d view look there.

FWIW my answer still stands. You just don’t want to copy the whole context. Use only the override area and region. I’ve added an answer over on BSE.

import bpy

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):        
        largest_area = (max((a for a in context.screen.areas if a.type == "OUTLINER"), key=lambda a:a.width * a.height))
        largest_region = next(r for r in largest_area.regions if r.type == "WINDOW")

        if largest_area and largest_region:
            override_context = {
                'area': largest_area,
                'region': largest_region
            }
            
            with context.temp_override(**override_context):
                print([sel for sel in context.selected_ids if sel.rna_type.name != 'Collection'])

        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)


if __name__ == "__main__":
    register()
1 Like

So what I see now:
toggle_hide.py would work in blender 4.2+ if we disable this row obj.select_set(True)

As same if we rewrite whole code (with Gorgious suggestion):

import bpy

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        # Get the largest outliner area and its region
        largest_area = (max((a for a in context.screen.areas if a.type == "OUTLINER"), key=lambda a:a.width * a.height))
        largest_region = next(r for r in largest_area.regions if r.type == "WINDOW")

        if largest_area and largest_region:
            override_context = {
                'area': largest_area,
                'region': largest_region
            }

            with context.temp_override(**override_context):
                selected_objects = [sel for sel in context.selected_ids if sel.rna_type.name != 'Collection']

        for obj in selected_objects:
            if obj.hide_get():
                obj.hide_set(False)
#                obj.select_set(True)
                # Make object active after switching
                context.view_layer.objects.active = obj

            # Is a visible object instance. Hide it.
            else:
                obj.hide_set(True)

        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)


if __name__ == "__main__":
    register()

To make hided object to be selected in outliner without ctypes not possible?