how to check if object exists

I am parsing an input file with the following code and run into a problem when the object (name) does not exists in the blend file. How can I handle the object.get without it stopping the program?

...
ar = split(line, '|')
    
ob_act = Blender.Object.Get(ar[0])
...

You could test your objects against the list of all objects, like this:

allobs = Blender.Object.Get()
ar = split(line, '|')

if ar[0] in allobs:
    ob_act = Blender.Object.Get(ar[0])

I don’t know if this is the best way, but that’s what came to my head right away.

thx for the fast reply. i tried it out but it does not work with me.

when i have an object called ‘Cube’ it is stored as ‘Object “Cube”’ in the allob list and i cannot test for it with either ‘Cube’ or ‘Object “Cube”’. how do i test for the cube?

something else: i would think that this is a very slow procedure as it has to go through all objects (which in my case can be thousands) every time i need to add an object from my parse file (again thousands). so if there is a solution where I can check for a specific object right away i would appreciate it.

Hmmm, when the scene has an object that is stored as “Cube” in the object list, you can indeeed get it like this:
ob_act = Blender.Object.Get(“Cube”)

If this doesn’t work for you, I would need some more of your script code to see what’s wrong, and also the error message.

EDIT: I just realize I might have misunderstood you. I’ll write up an example of what works for me.

EDIT2: You’re right, it doesn’t work that way. Dunno what I was thinking, sorry. allobs is a list ob Blender objects, and ar[0] is a string, probably, so testing can’t work. We would have to test against the name of the object in the list, but that would involve more looping and will take even longer, so my try…except suggestion below should be a better approach.

i would think that this is a very slow procedure

Yes, that was my fear also. Well, another thing you could use is the try…except construct in Python:

try:
    ob_act = Blender.Object.Get(ar[0])
except:
    print "Object", ar[0], "doesn't exist"

Double post

thanks alot sanne, this seems to work just fine :slight_smile:

You’re welcome lightning2911, glad you got it working. :slight_smile: