View Full Version : "If" statements and Selection Objects.

03 March 2011, 09:08 PM
Quick question for someone starting to get into Python scripting, I'm trying to get the data of the name of the object I am currently selecting (the name of the object is theShape) so that the code below runs correctly.

if Application.Selection(0) == "theShape":
print "What I'm Looking For!"
print "Not really"

When I run this sequence, I will always get the False output. I'd like to be able to manipulate the code so I can get the True output. Can anyone explain/offer a solution to this?

Thanks for your time!

03 March 2011, 06:52 PM
Try this instead:
if Application.Selection(0).Name == "theShape":
print "What I'm Looking For!"
print "Not really"

Selection(0) returns an object, which is basically a collection of properties (i.e. data) and methods (i.e. code). Objects always have types, and it's only valid to compare objects against other objects of the same type.

In your code snippet you're trying to compare an object of type X3DObject to an object of type string, which doesn't make sense. Python's behavior in this case is to classify the object as unequal instead of raising an error.

Now, the X3DObject does have a Name property, which is of type string. Comparing against that works properly.

This might not make much sense if you're not familiar with scripting and programming. I wish I could suggest a book or a reference you could consult on this topic, but my information on introductory material like that is kind of outdated.

Good luck, though :)

03 March 2011, 02:45 PM
This worked great! Thanks, I actually am beginning to understand what you said and what you did to make the code work.

Thanks a ton!

03 March 2011, 03:03 AM
Hopefully you don't mind me butting in. If you are looking for a good introduction book on Python programming I suggest this one:

Learning Python ( Powerful Object-Oriented Programming (

I think you might be able to get it even cheaper off of the publisher's website (digital download pdf as I recall).

Take care!


04 April 2011, 10:52 PM
It appears the book FistOfGod recommended is not all that good, based on the reviews.

I can tell you from my own experience, if you are learning for the first time, that John Zelle has an excellent book on programming principles using Python. (

My move towards Python, as an artist, has significantly increased my understanding of logical structures and other finer details of programming, and this book was a key part of my learning.

04 April 2011, 06:56 PM
Thank you everyone for your responses. I'll be sure to check out all of the material that has been recommended. At the time of this posting I have now written several successful IK/FK switching and matching scripts with Python. I hope to continue to learn more to the point where I can make auto-rigging tools for the more common biped structures.

Thanks again!

CGTalk Moderation
04 April 2011, 06:56 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.