PDA

View Full Version : how to query if an object exists


rusted_nut
08-07-2003, 09:51 PM
I'm trying to write an If-else statement based on wether or not an object exists, but I cannot find a command that will query an objects existence.

I tried:

if (`exists Rt_armJNT` == true) //do stuff

exists does not apply to objects. Any ideas would be appreciated.

mark_wilkins
08-07-2003, 10:00 PM
string $matchingNodes[] = `ls Rt_armJNT`;

// The following relies on Maya treating integers
// that are nonzero as true and zero as false,
// so if size($matchingNodes) is more than one
// it's true and if it's 0 it's false:

if (size($matchingNodes)) {
// if you get here, Rt_armJNT exists
}
else {
// if you get here, it doesn't
}


Note that if you had the name of a node for which you wanted to test in a string variable, you could do it like this:


string $mynodename = "Rt_armJNT";
string $matchingNodes[] = `ls $mynodename`;

if (size($matchingNodes)) {
// if you get here, the node whose name is in $mynodename exists
}
else {
// if you get here, it doesn't
}

mhovland
08-07-2003, 10:47 PM
Try this

if(`objExists Rt_armJNT`)
{
//do stuff here
}
else
{
warning ("object Rt_armJNT does not exist in the scene.");
}

rusted_nut
08-07-2003, 11:09 PM
thanks gentlemen,

your responses are appreciated

mark_wilkins
08-08-2003, 12:41 AM
Heh. I guess most of the time I'm checking for the existence of a number of objects and I get the list at the same time with a wildcard like


string $myJoints[] = `ls "*JNT"`;


so I just plain forgot about the objExists command.

You can use wildcards in objExists also, if that's useful or convenient for you:


if (`objExists "*JNT"`) {
//whatever
}


-- Mark

klod
08-08-2003, 08:09 AM
hi,

yes, objExists is a nice function.

I use it also to check if an attribute exists:

string $myNode = "justANode";
if (`objExists ($myNode + ".myAttribute")`)
// whatever
else
// whatever too


Klod

CGTalk Moderation
01-15-2006, 08: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.