Grabbing object name

1,228 views
Skip to first unread message

yann19

unread,
Nov 12, 2014, 3:46:29 AM11/12/14
to python_in...@googlegroups.com
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?

Justin Israel

unread,
Nov 12, 2014, 3:52:37 AM11/12/14
to python_in...@googlegroups.com
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.

--
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.

yann19

unread,
Nov 12, 2014, 3:57:01 AM11/12/14
to python_in...@googlegroups.com
Hey there..

Will this means that I have to use this Maya API thingy?
Just googled online and it looks like a foreign language to me :O



On Wednesday, November 12, 2014 4:52:37 PM UTC+8, Justin Israel wrote:
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.

yann19

unread,
Nov 12, 2014, 4:20:06 AM11/12/14
to python_in...@googlegroups.com
I searched and tried out a few more stuff and manage to get the name of the object selection by doing the following:
for item in sel:
   
print item

Was wondering if this is acceptable in Maya, despite I am getting the output I wanted?

Marcus Ottosson

unread,
Nov 12, 2014, 4:41:56 AM11/12/14
to python_in...@googlegroups.com

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 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/db5e7ecc-e2d3-4553-b9f9-7861b8305308%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Marcus Ottosson

unread,
Nov 12, 2014, 4:49:09 AM11/12/14
to python_in...@googlegroups.com
For starting out, you may find more answers and help over at tech-artists.org and for help with Python in general, you may find a lot of information over at stackoverflow

As you just experienced, the questions in this group are typically a little more involved and may assume that you are already familiar with the basics of Python and Maya.
--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Nov 12, 2014, 5:25:26 AM11/12/14
to python_in...@googlegroups.com
Also you may want to try some standard python learning material, such as http://learnpythonthehardway.org

This mailing list is also a great source for focused questions, but it would be a good idea to do some extra training on the basics so that you don't get too confused up front when delving into an API

yann19

unread,
Nov 12, 2014, 5:34:34 AM11/12/14
to python_in...@googlegroups.com
Hey Marcus,

The example you have listed out is indeed what I am looking for and +1 to the detailed explanation.
I will check out the above 2 links you have put up to get a better understanding between Maya and python.

Thanks mate!

yann19

unread,
Nov 12, 2014, 5:36:54 AM11/12/14
to python_in...@googlegroups.com
Hey Justin, the material in the link seems like a good starting point for me as well.

I will check that out too.

Thanks!
Reply all
Reply to author
Forward
0 new messages