How can I check if the object exists in python? I want to create an add-on but I'm unable to find any reference. For example how can I check if "Cube" exist? If yes I will make another object green, if no I will make it red.


Best practice is iterating through Scene.objects collection (all objects of the current scene):

import bpy

for o in bpy.context.scene.objects:
    if o.name == "Cube":
        print ("Cube found in scene")

Even easier to read is using python's get() on the actual collection to get the reference:

cube = bpy.context.scene.objects.get("Cube")
if cube:
    print ("Cube found in scene")

Recommend use the python console to figure out:

>>> C.scene.objects.get("Cube")

Alternatively you can also iterate through Data.objects (all objects of the actual file using):

>>> D.objects.get("Cube")

For the sake of completeness, demo on how to get all objects starting with "Cube":

import bpy

objs = []
for o in bpy.data.objects:
    if o.name.startswith("Cube"):

if objs:
    print ("Cube found {} time(s) in file".format(len(objs)))

Related: Python: Selecting object by name in 2.8

| improve this answer | |
  • $\begingroup$ Am I allowed to use break and else? $\endgroup$ – Andy Andy Jun 25 at 18:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.