View Full Version : [Python] Check if exist else do

08 August 2012, 08:22 AM

Trying to figure out how to check if something exist in my scene (on loop) and then if it does not then create bla bla bla.

In any case I have this code from codeacademy that I'm studying.

#Write your two "elif" statements below!
answer = "I like Spam!"

def feelings_about_spam():
if answer == "I hate Spam!":
return False
elif answer =='I like Spam!': #Fill in your `elif` statement here!
return True #Make sure `elif` returns `True`
else: answer != 'I like Spam!':
return False

that kinda makes sense. I just cant figure out how to check if something exist by name/type/layer etc...

in any case heres my test...

LTest =
print LTest
# listing lights by type
if Ltest:
#if they exist just print, not sure if thats correct how do I check if it exist?
print 'lights':
return True
# from now on I think its correct but maybe not :)
elif cmds.spotLight():
return True
else: print 'test'

Thanks any1 that come to help in advice! :)

08 August 2012, 08:50 AM
you rarely need to check if something exists right after you have use a list command to list it. If a list command returns it, then it must exist. Obviously if you are iterating over items and running some kind of process that may change names or delete items then it's useful.

Either way, to check if a given node exists you simply use the objExists command
lights =
if lights:
for item in lights: # iterate over each item in the list
if cmds.objExists(item):
print 'yes, the light %s still exists!' % item
obviously this code is pointless in this bare state because the fact the light is in the list proves it exists, but it should give you the info you require.

CGTalk Moderation
08 August 2012, 08:50 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.