there are several fundamental misunderstandings demonstrated in that  
code snippet that need to be addressed.


1.  99.99% of the time, calling the "python" command from python is  
not necessary. honestly, i'm not even sure why Autodesk included the  
command in maya.cmds, since A) that command was created to allow mel  
to execute python code, B) python already has "eval", and C) there are  
many commands in maya.cmds that were left out because they reproduce  
functionality that is already in python ( tokenize, for example).   
seeing this makes me think we should filter it from the pymel namespace.

2. as dean pointed out, to get an attribute using maya.cmds you would  
do the following:

cmds.getAttr( nodeName + '.' + attributeName )

sure, you could wrap this in another function, like so:

def getNodeAttribute( nodeName="", attributeName="" ):
     cmds.getAttr( nodeName + '.' + attributeName )

but what are you really buying yourself?  now you can use this in your  
code?

getNodeAttribute( nodeName, attributeName )

it's not really any more concise or any more readable.  wrapping a  
single line of code in a function only makes sense if that single line  
was a convoluted operation involving the daisy-chaingin of multiple  
methods or a list comprehension loop.  the reason for using pymel is  
that it is object oriented and that it produces more readable code. in  
this context, it makes even less sense to wrap this in a function.

3. there are several ways to get an attribute on one line using pymel:

PyNode('persp').attr('focalLength').get()
PyNode('persp').focalLength.get()
Attribute( 'persp.focalLength' ).get()
getAttr(  'persp.focalLength' )

( as paul pointed out, you can still uses the "getAttr" command in  
pymel when you want.  )

the PyNode and Attribute classes are used to cast a string to a pymel  
object, but this operation is not usually necessary because almost  
every command that creates or lists nodes/attributes in pymel already  
returns them as pymel objects.  a more realistic code snippet might  
look like this:

focalLengths = []
for cam in ls(type='camera'):
        focalLengths.append( cam.focalLength.get() )

hopefully you can now see that using another function to achieve this  
'get' operation is not really necessary.

also, as a general rule of thumb:  if you are adding or parsing  
strings to achieve a maya-specific operation using pymel you are  
probably doing something wrong.  working with maya objects as strings  
is a MEL paradigm that pymel tries very hard to eliminate by providing  
methods for achieving the most common name-based operations. ( the  
primary exception that comes to mind is creating expressions )

-chad




On Aug 16, 2009, at 5:15 AM, King wrote:

>
> def getNodeAttribute( nodeName="", attributeName="" ):
>
>    node = PyNode( nodeName )
>    cmnd = ("node."+attributeName+".get()")
>    result = python(cmnd)
>    return result
>
>
> Pymel way of getting an attribute's value is much easier. The code
> above is having some problem.
>
> Prashant
> >


--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

Reply via email to