[resolved] How do I make an object active from the scene?

Hye everyone,
In the api I found this code

import bpy

view_layer = bpy.context.view_layer

# Create new light datablock.
light_data = bpy.data.lights.new(name="New Light", type='POINT')

# Create new object with our light datablock.
light_object = bpy.data.objects.new(name="New Light", object_data=light_data)

# Link light object to the active collection of current view layer,
# so that it'll appear in the current scene.
view_layer.active_layer_collection.collection.objects.link(light_object)

# Place light to a specified location.
light_object.location = (5.0, 5.0, 5.0)

# And finally select it and make it active.
light_object.select_set(True)
view_layer.objects.active = light_object

It works in blender 2.82 but it is impossible to find anything about the “objects.active” element. I tried to do it again from the scene but the console tells me that the active property doesn’t exist. I don’t understand.

Is it possible to directly target an object to make it active, or to do it from the scene?

Have a nice day,

Not really sure what you’re asking for since the answer is in your question. If you’re asking how to make an object active from the scene you just click on it, the last thing you select will be active. If you want to do it in Python use the code in your post. If you’re asking something else you’re going to need to clarify.

context.view_layer.objects.active = light_object
is the right (and only one) way to make your light_object active.

@stephen_leger
thank you, I’m fixed now.

@testure
It’s just that I thought there had to be an easier way. I thought the “select_set” function had an effect on the object that will be active according to the order in which the objects are selected with. So I thought there should be a simple function to directly design the active object.

No, apparently if I’m not mistaken, you have to explicitly say which object will be active. At least that’s what my code tests gave.