2

In 2022 there was an answer to a question over at devtalk.blender.org about selecting multiple collections.

The answer was this code segment:

import bpy

Set the area to the outliner

area = bpy.context.area old_type = area.type area.type = 'OUTLINER'

some operations

ids = bpy.context.selected_ids names = [o.name for o in ids] print (names)

Reset the area

area.type = old_type

As it seems, this has worked in the version what was current at that time but it doesn't work in 3.6 anymore. The list remains empty regardless of what is selected in the Outliner.

It was a very elegant solution to get more than one selected collection.

Can this be made working again in 3.6?

In the 3.6 documentation bpy.context.selected_ids is listed under View_3D context. Does this mean that it is now only available in the View_3D area and no longer in the Outliner?

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51
Steve
  • 446
  • 2
  • 8

1 Answers1

1

You can use context override instead. The Outliner you temporarily opened with the statement area.type = 'OUTLINER', which is essentially another instance of that area, didn't retain the collections you had selected. Those selections were only applied to the Outliner area that is open by default when you open Blender, which is why the list turned up empty.

The following solution tackles the problem by pinpointing the specific Outliner area, of which there's typically just one, and then delving into its context to fetch the list of selected collections. If you don't have an Outliner open, this approach will naturally result in an error. And in cases where there are two (like in the Scripting tab) or more Outliners open, the method will focus on the first area instance that you get with next keyword.

import bpy

def print_selected_collections(): ids = bpy.context.selected_ids names = [o.name for o in ids] print(names)

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 ): print_selected_collections()

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51
  • Thank you, it works fine. If I understand correctly this is a more error proof version of saving/changing/restoring an area to do there some operation. The problem then was not in the selected_ids but rather the correct area selection. – Steve Aug 23 '23 at 15:35
  • @Steve I updated the answer to add the explanation – Harry McKenzie Aug 23 '23 at 15:49
  • 1
    Fantastic! Many thanks for taking the time to explain. Now it makes perfect sense. – Steve Aug 24 '23 at 15:47