If you create an object with bpy.ops, it will become "active" after creation.
If you register the object in a variable when it is active, the next time you want to access the object, you can directly access the variable (data block) to manipulate the object and make it active again.
If you want to use "bpy.ops・・・・", you will need to make the object active once.
import bpy
If you create an object with bpy.ops, it will become "active" after creation.
bpy.ops.mesh.primitive_cube_add(enter_editmode=False, align='WORLD', location=(0, 0, 1), scale=(1, 1, 1))
"bpy.context.object" stores the object in the active state in a variable.
add_obj1 = bpy.context.object
Similarly, "bpy.context.object" stores the object in the active state in a variable.
bpy.ops.mesh.primitive_cylinder_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
add_obj2 = bpy.context.object
If you want to change the location of the Cube.
add_obj1.location = (5.0, 5.0, 5.0)
If you want to change the location of the cylinder.
add_obj2.location = (-5.0, 5.0, 5.0)
How to select it after the script
Sometimes you will want to select a specific object again after the script.
In this case, you can select the object from the name of the object in the scene.
In this case, you can select the object by its name,
or you can use the RNA and ID properties to identify it.
Here's a point.
If you select an object later by name
If you change the name while you are working on it, you will not know how to set the object again.
This is quite common.
With "Custom Properties", you can set the object in a way that does not depend on its name,
With custom properties, the setting method is independent of the name.
You may want to keep this method in mind if you want to select a particular object again later.
You may want to keep this in mind.

Example
- 1 is selected
- 2 is selected and active state
- 0 is unselected
- And so on...
I have written four scripts.
Unselected script
bool_False.py
import bpy
def bool_False():
bpy.types.Object.my_rna_bool = bpy.props.IntProperty(name='RNA bool')
bpy.context.active_object.my_rna_bool = False
layer = bpy.context.view_layer
layer.update()
bool_False()
The script to make it selected.
bool_true.py
import bpy
def bool_true():
bpy.types.Object.my_rna_bool = bpy.props.IntProperty(name='RNA bool')
bpy.context.active_object.my_rna_bool = True
layer = bpy.context.view_layer
layer.update()
bool_true()
Script to select and make active
bool_active_true.py
import bpy
def bool_active_true():
bpy.types.Object.my_rna_bool = bpy.props.IntProperty(name='RNA bool')
bpy.context.active_object.my_rna_bool = 2
layer = bpy.context.view_layer
layer.update()
bool_active_true()
Script to output the result of the above configuration
select_set_active.py
import bpy
def select_set_active():
for obj in bpy.context.scene.objects:
print(obj.name, obj.get("my_rna_bool"))
obj_bool = obj.get("my_rna_bool")
if obj_bool == 2:
# If there is more than one 2 in the value of the Int, the last one in the array will be active.
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
elif obj_bool == 1:
# select only.
obj.select_set(True)
else:
obj.select_set(False)
pass
select_set_active()

Script results

bpy.data.objects['Cylinder'].select_set(False); obj.select_set(True); bpy.context.view_layer.objects.active = obj– Markus von Broady Jan 11 '22 at 18:52objvariable pointing to the object to be selected. In your updated code you no longer defineobj, and so using the undefined name causes an error NameError: name 'obj' is not defined, stopping the execution of the script. Copy-paste my code and add it to your original code, then it should work. – Markus von Broady Jan 11 '22 at 19:55bpy.data.objects['Cylinder'].select_set(False); obj.select_set(True); bpy.context.view_layer.objects.active = obj
bpy.data.objects['Cylinder'].select_set(False); obj.select_set(True);
– ofey Jan 11 '22 at 21:32bpy.context.view_layer.objects.active = obj But this selects nothing.