I think it is best to define selected object rather than active object.
Blender will take care of "active" object on its own. At any time, there is always supposed to be only one "active" object but can have several selected objects. If there is nothing selected, and you select "ob1" then "ob2", "ob2" is active object. If you then delete "ob2" then "ob1" (the last selected ob still in existence) is active etc... something like that, you get the idea. Sometimes active is not necessarily selected if you deselect all, there is still one active and active does not trigger selection to ON. I use:
Code: Select all
print("Error selecting objects[" + obj_name + "]")
print(" -- verify: change mode to OBJECT and/or object exists")
The bpy.ops.object.select_name can have extend=True (your object added to already selected) or extend=False (everything deselected except your object)