28
$\begingroup$

I'm trying to amend an addon that I made for Blender 2.7X to work with Blender 2.80.
The problem is with

bpy.context.scene.objects.active = some_object

This is how I used to set an active object, the problem is, active property was removed in the new API!,reading other Q&As, I found 2 suggestions:

bpy.context.object

and

bpy.context.active_object 

The problem is, both of these will return the already active object, but when I try to use them this way:

bpy.context.active_object = some_object

or

bpy.context.object = some_object

I'm getting an AttributeError: property is read-only!

I tried the documentation with no luck!

EDIT:

According to Blender 2.8 WIKI API changes: https://wiki.blender.org/wiki/Dev:2.8/Source/LayersCollections/API-Changes

bpy.context.scene.objects.active

changed into

bpy.context.render_layer.objects.active

This doesn't work, AttributeError: 'Context' object has no attribute 'render_layer'

$\endgroup$

1 Answer 1

59
$\begingroup$

Use the view_layer

Set ViewLayer.active to any object in view layer or None

context.view_layer.objects.active = ob

Note selecting has also changed from ob.select = True to

ob.select_set(True)

which together will emulate the general case UI state of having the context object both active and selected.

$\endgroup$
4
  • 3
    $\begingroup$ Great, many thanks @batFINGER, may I ask, how did you figure it out, even the wiki changes got it wrong! $\endgroup$
    – Georges D
    Commented Dec 19, 2018 at 15:32
  • 3
    $\begingroup$ Upgrading scripts. Read that wiki a while back and remembered it was a context.somelayer.objects.active but not which one lol. Pecking around in API in py console, looking for active prop on some of new type context layers objects collection, and on view_layer bingo. Have a feeling it has been answered here before too. $\endgroup$
    – batFINGER
    Commented Dec 19, 2018 at 15:54
  • 2
    $\begingroup$ Lol, I guess hacking skills are required to properly read the API $\endgroup$
    – Georges D
    Commented Dec 19, 2018 at 16:01
  • 2
    $\begingroup$ Wouldn't be able to do it without your help @batFINGER many thanks! github.com/sergeod9/Easy_Align_Addon/blob/master/… $\endgroup$
    – Georges D
    Commented Dec 19, 2018 at 17:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .