"If" statements and Selection Objects.


#1

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!"
else:
	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!


#2

Try this instead:

if Application.Selection(0).Name == "theShape":
   print "What I'm Looking For!"
  else:
   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 :slight_smile:


#3

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!


#4

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!

Waylon


#5

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.


#6

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!


#7

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.