Finding object in scene by name

5,997 views
Skip to first unread message

Eric Thivierge

unread,
Mar 19, 2014, 10:54:37 AM3/19/14
to python_in...@googlegroups.com
Hello all,

This may seem like a simple question but my Googling didn't return any
useful results.

I have a name of an object 'pCube1'. I need to find it in the Maya
scene. Problem is, I may have multiple objects in the scene that are
named the same thing. Even in the same hierarchy (Crazy that Maya lets
you do this btw). Is there a way to get a list of all objects that are
named 'pCube1'? Once I get that list I need to find the top most parent
in the scene. From there I can find the one I need.

When I try to listRelatives, it throws an error saying there is more
than one node int he scene that is name 'pCube1'.

# PyMel
print pm.objExists('pCube1')

parents = pm.listRelatives('pCube1', allParents=True)
for eachItem in parents:
print eachItem.name()

Thanks,
Eric T.

damon shelton

unread,
Mar 19, 2014, 10:59:10 AM3/19/14
to python_in...@googlegroups.com

cmds.ls ('*pCube1', long=True)

--
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_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/5329AFAD.5080403%40hybride.com.
For more options, visit https://groups.google.com/d/optout.

Eric Thivierge

unread,
Mar 19, 2014, 11:00:45 AM3/19/14
to python_in...@googlegroups.com, damon shelton
Hah. Well, yeah... thanks.

On Wednesday, March 19, 2014 10:59:10 AM, damon shelton wrote:
> cmds.ls <http://cmds.ls> ('*pCube1', long=True)
>
> On Mar 19, 2014 7:54 AM, "Eric Thivierge" <ethiv...@hybride.com
> <mailto:ethiv...@hybride.com>> wrote:
>
> Hello all,
>
> This may seem like a simple question but my Googling didn't return
> any useful results.
>
> I have a name of an object 'pCube1'. I need to find it in the Maya
> scene. Problem is, I may have multiple objects in the scene that
> are named the same thing. Even in the same hierarchy (Crazy that
> Maya lets you do this btw). Is there a way to get a list of all
> objects that are named 'pCube1'? Once I get that list I need to
> find the top most parent in the scene. From there I can find the
> one I need.
>
> When I try to listRelatives, it throws an error saying there is
> more than one node int he scene that is name 'pCube1'.
>
> # PyMel
> print pm.objExists('pCube1')
>
> parents = pm.listRelatives('pCube1', allParents=True)
> for eachItem in parents:
> print eachItem.name()
>
> Thanks,
> Eric T.
>
> --
> 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_ma...@googlegroups.com
> <mailto:python_inside_maya%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/__msgid/python_inside_maya/__5329AFAD.5080403%40hybride.com
> <https://groups.google.com/d/msgid/python_inside_maya/5329AFAD.5080403%40hybride.com>__.
> For more options, visit https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>.
>
> --
> 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
> <mailto:python_inside_m...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAM9RXoLb37cQR4QB8R6eshN1PmT95n3uGY9qJicLtHFoEhQjLw%40mail.gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAM9RXoLb37cQR4QB8R6eshN1PmT95n3uGY9qJicLtHFoEhQjLw%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Marcus Ottosson

unread,
Mar 19, 2014, 1:12:25 PM3/19/14
to python_in...@googlegroups.com, damon shelton
In Maya, the hierarchy is included in a node's path but gets discarded most of the time and instead the artist is trusted to keep his names unique. The reason this is the case is because in Maya, there is no "hierarchy". Maya is inherently node-based; Softimage on the other hand does have the notion of a hierarchy.

Thus, a full path of a pCube1 may be resolve into '|a_group|another_group|pCube1', the | being the separator.

You can ask for full paths via cmds.ls(long=True), other commands have similar flags, such as cmds.listRelatives(fullPath=True) that ultimately do the same thing; concatenate the depth-first path of a node within a graph.

Additionally, Maya has the notion of namespaces, which encapsulates nodes similar to how Models encapsulates nodes in Softimage, however I would suggest sticking with hungarian notation at first as namespaces are tricky to understand at first.

Best,
Marcus




--
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
--
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_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/5329B11D.4040200%40hybride.com.

For more options, visit https://groups.google.com/d/optout.



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

Eric Thivierge

unread,
Mar 19, 2014, 1:44:07 PM3/19/14
to python_in...@googlegroups.com, damon shelton, Marcus Ottosson
Thanks Marcus,

The problem I'm facing currently is that I need to write data to a json
file that can be read in and interperated by both Softimage and Maya.
So I have to store object names consistently when exporting from
Softimage or Maya. I'm probably more in favor of mimicking the Maya
format at this point since, well you know why.

Thanks,
Eric T.

On Wednesday, March 19, 2014 1:12:25 PM, Marcus Ottosson wrote:
> In Maya, the hierarchy is included in a node's path but gets discarded
> most of the time and instead the artist is trusted to keep his names
> unique. The reason this is the case is because in Maya, there is no
> "hierarchy". Maya is inherently node-based; Softimage on the other
> hand does have the notion of a hierarchy.
>
> Thus, a full path of a pCube1 may be resolve
> into'|a_group|another_group|pCube1', the | being the separator.
>
> You can ask for full paths via cmds.ls <http://cmds.ls>(long=True),
> other commands have similar flags, such as
> cmds.listRelatives(fullPath=True) that ultimately do the same thing;
> concatenate the depth-first path of a node within a graph.
>
> Additionally, Maya has the notion of namespaces, which encapsulates
> nodes similar to how Models encapsulates nodes in Softimage, however I
> would suggest sticking with hungarian notation at first as namespaces
> are tricky to understand at first.
>
> Best,
> Marcus
>
>
> On 19 March 2014 15:00, Eric Thivierge <ethiv...@hybride.com
> <mailto:ethiv...@hybride.com>> wrote:
>
> Hah. Well, yeah... thanks.
>
> On Wednesday, March 19, 2014 10:59:10 AM, damon shelton wrote:
>
> cmds.ls <http://cmds.ls> <http://cmds.ls> ('*pCube1', long=True)
>
>
> On Mar 19, 2014 7:54 AM, "Eric Thivierge"
> <ethiv...@hybride.com <mailto:ethiv...@hybride.com>
> <mailto:ethiv...@hybride.com
> python_inside_may...@googlegroups.com
> <mailto:python_inside_maya%2B__uns...@googlegroups.com>
>
> <mailto:python_inside_maya%__2Buns...@googlegroups.com
> <mailto:python_inside_maya%252Buns...@googlegroups.com>__>.
>
> To view this discussion on the web visit
> https://groups.google.com/d/____msgid/python_inside_maya/____5329AFAD.5080403%40hybride.com
> <https://groups.google.com/d/__msgid/python_inside_maya/__5329AFAD.5080403%40hybride.com>
>
> <https://groups.google.com/d/__msgid/python_inside_maya/__5329AFAD.5080403%40hybride.com
> <https://groups.google.com/d/msgid/python_inside_maya/5329AFAD.5080403%40hybride.com>__>__.
> For more options, visit
> https://groups.google.com/d/____optout
> <https://groups.google.com/d/__optout>
> <https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>>.
>
>
> --
> 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_ma...@googlegroups.com
> <mailto:python_inside_maya%2Bunsu...@googlegroups.com>
> <mailto:python_inside_ma...@googlegroups.com
> <mailto:python_inside_maya%2Bunsu...@googlegroups.com>>.
>
> To view this discussion on the web visit
> https://groups.google.com/d/__msgid/python_inside_maya/__CAM9RXoLb37cQR4QB8R6eshN1PmT95__n3uGY9qJicLtHFoEhQjLw%40mail.__gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAM9RXoLb37cQR4QB8R6eshN1PmT95n3uGY9qJicLtHFoEhQjLw%40mail.gmail.com>
> <https://groups.google.com/d/__msgid/python_inside_maya/__CAM9RXoLb37cQR4QB8R6eshN1PmT95__n3uGY9qJicLtHFoEhQjLw%40mail.__gmail.com?utm_medium=email&__utm_source=footer
> <https://groups.google.com/d/msgid/python_inside_maya/CAM9RXoLb37cQR4QB8R6eshN1PmT95n3uGY9qJicLtHFoEhQjLw%40mail.gmail.com?utm_medium=email&utm_source=footer>>.
>
> For more options, visit https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>.
>
>
> --
> 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_ma...@googlegroups.com
> <mailto:python_inside_maya%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/__msgid/python_inside_maya/__5329B11D.4040200%40hybride.com
> <https://groups.google.com/d/msgid/python_inside_maya/5329B11D.4040200%40hybride.com>__.
>
> For more options, visit https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>.
>
>
>
>
> --
> *Marcus Ottosson*
> konstr...@gmail.com <mailto:konstr...@gmail.com>
>
> --
> 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
> <mailto:python_inside_m...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCB%2BTeeUmXjjM2eBYbnFLDELt%2BZB-gBfH8L2L1QChnE-w%40mail.gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCB%2BTeeUmXjjM2eBYbnFLDELt%2BZB-gBfH8L2L1QChnE-w%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Marcus Ottosson

unread,
Mar 19, 2014, 1:56:58 PM3/19/14
to Eric Thivierge, python_in...@googlegroups.com, damon shelton
I see. It's generally a good approach to work with full-paths throughout, and if you do, you should encounter little difficulty in paths across packages. Even duplicate names across hierarchies are unique given a full path.




            To view this discussion on the web visit
        https://groups.google.com/d/____msgid/python_inside_maya/____5329AFAD.5080403%40hybride.com
        <https://groups.google.com/d/__msgid/python_inside_maya/__5329AFAD.5080403%40hybride.com>

        <https://groups.google.com/d/__msgid/python_inside_maya/__5329AFAD.5080403%40hybride.com
        <https://groups.google.com/d/msgid/python_inside_maya/5329AFAD.5080403%40hybride.com>__>__.
            For more options, visit
        https://groups.google.com/d/____optout
        <https://groups.google.com/d/__optout>

            <https://groups.google.com/d/__optout
        <https://groups.google.com/d/optout>>.


        --
        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

    <mailto:python_inside_maya%2Bunsu...@googlegroups.com>.
    To view this discussion on the web visit


--
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

To view this discussion on the web visit




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

Eric Thivierge

unread,
Mar 19, 2014, 1:59:45 PM3/19/14
to Marcus Ottosson, python_in...@googlegroups.com, damon shelton
Agreed. The problem is, in Softimage there is not "Full Path". You can
get the ".FullName()" which only gives you the model space plus object
name "myRig.pCube1" for example instead of
"myRig.geometry.lowres.pCube1". It was quick enough to roll my own
though.

Eric T.

On Wednesday, March 19, 2014 1:56:58 PM, Marcus Ottosson wrote:
> I see. It's generally a good approach to work with full-paths
> throughout, and if you do, you should encounter little difficulty in
> paths across packages. Even duplicate names across hierarchies are
> unique given a full path.
>
>
> On 19 March 2014 17:44, Eric Thivierge <ethiv...@hybride.com
> <mailto:ethiv...@hybride.com>> wrote:
>
> Thanks Marcus,
>
> The problem I'm facing currently is that I need to write data to a
> json file that can be read in and interperated by both Softimage
> and Maya. So I have to store object names consistently when
> exporting from Softimage or Maya. I'm probably more in favor of
> mimicking the Maya format at this point since, well you know why.
>
> Thanks,
> Eric T.
>
>
> On Wednesday, March 19, 2014 1:12:25 PM, Marcus Ottosson wrote:
>
> In Maya, the hierarchy is included in a node's path but gets
> discarded
> most of the time and instead the artist is trusted to keep his
> names
> unique. The reason this is the case is because in Maya, there
> is no
> "hierarchy". Maya is inherently node-based; Softimage on the other
> hand does have the notion of a hierarchy.
>
> Thus, a full path of a pCube1 may be resolve
> into'|a_group|another_group|__pCube1', the | being the separator.
>
> You can ask for full paths via cmds.ls <http://cmds.ls>
> <http://cmds.ls>(long=True),
>
> other commands have similar flags, such as
> cmds.listRelatives(fullPath=__True) that ultimately do the
> same thing;
> concatenate the depth-first path of a node within a graph.
>
> Additionally, Maya has the notion of namespaces, which
> encapsulates
> nodes similar to how Models encapsulates nodes in Softimage,
> however I
> would suggest sticking with hungarian notation at first as
> namespaces
> are tricky to understand at first.
>
> Best,
> Marcus
>
>
> On 19 March 2014 15:00, Eric Thivierge <ethiv...@hybride.com
> <mailto:ethiv...@hybride.com>
> <mailto:ethiv...@hybride.com
> <mailto:ethiv...@hybride.com>__>> wrote:
>
> Hah. Well, yeah... thanks.
>
> On Wednesday, March 19, 2014 10:59:10 AM, damon shelton wrote:
>
> cmds.ls <http://cmds.ls> <http://cmds.ls> <http://cmds.ls>
> ('*pCube1', long=True)
>
>
>
> On Mar 19, 2014 7:54 AM, "Eric Thivierge"
> <ethiv...@hybride.com
> <mailto:ethiv...@hybride.com> <mailto:ethiv...@hybride.com
> <mailto:ethiv...@hybride.com>__>
> <mailto:ethiv...@hybride.com
> <mailto:ethiv...@hybride.com>
>
> <mailto:ethiv...@hybride.com
> python_inside_maya...@googlegroups.com
> <mailto:python_inside_maya%2B____un...@googlegroups.com>
>
> <mailto:python_inside_maya%2B____un...@googlegroups.com
> <mailto:python_inside_maya%252B__un...@googlegroups.com>>
>
>
> <mailto:python_inside_maya%____2Bun...@googlegroups.com
> <mailto:python_inside_maya%25__2Bun...@googlegroups.com>
>
> <mailto:python_inside_maya%__252Bunsubscribe@googlegroups.__com <mailto:python_inside_maya%25252Bun...@googlegroups.com>>__>.
>
>
> To view this discussion on the web visit
> https://groups.google.com/d/______msgid/python_inside_maya/______5329AFAD.5080403%40hybride.__com
> <https://groups.google.com/d/____msgid/python_inside_maya/____5329AFAD.5080403%40hybride.com>
>
> <https://groups.google.com/d/____msgid/python_inside_maya/____5329AFAD.5080403%40hybride.com
> <https://groups.google.com/d/__msgid/python_inside_maya/__5329AFAD.5080403%40hybride.com>__>
> <https://groups.google.com/d/msgid/python_inside_maya/5329AFAD.5080403%40hybride.com>__>__>__.
> For more options, visit
> https://groups.google.com/d/______optout
> <https://groups.google.com/d/____optout>
> <https://groups.google.com/d/____optout
> <https://groups.google.com/d/__optout>>
>
> <https://groups.google.com/d/____optout
> <https://groups.google.com/d/__optout>
> <https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>>>.
>
>
> --
> 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
> <mailto:python_inside_may...@googlegroups.com
> <mailto:python_inside_maya%2B__uns...@googlegroups.com>
>
>
> <mailto:python_inside_maya%__2Buns...@googlegroups.com
> <mailto:python_inside_maya%252Buns...@googlegroups.com>__>>.
>
> To view this discussion on the web visit
> https://groups.google.com/d/____msgid/python_inside_maya/____CAM9RXoLb37cQR4QB8R6eshN1PmT95____n3uGY9qJicLtHFoEhQjLw%__40mail.__gmail.com
> <https://groups.google.com/d/__msgid/python_inside_maya/__CAM9RXoLb37cQR4QB8R6eshN1PmT95__n3uGY9qJicLtHFoEhQjLw%40mail.__gmail.com>
>
> <https://groups.google.com/d/__msgid/python_inside_maya/__CAM9RXoLb37cQR4QB8R6eshN1PmT95__n3uGY9qJicLtHFoEhQjLw%40mail.__gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAM9RXoLb37cQR4QB8R6eshN1PmT95n3uGY9qJicLtHFoEhQjLw%40mail.gmail.com>>
>
> <https://groups.google.com/d/____msgid/python_inside_maya/____CAM9RXoLb37cQR4QB8R6eshN1PmT95____n3uGY9qJicLtHFoEhQjLw%__40mail.__gmail.com?utm_medium=__email&__utm_source=footer
> <https://groups.google.com/d/__msgid/python_inside_maya/__CAM9RXoLb37cQR4QB8R6eshN1PmT95__n3uGY9qJicLtHFoEhQjLw%40mail.__gmail.com?utm_medium=email&__utm_source=footer>
> https://groups.google.com/d/____optout
> <https://groups.google.com/d/__optout>
> <https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>>.
>
>
> --
> 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
> <mailto:python_inside_maya%252Buns...@googlegroups.com>__>.
> To view this discussion on the web visit
> https://groups.google.com/d/____msgid/python_inside_maya/____5329B11D.4040200%40hybride.com
> <https://groups.google.com/d/__msgid/python_inside_maya/__5329B11D.4040200%40hybride.com>
>
> <https://groups.google.com/d/__msgid/python_inside_maya/__5329B11D.4040200%40hybride.com
> <https://groups.google.com/d/msgid/python_inside_maya/5329B11D.4040200%40hybride.com>__>__.
> *Marcus Ottosson*
> konstr...@gmail.com <mailto:konstr...@gmail.com>
> <mailto:konstr...@gmail.com <mailto:konstr...@gmail.com>__>
>
>
> --
> 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
> <mailto:python_inside_maya%2Bunsu...@googlegroups.com>>.
> To view this discussion on the web visit
> https://groups.google.com/d/__msgid/python_inside_maya/__CAFRtmOCB%__2BTeeUmXjjM2eBYbnFLDELt%2BZB-__gBfH8L2L1QChnE-w%40mail.gmail.__com
> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCB%2BTeeUmXjjM2eBYbnFLDELt%2BZB-gBfH8L2L1QChnE-w%40mail.gmail.com>
> <https://groups.google.com/d/__msgid/python_inside_maya/__CAFRtmOCB%__2BTeeUmXjjM2eBYbnFLDELt%2BZB-__gBfH8L2L1QChnE-w%40mail.gmail.__com?utm_medium=email&utm___source=footer
> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCB%2BTeeUmXjjM2eBYbnFLDELt%2BZB-gBfH8L2L1QChnE-w%40mail.gmail.com?utm_medium=email&utm_source=footer>>.
>

Marcus Ottosson

unread,
Mar 19, 2014, 2:14:27 PM3/19/14
to Eric Thivierge, python_in...@googlegroups.com, damon shelton
Aaah, right. I suppose you would need to recursively head up the hierarchy for that then. Glad to hear you got it working.




                To view this discussion on the web visit
        https://groups.google.com/d/____msgid/python_inside_maya/____CAM9RXoLb37cQR4QB8R6eshN1PmT95____n3uGY9qJicLtHFoEhQjLw%__40mail.__gmail.com
        <https://groups.google.com/d/__msgid/python_inside_maya/__CAM9RXoLb37cQR4QB8R6eshN1PmT95__n3uGY9qJicLtHFoEhQjLw%40mail.__gmail.com>

        <https://groups.google.com/d/__msgid/python_inside_maya/__CAM9RXoLb37cQR4QB8R6eshN1PmT95__n3uGY9qJicLtHFoEhQjLw%40mail.__gmail.com
        <https://groups.google.com/d/msgid/python_inside_maya/CAM9RXoLb37cQR4QB8R6eshN1PmT95n3uGY9qJicLtHFoEhQjLw%40mail.gmail.com>>

        <https://groups.google.com/d/____msgid/python_inside_maya/____CAM9RXoLb37cQR4QB8R6eshN1PmT95____n3uGY9qJicLtHFoEhQjLw%__40mail.__gmail.com?utm_medium=__email&__utm_source=footer
        <https://groups.google.com/d/__msgid/python_inside_maya/__CAM9RXoLb37cQR4QB8R6eshN1PmT95__n3uGY9qJicLtHFoEhQjLw%40mail.__gmail.com?utm_medium=email&__utm_source=footer>
        https://groups.google.com/d/____optout
        <https://groups.google.com/d/__optout>
                <https://groups.google.com/d/__optout
        <https://groups.google.com/d/optout>>.


            --
            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

            To view this discussion on the web visit



        --
        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
        <mailto:python_inside_maya%2Bunsu...@googlegroups.com>>.
        To view this discussion on the web visit




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

Joe Weidenbach

unread,
Mar 22, 2014, 3:09:45 AM3/22/14
to python_in...@googlegroups.com
Actually, even after all of my years (12+) working with Maya, I've got a question on this one too--has anyone found a reliable way to get around maya creating a group with a name which returns only the short path (not even the shortest unique path)?  Is the only way to rename the group after the creation?  Or, is there an API function that I'm missing that will return the actual long group name?  My current workaround is running a cmds.ls on any nodes I create with long=true, but with a group that returns a list of possible nodes; like so:

node = cmds.ls(cmds.group(empty=True, name="testGRP"), long=True)

if I have a group such as "|test|testGRP", node will equal [u'|test|testGRP', u'|testGRP']

Of course, the intention is that node would equal [u'|testGrp'] so that I could use node[0] to reference it, versus arbitrarily guessing which is the correct node.  This is one of those cases where I miss using PyMEL currently :)

My current solution is to use something more like this:

node = cmds.group(empty=True, world=True, name="testGRP")
foundNode = cmds.ls("|{0}".format(node), long=True)

But for obvious reasons, that feels fairly silly and highly unpythonic :)

Also, as an update, I talked to Steven Roselle at GDC, and he said that Python API 2.0 is still very high on their list of priorities, but has no updates in 2015 :(

Joe


            To view this discussion on the web visit
        https://groups.google.com/d/____msgid/python_inside_maya/____5329AFAD.5080403%40hybride.com
        <https://groups.google.com/d/__msgid/python_inside_maya/__5329AFAD.5080403%40hybride.com>

        <https://groups.google.com/d/__msgid/python_inside_maya/__5329AFAD.5080403%40hybride.com
        <https://groups.google.com/d/msgid/python_inside_maya/5329AFAD.5080403%40hybride.com>__>__.
            For more options, visit
        https://groups.google.com/d/____optout
        <https://groups.google.com/d/__optout>

            <https://groups.google.com/d/__optout
        <https://groups.google.com/d/optout>>.


        --
        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
        <mailto:python_inside_maya%2Bunsu...@googlegroups.com>>.

        To view this discussion on the web visit
    send an email to python_inside_ma...@googlegroups.com

    <mailto:python_inside_maya%2Bunsu...@googlegroups.com>.
    To view this discussion on the web visit


--
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

To view this discussion on the web visit




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

--
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/CAFRtmODcD4zs%3DKJhAYr2e4DKcvhtZ%2BQMXwYRbKPz2M2zP7rXSg%40mail.gmail.com.

Justin Israel

unread,
Mar 22, 2014, 7:03:02 AM3/22/14
to python_in...@googlegroups.com
What if you did it through the Maya API, and made use of the actual MObject knowing exactly which node it is pointing at, instead of the ambiguous maya paths?

import maya.OpenMaya as om
dagFn = om.MFnDagNode()
group = dagFn.create("transform", "testGRP")
dagFn.setObject(group)
groupPath = dagFn.fullPathName()
print groupPath
# |testGRP

But I will be honest. Given that the currency in the Maya commands is string paths, I don't think it is wrong to format a full path of "|testGRP" if a call to cmds.group(empty=True, world=True, name="testGRP") returns your "testGRP". Am I right in assuming that if you are creating it with world=True, you are placing it at the root of the scene anyways? The only thing that might be faulty with that is if you are in a different namespace, right? But overall, the Maya API might be more "precise". 





Marcus Ottosson

unread,
Mar 22, 2014, 7:05:18 AM3/22/14
to python_in...@googlegroups.com
I think the Python API 2.0 is already in effect (since Maya 2013 or so) but only applies to the API. maya.cmds on the other hand is a wrapper around MEL independent of the underlying API and so wasn't affected by that change, PyMEL is then a another wrapper on-top of that wrapper, but I think PyMEL uses some of the Python API for some of its more esoteric features; possibly to distinguish between nodes, I'm not sure.

To fully understand why Maya doesn't deal in "absolute paths" you'll first need to understand what a graph is. In short, the concept of paths in Maya is a mere convenience to users; nodes exist in a type of graph known as a Directed Acyclic Graph (or DAG) which is rooted in a family of graphs, each with its own strengths and weaknesses, and in such a graph there can be multiple paths to any given node. This also makes the term "absolute" also a convenience.

There is an interesting wiki page about it but the topic extends quite far and is ultimately grounded in a branch of mathematics known as discrete mathematics.

In Maya, whenever you create a node without specifying a name, Maya goes ahead and gives that node a unique name based on the currently existing names in your scene. This is also for convenience, as we've already seen, names can co-exist. With a unique name, Maya is able to provide you with this mythical "absolute path" and allow you to work with nodes similar to how you would work with files and folders; as though they existed within a hierarchy. Its familiar and easy, unlike graphs which is ultimately a network and not a hierarchy. (Think about how you would go about finding the absolute path to a person on Facebook or Linkedin; its not possible as there would be many paths depending on where you start looking from and which route you are looking to take; think also Google Maps and addresses)

So, in short), when you force a name onto your new nodes, Maya will have no way of providing you with an absolute path as the name you provide may likely exist elsewhere which will give Maya no other option than to provide you will all alternatives to the node you are looking for.

A more consistent way would be to let Maya assign this unique name, by simply not providing one, and then go about renaming it. Before renaming it, cmds.ls(long=True) would then give you only one result that you could rely on. What happens when you rename it however is a different matter and keeping track of it would be up to you.

If you truly needed a sustainable, persistent "absolute path" to the nodes you create, you could do what I suspect Maya does internally; which is to assign a unique identifier (UUID, GUID or similar) to an attribute within your node. You could then use this attribute to distinguish nodes from each other, even though they may have the same name.


# Psuedo-code, bear with me.

# Step 1. Create node with unique identifier
group = cmds.createNode('transform')  # Will produce a unique name
cmds.addAttr(group, 'uid', 'my_unique_identifier', dt='string')
cmds.rename(group, 'myCommonName')  # Absolute path is lost into oblivion..

# Step 2. Find node by unique identifier
exact_result = None
multiple_results = cmds.ls('myCommonName', long=True)  # May return multiple nodes
for result in multiple_results:
   uid = cmds.getAttr(result + ".uid")
   if uid == my_special_id:
       exact_result = result





For more options, visit https://groups.google.com/d/optout.



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

Joe Weidenbach

unread,
Mar 22, 2014, 1:33:42 PM3/22/14
to python_in...@googlegroups.com
Re: Python API 2.0, the API is in effect (and wonderful to use).  However, it's incomplete--we're still missing about half of the API (Most common one I run into is iterators--we don't have them yet.)

Regarding the graph information--that makes absolute sense to me, and explains well what I've been running into.  It also probably explains why trying to name an object on creation in PyMEL tended to crash Maya.  So, it's probably best to do exactly that--create the objects, then rename then to what I actually want them to be called.

Thanks!

Joe

Joe Weidenbach

unread,
Mar 22, 2014, 1:42:51 PM3/22/14
to python_in...@googlegroups.com
I tend to agree, Justin. As far as your assumptions, you're pretty much
dead on--I take the approach that this node should only be created by my
framework, and is immediately re-parented to another object, so there
should never be two instances of it in the scene root. and actually, as
far as namespaces are concerned, they get escaped by the script anyway.
But, I do like to be precise, so I appreciate the pointer to the API
commands!

Thanks!

Joe Weidenbach

unread,
Mar 22, 2014, 10:49:52 PM3/22/14
to python_in...@googlegroups.com
Just for clarification on a few of these areas, I'd like to break this down a bit, and who knows, this great info on the backend might be useful to someone, so here goes:




On 3/22/2014 4:05 AM, Marcus Ottosson wrote:
I think the Python API 2.0 is already in effect (since Maya 2013 or so) but only applies to the API. maya.cmds on the other hand is a wrapper around MEL independent of the underlying API and so wasn't affected by that change, PyMEL is then a another wrapper on-top of that wrapper, but I think PyMEL uses some of the Python API for some of its more esoteric features; possibly to distinguish between nodes, I'm not sure.

As I already mentioned in a previous response to this email, Maya's Python API 2.0 is available (and has been since 2013), but is incomplete.  I'm not deep enough into programming the Maya API to know exactly how much is not done yet, but at the very least it is missing Iterators, which takes away the majority of the useful mesh information/editing functions.  You can still use the 1.0 API for those within the same script, but you can't mix the two--you can't retrieve an object with the 2.0 API and then pass it to a 1.0 function for processing (or vice versa).

You have correctly stated that maya.cmds is a wrapper around the MEL functionality, and is again incomplete (although much less so).  You have to go pretty deep to find the areas where maya.cmds just won't work, and it is mostly in the UI and callback functionality, along with some of the animation commands.  For these, you must wrap a MEL command to be called.  Also, maya.cmds, like MEL, is completely string based, so you have to do a lot of work keeping track of any changes to names.  This is where PyMEL comes in.

PyMEL is a wrapper around the overall Python functionality--not just maya.cmds.  Specifically, it uses the API to reach in and give us direct access to the node-level information and functions, and keeps direct references to objects instead of names.  It's really nice functionality for keeping your code simple (I find that coding in PyMel produces nice clean code that tends to take up 1/2 to 2/3 of the lines of code to achieve the same tasks versus maya.cmds and the API.  The problem with PyMel is twofold--first, it's slower (it's another layer of wrappers, after all), and two, it causes compatibility issues.  I've run into situations where a bug in PyMEL effectively broke my maya installations, and anything which even used it would drop Maya into an infinite plugin-loading loop.  Because of this, I try to stay away from PyMel unless I know it's going to be fully available and tested for installs I'm working on.

If you truly needed a sustainable, persistent "absolute path" to the nodes you create, you could do what I suspect Maya does internally; which is to assign a unique identifier (UUID, GUID or similar) to an attribute within your node. You could then use this attribute to distinguish nodes from each other, even though they may have the same name.


# Psuedo-code, bear with me.

# Step 1. Create node with unique identifier
group = cmds.createNode('transform')  # Will produce a unique name
cmds.addAttr(group, 'uid', 'my_unique_identifier', dt='string')
cmds.rename(group, 'myCommonName')  # Absolute path is lost into oblivion..

# Step 2. Find node by unique identifier
exact_result = None
multiple_results = cmds.ls('myCommonName', long=True)  # May return multiple nodes
for result in multiple_results:
   uid = cmds.getAttr(result + ".uid")
   if uid == my_special_id:
       exact_result = result

This is actually a really useful pattern to setup, and I might implement it in future projects.  However, Maya does give us functionality that would allow us to keep direct references to objects using the graph--Message Nodes.  This also avoids the need for loops to search for a given GUID.  What it does add is the need to maintain a node to keep these references on, which is incidentally the exact use case this question belonged to for me :).  I'm actually creating a "Meta" node that attaches itself to my relevant structures in the scene, and it always has the same name--which led to challenges when I was creating a new node.  So, that's where the question came from :).  I think Justin's solution is actually the one I want to work with on this, just because I much prefer to deal with the actual object instead of trying to keep track of the name.  As I said, I really like PyMel's way of handling things, I've just run into enough issues that I don't want to deal with PyMel directly :).

With that said, this is a really good explanation of why you should create a node and rename it rather than specifying the name directly.

Marcus Ottosson

unread,
Mar 23, 2014, 5:25:32 AM3/23/14
to python_in...@googlegroups.com
Sounds right. I've never used much of the Maya API myself, but it sounds like it lets you keep an absolute reference to the nodes you create which seems like a good way to go if all of your node creation happens within the same script.

The issue is what happens once your script finishes, when the absolute reference also vanishes into oblivion along with the absolute path. But, as you said, you can exploit the fact that nodes can stay connected to other nodes which would give them a common starting point from which to start looking for names.

However I'm curious as to why you choose to create this Meta node (is that a custom node-type?) or rely on the message slot of nodes (if I understood you correctly), which is already used by other nodes (although sporadically) when Maya already makes use of objectSet, aka "set", for this purpose.

Maybe I'm not in the loop on exactly it is what you're trying to do though; is this still a question on finding objects in a scene? By name?


--
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.

For more options, visit https://groups.google.com/d/optout.



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

Joe Weidenbach

unread,
Mar 23, 2014, 4:23:11 PM3/23/14
to python_in...@googlegroups.com
My meta node is just a group node that I add connections to.  My toolset is an extensable auto-rigging framework, and I'm building it out as a series of connectable blocks.  I like having named access to the relevant nodes from each block to access the necessary nodes for connection of the blocks at later stages in the process.  Another eventual benefit is that a rigger can work backwards on a rig that was built with the framework--i.e. find a rigged block and revert to just joints (and by joints I mean a custom joint system that handles orientations internally), make adjustments, switch the rig module that controls that block, then re-apply.  That's all possible through this meta-network.  Sets don't (to my knowledge) have that level of functionality.  The main purpose of the Meta node is that someone using the toolkit can click on ANY object in a scene, and as long as it's hooked up to a metanode the system can identify all of the needed nodes to interact with.  I don't need search by name specifically at that point, that's just something for while the network builds.


On 3/23/2014 2:25 AM, Marcus Ottosson wrote:

Eduardo Grana

unread,
Mar 23, 2014, 8:20:31 PM3/23/14
to python_in...@googlegroups.com
Hello People,

Regarding tracking names, besides the options mentioned already,
you can use the ui control called nameField, that you can connect
to a node and it will keep track of it's name.

Regarding the tracking of objects via connections, i've found that
ever since maya 2013, if you make intensive use of them in your
files, the loading and referencing times get really big. Have anyone
else run into that situation? Any idea if there could be any work around?

Cheers!
Eduardo


--
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.

For more options, visit https://groups.google.com/d/optout.



--
Eduardo Graña
www.eduardograna.com.ar

Marcus Ottosson

unread,
Mar 24, 2014, 2:49:21 AM3/24/14
to python_in...@googlegroups.com
Hi Eduardo, that's really interesting. Never used the nameField command before but it seems to do exactly that; keep track of name changes. I would imagine it made use of a scriptJob hooked up to the node via nodeNameChanged which you could also use for your own UIs if you'd like. Unsure of how well it would work when monitored nodes are referenced into a scene though, do you have any experience with that?

As per loading and reference times, seems intriguing, but is it possible to open up a separate thread for it? Otherwise I fear the topic of tracking down nodes might get diluted. :)



For more options, visit https://groups.google.com/d/optout.



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

Reply all
Reply to author
Forward
0 new messages