Consider the following situation:
Suppose you store the Cube in a global variable and prints the object's position every 2 seconds. Assume you never delete the object.
import bpy
ob = [bpy.data.objects["Cube"]]
def in_2_seconds():
print(ob[0].location)
return 2.0
bpy.app.timers.register(in_2_seconds)
Scripts will not work when you perform undo/redo operations in the UI.
Traceback (most recent call last):
File "\Text", line 6, in in_2_seconds
ReferenceError: StructRNA of type Object has been removed
The simple fix should be:
import bpy
ob = [bpy.data.objects["Cube"]]
name = [ob[0].name]
def in_2_seconds():
try: ob[0].name
except:
print("---- Object is Dead ----")
ob[0] = bpy.data.objects[name[0]]
name[0] = ob[0].name
print(ob[0].location)
return 2.0
bpy.app.timers.register(in_2_seconds)
But it's not always correct, like when you rename the Cube then undo once and redo once.
Then what is the correct way to fix that?
bpy.types.ID.session_uid
which should do what you're looking for. docs.blender.org/api/current/… it should remain the same so long as you don't reload the file $\endgroup$