issue using cmds.getAttr()

Anonymous

issue using cmds.getAttr()

Anonymous
Not applicable

Hi guys!

 

I am trying to make a python script that gives me back the translate attribute of my maya selected objetcs.

This is what I have for now (only for the selected geometry on my maya scene).

 

if cmds.ls(geometry=True):

    selectedObjects= cmds.ls(sl=True)

    for item in selectedObjects:

        translate = cmds.getAttr(item.translate)

 

I am getting the following error;

# Error: TypeError: file <maya console> line 4: Object <built-in method translate of unicode object at 0x00000000A0A106F0> is invalid #

 

Same happens if I use a string instead of unicode.

 

I am trying to follow the maya docs but I can't spot my error for now.

If someone has used this command previously and can help me would be much appreciatted.

 

Thanks,

 

Joan

 

0 Likes
Reply
2,309 Views
5 Replies
Replies (5)

kevin.picott
Alumni
Alumni

You are trying to use a string as a Python object. cmds.ls() returns a list of strings. What you are intending on doing is probably this:

 

if cmds.ls(geometry=True):
    selectedObjects= cmds.ls(sl=True)
    for item in selectedObjects:
        translate = cmds.getAttr('{}.translate'.format(item))


Kevin "Father of the DG" Picott

Senior Principal Engineer
0 Likes

Anonymous
Not applicable

Hi Kevin,

 

Thanks for your reply.

Great that is just what I wanted.

Let's see if I understood how it works, thanks in advance for helping.

cmds.getAttr expects a python object and I'm giving a list, then with your code,

 

 cmds.getAttr('{}.translate'.format(item)

 

I'm giving {} as the python object I want to have the attribute, isn't that an empty dictionary?

And what does format(item) do? Sorry I'm looking for format in the cmds.getAttr flags but I don't find it.

 

Many thanks in advance.

 

Joan

0 Likes

kevin.picott
Alumni
Alumni

"format" is a method on the string object. In this context '{}.translate'.format(item) is a string, and is the same as '%s.translate'%item. Which of the two you use is a matter of style - I use "format" because it performs automatic string conversion on complex types. In a format string "{}" doesn't refer to a dictionary, it's a placeholder for the argument to be formatted. To be strictly correct you would say '{0}.translate'.format(item) because the placeholder is positional, in order to support different languages.

 

For example if item='MyTransform' then these four statements are all equivalent:

 

 

cmds.getAttr( '{}.translate'.format( item ) )
cmds.getAttr( '{0}.translate'.format( item ) )
cmds.getAttr( '%s.translate' % item )
cmds.getAttr( 'MyTransform.translate' )

 

 



Kevin "Father of the DG" Picott

Senior Principal Engineer
0 Likes

Anonymous
Not applicable

Hi Kevin,

 

I think understand what you are explaining. Apologies my coding knowledge it still needs a lot of improvement.

To try different things I am trying now to make a loop over my geo objects that gives me back the translate values of these objects.

 

 

geoObjects= cmds.ls(geometry=True)
for item in geoObjects:
    translate = cmds.getAttr('{}.translate'.format(item))

 

Which is giving me back,

 

# Error: ValueError: file <maya console> line 3: No object matches name: pSphereShape1.translate

 

Sorry I can't understand what I am doing wrong here, would not be this the way to get the translate values? Is not that the way i should be calling the object?

Thanks,

 

Joan

0 Likes

kevin.picott
Alumni
Alumni

The translate attribute belongs to nodes of type "transform" and you are walking nodes of type "shape". In order to get the translation you have to walk up to the parents:

geoObjects = cmds.ls(geometry=True)
for geoItem in geoObjects:
    for parent in cmds.listRelatives( geoItem, allParents=True ):
        translate = cmds.getAttr('{}.translate'.format(parent))


Kevin "Father of the DG" Picott

Senior Principal Engineer
0 Likes