PyNode and Attribute exist

1,394 views
Skip to first unread message

King

unread,
Mar 14, 2009, 6:17:09 AM3/14/09
to python_inside_maya
Hi,

node = PyNode("pCubeShape1")
if node.nullattrib.exists():
print "yes"


and there is no attribute on "pCubeShape1" named "nullattrib". It
should return false. Instead I am getting an error:

# Error: Mesh(u'pCubeShape1') has no attribute or method named
'nullAttrib'
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# File "D:\maya2008\Python\lib\site-packages\pymel\core
\nodetypes.py", line 3276, in __getattr__
# return DependNode.__getattr__(self,attr)
# File "D:\maya2008\Python\lib\site-packages\pymel\core
\nodetypes.py", line 1931, in __getattr__
# raise AttributeError,"%r has no attribute or method named '%s'"
% (self, attr)
# AttributeError: Mesh(u'pCubeShape1') has no attribute or method
named 'nullAttrib' #


AFAIK this method was working previously. Is it changed? What's the
new pymel way of checking an attribute existence?

Cheers

Prashant

Chad Dombrova

unread,
Mar 14, 2009, 2:32:22 PM3/14/09
to python_in...@googlegroups.com
check the docs under Non-Backward compatible changes:  http://pymel.googlecode.com/svn/docs/index.html



this is pasted from the docs ( not sure how much google-groups will butcher it ):

Non-Existent Objects

Previous versions of PyMEL allowed you to instantiate classes for non-existent objects. This was common practice for testing object existence using PyNode.exists and could also be useful in circumstances where you wished to use name formatting methods.

Starting with this version, an exception will be raised if the passed name does not represent an object in the scene. This has several advantages:

  1. you will never unknowingingly attempt to use a node or attribute that does not exist, either due to a typo or unexpected context
  2. it brings PyMEL's attribute handling more in line with pythonic rules, where attributes must exist before accessing them
  3. it prevents the awkward situation of having a python object for which only a handful of methods will actually work

The side-effect, however, is that certain conventions for existence testing are no longer supported, while new ones have also been added. 

Compatibility Mode

We realize this is a big change so we have provided an option in the new pymel.cfg file ( found in the root of the PyMEL directory ) called0_7_compatibility_mode. When enabled, this option causes PyMEL to treat non-existent objects in a similar fashion to version 0.7.x:

>>> x = PyNode( 'foobar' ) 
>>> x
DependNodeName('foobar') # doctest: +SKIP
>>> x.exists()
False

The object returned is not a subclass of PyNode, but rather of other.NameParser. For more information see Manipulating Names of Non-Existent Objects. Also, keep in mind that the behavior of "compatibility mode" is deprecated and will not be supported in PyMEL 1.0 ( unless, of course, there is strong public support to keep it ).

New Exceptions

We've added three new exceptions which can be used to test for existence errors when creating new PyNodes: MayaObjectErrorMayaNodeError, andMayaAttributeError.

>>> for x in [ 'fooBar.spangle', 'superMonk' ] :
...     try:
...         PyNode( x )
...         print "It Exists"
...     except MayaNodeError:
...         print "The Node Doesn't Exist:", x
...     except MayaAttributeError:
...         print "The Attribute Doesn't Exist:", x
...
The Attribute Doesn't Exist: fooBar.spangle
The Node Doesn't Exist: superMonk

Both exceptions can be caught by using the parent exception MayaObjectError. In addition MayaAttributeError can also be caught with the builtin exception AttributeError.

Note that you will get different exceptions depending on how you access the attribute. This is because the shorthand notation can also be used to access functions, in which case the MayaAttributeError does not make sense to raise. As mentioned above, you can always use AttributeError to catch both.

Explicit notation:
>>> x = polySphere(name='earth')[0]
>>> x.attr('myAttr')
Traceback (most recent call last):
    ...
MayaAttributeError: Maya Attribute does not exist: u'earth.myAttr'
Shorthand notation:
>>> x = polySphere(name='moon')[0]
>>> x.myAttr
Traceback (most recent call last):
    ...
AttributeError: Transform(u'moon') has no attribute or method named 'myAttr'

Testing Node Existence

No longer supported:
>>> if PyNode( 'fooBar' ).exists(): 
...     print "It Exists"
... else:
...     print "It Doesn't Exist"
It Doesn't Exist
Still supported:
>>> if objExists( 'fooBar' ):
...     print "It Exists"
... else:
...     print "It Doesn't Exist"
It Doesn't Exist
New construct:
>>> try:
...     PyNode( 'fooBar' )
...     print "It Exists"
... except MayaObjectError:
...     print "It Doesn't Exist"
It Doesn't Exist

Testing Attribute Existence

No longer supported:
>>> if PyNode( 'persp.spangle' ).exists(): 
...     print "Attribute Exists"
... else:
...     print "Attribute Doesn't Exist"
Attribute Doesn't Exist
No longer supported:
>>> x = PyNode('persp')
>>> if x.spangle.exists(): 
...     print "Attribute Exists"
... else:
...     print "Attribute Doesn't Exist"
Attribute Doesn't Exist
Still supported:
>>> if objExists( 'persp.spangle' ):
...     print "Attribute Exists"
... else:
...     print "Attribute Doesn't Exist"
Attribute Doesn't Exist
New construct:
>>> x = PyNode('persp')
>>> if x.hasAttr('spangle'):
...     print "Attribute Exists"
... else:
...     print "Attribute Doesn't Exist"
Attribute Doesn't Exist
New construct:
>>> try:
...     PyNode( 'persp.spangle' )
...     print "Attribute Exists"
... except MayaAttributeError:
...     print "Attribute Doesn't Exist"
Attribute Doesn't Exist
New construct:
>>> x = PyNode('persp')
>>> try:
...     x.spangle
...     print "Attribute Exists"
... except AttributeError:
...     print "Attribute Doesn't Exist"
Attribute Doesn't Exist

Other PyMEL Idioms

Two other PyMEL idioms have been removed as a result of this change. Attribute.add has been removed because, the attribute has to exist in order to successfully get an Attribute instance. So instead you have to use the addAttr method on the node:

No longer supported:

>>> PyNode('persp').myNewFloatAttr.add( at=float ) 

Still supported:

>>> PyNode('persp').addAttr( 'myNewFloatAttr', at=float )

Similarly, the force flag for setAttr functions, which creates the attribute before setting if it does not exist, can only be safely used from the node class and not the attribute class:

No longer supported:

>>> PyNode('persp').myNewIntAttr.set( 2, force=1 ) 

Still supported:

>>> PyNode('persp').setAttr( 'myNewIntAttr', 2, force=1 )

New construct:

>>> PyNode('persp').setDynamicAttr( 'myNewIntAttr', 2 )

Manipulating Names of Non-Existent Objects

One advantage of the old way of dealing with non-existent objects was that you could use the name parsing methods of the PyNode classes to manipulate the object's name until you found what you were looking for. To allow for this, we've added several classes which operate on non-existent nodes and contain only methods for string parsing and existence testing. These nodes can be found in the other module and are namedother.NameParserother.AttributeNameother.DependNodeName, and other.DagNodeName.

Chad Dombrova

unread,
Mar 14, 2009, 2:44:19 PM3/14/09
to python_in...@googlegroups.com
p.s.

i'm in the process of updating the docs online, so they might go down.
Reply all
Reply to author
Forward
0 new messages