1
$\begingroup$

I have a script which includes the line:

ListObjects = [ o for o in bpy.context.selected_objects if o.type == 'MESH' ]
print(ListObjects)

That works as a standalone script. But within a larger script it fails with:

AttributeError: 'Context' object has no attribute 'selected_objects'

I have tried to follow the advice here: bpy.context.selected_objects by using:

ListObjects = [ o for o in bpy.context.scene.objects if o.select]

But that fails with:

AttributeError: 'Object' object has no attribute 'select'

Has the o.select attribute changed in 2.80, or am I using it incorrectly? Something to do with needing to use an override, perhaps?

$\endgroup$
  • 2
    $\begingroup$ o.select_get(), Blender 2.80 use getter and setter for some value $\endgroup$ – Hikariztw May 24 at 5:05
  • 1
    $\begingroup$ And if you find anything that come up with AttributeError. Try a simple command in interactive console, using Ctrl+Space to auto complete with keywords. Most of the time you can find a similar function due to the 2.80 API change. $\endgroup$ – Hikariztw May 24 at 5:07
2
$\begingroup$

So the thing that is you should always use context that passed by the operator instead of bpy.context. Unless you want to override context with the one you need in rare case.

And Blender 2.80 does change the object.select to its private(?) value, one should consider use object.select_set(bool) and object.select_get() for select value manipulation.

So assume that your function got a context passed in:
ListObjects = [ o for o in context.scene.objects if o.select_get()]
will give you a more reliable context.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.