Ok, a few things:

The problem with the code that you're giving is one of scope - the
'python' command is complaining that "node" isn't defined.  There may
be a way to fix this using the python command, but I wouldn't bother -
maya.cmds.python is mainly there for invoking python from MEL - if
you're evaluating python commands from python, use the eval function
or the exec command instead.  They have the nice property (or at least
the eval statement does - not sure about exec, but I think so) of
inheriting the current scope by default, and so will recognize the
"node" variable.  So to get your function to work, you'd make this
change:

   result = eval(cmnd)

However, while this will work, it's probably not the best approach.
As Dean points out, if you're strictly dealing with strings, the
simplest way to go about this is to not use pymel at all - one of the
strengths of pymel is that you can work with objects, not strings, but
in your sample code, you're really just doing string processing, so
you may as well stick with maya.cmds.

Even so, if you want to use pymel - say, for instance, you want your
function to accept either pymel objects OR string names, something
which is frequently handy if you're using a lot of pymel - there's
still better ways of going about it.  Such as:

def getNodeAttribute( node, attributeName ):
   node = PyNode( node )
   return node.attr(attributeName).get()

The biggest difference here is the use of the .attr() method, which is
the preferred way of accessing a pymel attribute from a string name.
The reason why it's better is because if "attributeName" happens to be
the name of some method on the node, then if try

    node.attributeOrMethodName

then you will get the METHOD returned, not the maya attribute.  So if
you want to be absolutely sure you get the attribute, you have to use

    node.attr("attributeOrMethodName")

- Paul

On Sun, Aug 16, 2009 at 9:16 AM, Dean Edmonds<[email protected]> wrote:
>
> On Sun, Aug 16, 2009 at 05:15, King<[email protected]> 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.
>
> This seems a lot lot simpler to me:
>
> import maya.cmds as cmds
>
> def getNodeAttribute(nodeName, attrName):
>  return cmds.getAttr(nodeName + '.' + attrName)
>
> --
> -deane
>
> >
>

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

Reply via email to