For what I understood, I know that an object can be deleted by running bpy.ops.object.delete() when that object is active.

But how can I verify that the object is deleted?

For example:

o = bpy.context.active_object # object I want to delete
print(is_deleted(o)) # False
print(is_deleted(o)) # True

What should I write in the function is_deleted?


I would do it by object name

def is_deleted(o):
    return not (o.name in bpy.data.objects)

# or instead of a function,
# obj will be None if the object isn't found, 
# else it returns the obj reference
obj = bpy.data.objects.get(o.name)  
  • $\begingroup$ Did you mean o.name not in bpy.data.objects? $\endgroup$ – user16046 Jul 31 '15 at 11:34
  • $\begingroup$ And what if the object is deleted and a new one with the same name is created before I perform the verification, would that be a problem? $\endgroup$ – user16046 Jul 31 '15 at 11:40
  • 2
    $\begingroup$ The delete operator is probably safer than removing objects via RNA methods. I don't know if it clears all references, but at least it won't fail if there are any users and I haven't seen any follow-up crashes unlike with RNA (which usually happens if users are cleared without clearing references). Other than that, I would do bpy.data.objects["name"] and catch a possible AttributeError, or use .objects.get("name") for performance reasons. BTW: it's impossible that a deletion occurs while your script is running, unless your script itself does that, because scripts run synchronous/blocking. $\endgroup$ – CodeManX Jul 31 '15 at 18:24
  • 1
    $\begingroup$ Did you test this thoroughly? Because Blender will not crash before it actually tries to dereference an invalid reference (dangling pointer). As far as I understand, you would need to scan through each and every datablock and check if there's a reference to the object you wanna delete and clear it (set to None), then user count should drop to zero and the object can be safely deleted. $\endgroup$ – CodeManX Aug 2 '15 at 9:25
  • 2
    $\begingroup$ I favour a succinct answer and have removed the extra noise. $\endgroup$ – zeffii Aug 2 '15 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy