6
$\begingroup$

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.

$\endgroup$

2 Answers 2

12
$\begingroup$

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")
bpy.data.objects['Cube']

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

>>> D.objects.get("Cube")
bpy.data.objects['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"):
        objs.append(o)

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

Related: Python: Selecting object by name in 2.8

$\endgroup$
4
  • $\begingroup$ Am I allowed to use break and else? $\endgroup$
    – Andy Andy
    Jun 25, 2020 at 18:14
  • $\begingroup$ Why not simply if 'Cube' in bpy.context.scene.objects: ? $\endgroup$ Feb 25, 2021 at 13:39
  • $\begingroup$ Good idea, would have to explain why this works and that a prop_collection similar to a dict - __contains__ slot... get() is prefered and also returns a reference to work with, so I'd use it @MarkusvonBroady $\endgroup$
    – brockmann
    Feb 25, 2021 at 14:09
  • $\begingroup$ I like dictionary keys method of the below answer. I use keys so frequently in regular python codes and so its great to use that than using get() $\endgroup$ Mar 5 at 1:48
2
$\begingroup$

I use .keys()

objects = bpy.context.scene.objects.keys()
if 'Cube' not in objects:
     print("cube doesn't exist!")
elif 'Cube' in objects:
     print("Cube exists!!")

the same as dict.keys() in python dict returns a list of the objects names

$\endgroup$
2
  • $\begingroup$ Thanks for trying to help, however there's a few issues with your answer, which have resulted with a downvote: an answer already exists, but you didn't explain why your answer is a reasonable alternative to it; you didn't check your answer (there's a syntax error because of the apostrophe in the string); there's no point in calling .keys() method, as that will be the default iterator; instead of using elif you could just use else and make the code cleaner. $\endgroup$ Feb 25, 2021 at 13:44
  • $\begingroup$ This answer is much better than the selected answer. Yes there is a syntax error but I really appreciate the key idea highlighted in this answer which is to use dictionaries something which we do so frequently in regular python codes. Also i appreciate the explicit usage of keys(). $\endgroup$ Mar 5 at 1:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .