View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
exists() function in R Programming Language is used to check if an object with the names specified in the argument of the function is defined or not. It returns TRUE if the object is found.
Syntax: exists(name)
Parameters:
- name: Name of the Object to be searched
exists() Function in R Language Example
Example 1: Apply exists() Function to variable
R
exists("cos")
exists("diff")
exists("Hello")
Output:
[1] TRUE [1] TRUE [1] FALSEExample 2: Apply exists() Function to Vector
R
Hello <- c(1, 2, 3, 4, 5)
exists("Hello")
Output:
[1] TRUEIn the above examples, it can be seen that when there was no object named “Hello” defined in the first code, the exists() function returned FALSE, whereas after the declaration of the “Hello” Object, the exists() function returned TRUE. This means that the exists() function checks for pre-defined and user-defined, objects.
Example 3: Check if Variable in Data Frame is Defined
R
friend.data <- data.frame(
friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),
stringsAsFactors = FALSE
)
print(friend.data)
attach(friend.data)
exists('friend_id')
Output:
friend_id friend_name 1 1 Sachin 2 2 Sourav 3 3 Dravid 4 4 Sehwag 5 5 Dhoni TRUE
unread,
Dec 3, 2015, 7:29:04 PM12/3/15
to Python Programming for Autodesk Maya,
Hi all,
Am new to maya swiching from XSI and am learning mel and python for now.
Am trying to check if multiple object are in the scene but it seems that when I use "objExists" I
can only test if one object exist....
Can anyone help me on this.
The code would go in my mind something like this
if ( `objExists Object1, Object2, ...` )
{
print "All object exist";
} else {
print "Your missing some objects";
}
Thanks in advance!
Justin Israel
unread,
Dec 3, 2015, 9:55:55 PM12/3/15
to Python Programming for Autodesk Maya,
Hi,
Is your goal to just find out if any of the given objects exist? or all of the objects exist? To you want be able to handle the case where some exist and some dont?
The `ls` command can handle being given a list of names and returning those that exist:
if cmds.ls(["foo", "bar", "baz"]): print "at least one exists"
Or if you want to ensure all objects exist:
names = ["foo", "bar", "baz"] found = cmds.ls(names) if len(found) == len(names): print "all objects exist"
Justin
Carlos Abrego
unread,
Dec 4, 2015, 1:30:59 AM12/4/15
to Justin Israel, Python Programming for Autodesk Maya
Thanks a lot Justin it really helps.
I don t need it right now but for sure would be nice to seee how you handle the case where some object exist and some don't.
For now, let see if am able to convert that to mel.
Thanks a lot again
Carlos
Marcus Ottosson
unread,
Dec 4, 2015, 2:17:25 AM12/4/15
to , Justin Israel
Now why would one want to convert anything to MEL?
Since you mention that you are new to Maya, I'll just mention that MEL is the predecessor to Python and code is typically converted the other way around. The one (only?) reason to stick with MEL is if you have old code that you cannot change and need to maintain or update; MEL itself doesn't offer anything that Python doesn't do better. However this doesn't seem to be the case if you are just switching to Maya.
Justin Israel
unread,
Dec 4, 2015, 2:37:25 AM12/4/15
to Carlos Abrego, Python Programming for Autodesk Maya
Thanks a lot Justin it really helps.
I don t need it right now but for sure would be nice to seee how you handle the case where some object exist and some don't.
Sure. The easiest way to do it is to loop over each name and call objExists(). If we had a massive list of items and wanted to reduce the maya calls, we could do something more complicated with a single call to ls, but that is for another time...
items = ["foo", "biz", "baz"] for item in items: if cmds.objExists(item): print item, "exists" else: print item, "does not exist"