Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

issue using cmds.getAttr()

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
2339 Views, 5 Replies

issue using cmds.getAttr()

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

 

Tags (3)
5 REPLIES 5
Message 2 of 6
kevin.picott
in reply to: Anonymous

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
Message 3 of 6
Anonymous
in reply to: kevin.picott

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

Message 4 of 6
kevin.picott
in reply to: Anonymous

"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
Message 5 of 6
Anonymous
in reply to: kevin.picott

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

Message 6 of 6
kevin.picott
in reply to: Anonymous

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report