sel = cmds.ls (selection = True)
print str(sel)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3d317cc5-4a5f-424a-9cb3-7ac5af4c040a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hi,
You are doing it correctly. What you are getting is a list of string (Unicode actuslly) results. The Maya commands api, just like the MEL counterpart, uses string paths to all object references.
If you are expecting a 1st node object, then you are probably thinking of the PyMel api which is an abstraction over the native Maya API to provide something more pythonic and object oriented.
The Maya API (derived from the C++ counterpart) also is more object oriented that the commands Mel api.
Basically, if you use the Python commands api, you pass around strings.
On Wed, 12 Nov 2014 9:46 PM yann19 <yang...@gmail.com> wrote:
Hi all, I just started learning Python and currently I am confused about some stuff in Maya. Please do bear with me and my noob questions--
I am selecting on an object in which it is called pCube1 and so I run the following code:sel = cmds.ls (selection = True)
print str(sel)
However, here I thought I am supposed to get the output result as pSphere1 in my editor but yet I am seeing [u'pSphere1'] instead.
Or am I doing it in the wrong way to grab the object name?
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
for item in sel:
print item
I think what you are looking for, yann19, is this.
sel = cmds.ls (selection = True)
print sel[0]
The [0] at the end of sel is an index, as sel is a Python list and 0 is the first item in that list. If you have one item selected, it will be located at 0, and every other object after that, such as 1, 2 and so on.
When you do str(sel) you are printing the full list, as a Python string, which is why you are seeing the brackets, [ and ] along with u'pSphere1'.
The u at the start of pSphere indicates that pSphere is a special type of string, called unicode, hence the letter “u”. But you won’t need to pay much attention to that when just starting out and may think of it as any other string.
With the code above, if you have nothing selected, you will however get an error, an IndexError, because the list will be empty. To protect your program against that, you could do a try/except.
try:
print sel[0]
except IndexError:
print "Nothing selected"
Good luck!
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/db5e7ecc-e2d3-4553-b9f9-7861b8305308%40googlegroups.com.To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBo5zfwx%2BPgBLRXTYY2jXe9v0ADe9GBC8JhCnRUvRXUmw%40mail.gmail.com.