PDA

View Full Version : Checking if an object exists


Hugh
03-22-2003, 04:57 PM
I'm writing a script at the moment that will check whether certain objects exist in the current scene....

I'm currently doing it with the following procedure:

global proc int findObject(string $name)
{
select -r $name;
$object = `ls -sl`;
if(size($object) == 0)
{
print("Failed to find "+$name+"\n");
return 0;
}
else
return 1;
}

However, if it finds that an object doesn't exist, the select command gives an error, and no more of the script is executed....

Is there any way of getting around this without writing my own stuff to go through all of the objects in the scene?

X-Tender
03-22-2003, 06:21 PM
try this ...


if (`exists $obj`)
{
//do funny stuff
};


$obj is the object that you wanna ask if it exist :cool:

Hugh
03-22-2003, 06:58 PM
ah - okay - I'll give that a go...

When I looked in the MEL reference, it said that 'exist' was for checking if a function existed....

X-Tender
03-22-2003, 07:20 PM
oh damn, you're right ... i must lern to read the description first =D ...

but try this =D

objExists $objectname;

thats works .. you get 0 back if it not exist or 1 if it exist .. have fun ..

Hugh
03-22-2003, 07:24 PM
thanks!

I did go the route you thought at first...

What would be great would be to somehow catch the error that select gives out...

Edit: Nice - thanks - I hadn't come across that one! Thanks for your help...

klod
03-24-2003, 08:23 AM
with objExists, you can also check if an attribute exists on a node:

objExists ($obj + ".myAttr")

Klod

CGTalk Moderation
01-14-2006, 05:00 PM
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.