SCENE.node syntax with heirarchy?

32 views
Skip to first unread message

Matt Estela

unread,
Mar 27, 2014, 8:56:24 PM3/27/14
to python_inside_maya
If I'm being lazy for quick 5 line scripts I'll use something like

important_thing = SCENE.hero_curve_shape

I've had a few occasions where the thing I want is a few groups down, and I haven't been able to use the 'SCENE.' syntax. 

Anyone know what the trick is?

Eg, I have locator1, and a|b|locator1:

SCENE.locator1
# Error: MayaNodeError: file C:\Program Files\Autodesk\Maya2014\Python\lib\site-packages\pymel\core\general.py line 1754: Multiple maya Nodes existed: u'locator1' # 

SCENE.a|b|locator1
# Error: NameError: file <maya console> line 1: name 'b' is not defined # 

SCENE.'a|b|locator1'
# Error: invalid syntax # 

Marcus Ottosson

unread,
Mar 28, 2014, 3:03:49 AM3/28/14
to python_in...@googlegroups.com
SCENE.locator1 would return an instance of a PyNode which no longer provides you with the dot-notation for grabbing children, so daisy-chining them wouldn't work.

>>> SCENE.a.b.locator1  # <-- won't work

The '|' is a bitwise operator and unless implemented will return a bitwise OR from its left- and right-hand sides.

>>> SCENE.a | b | locator1  # <-- won't work, b isn't defined and | wouldn't know what to do with it if it was

As for duplicate names.. the only possibly solution SCENE could offer you, other than throw an exception, would be to hide duplicates and only return one of the possible matches. This would lead to some unpredictable code on your end.

Why not stick with pm.PyNode('|a|b|locator1')?

To make it hide duplicates, you could do:

def SCENE(name):
    single_result = (pm.ls(name) + [None])[0]
    return pm.PyNode(single_result) if single_result else None

>>> SCENE('locator1')

Or

SCENE = lambda name: pm.PyNode((pm.ls(name) + [None])[0])

>>> SCENE('locator1')

Or

class SCENE(object):
   def __getattr__(self, name):
        single_result = (pm.ls(name) + [None])[0]
        return pm.PyNode(single_result) if single_result else None

>>> SCENE.locator1


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAGCWdhki%2BzyvaJhVwR%2BbK%3D71LPvSti-2xVJbo80XC6_w23C5qg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Matt Estela

unread,
Mar 28, 2014, 3:06:01 AM3/28/14
to python_inside_maya
"Why not stick with pm.PyNode('|a|b|locator1')?"

Cos I'm lazy, that's why.  ;)

Actually that'll work fine for my needs, quick enough to put in a throwaway script, easy enough to type. Thanks for the explanation and tip Marcus!

-matt


Reply all
Reply to author
Forward
0 new messages