0
$\begingroup$

I upgraded blender to version 4.0, but I can't use the code bpy.context.selected_ids in version 4.0. The console gives an error AttributeError: 'Context' object has no attribute 'selected_ids'.

Please help me, thank you guys.

import bpy


def ADD_HT_outliner(self, context):
    print(bpy.context.selected_ids)


def register():
    bpy.types.OUTLINER_HT_header.append(ADD_HT_outliner)
$\endgroup$
4
  • $\begingroup$ it must be in the outliner context to work. If not this attribute is not defined/associated to the context. $\endgroup$ Commented Nov 29, 2023 at 11:14
  • $\begingroup$ It should work. How exactly does the error happen ? BTW you can use the passed context with print(context.selected_ids) $\endgroup$ Commented Nov 29, 2023 at 11:57
  • $\begingroup$ Thank you for your answer, but even using print(context.selected_ids), the same error will still occur $\endgroup$ Commented Nov 29, 2023 at 17:02
  • $\begingroup$ I just want to get the id of the selected object in the UI. Isn't your code supposed to report an error? $\endgroup$ Commented Nov 30, 2023 at 3:52

1 Answer 1

2
$\begingroup$

This worked for me in blender 4.0

import bpy

scr = bpy.context.screen
areas = [area for area in scr.areas if area.type == 'OUTLINER']
regions = [region for region in areas[0].regions if region.type == 'WINDOW']

with bpy.context.temp_override(area=areas[0], region=regions[0], screen=scr):
    for obj in bpy.context.selected_ids:
        print(f"Selected: {obj.name} - {type(obj)}")
$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.