Selecting objects within a set namespace?

1,348 views
Skip to first unread message

Panupat Chongstitwattana

unread,
Mar 16, 2015, 1:20:03 AM3/16/15
to python_in...@googlegroups.com
If I store my object names without namespace and then reference them into a new scene.

For example if this is the string I stored.
rig_grp|geo_grp|head_grp|left_eye_geo

And I know for certain my namespace would be "ref_RN1"

Currently I would manipulate the stored string so it turns into ref_RN1:rig_grp|ref_RN1:geo_grp|ref_RN1:head_grp|ref_RN1:left_eye_geo
But I'm curious if there's a cleaner or more proper way to do this?

Tried use namespace(set=':ref_extra_RN1') but that doesn't seem to do anything

Best regard.

Justin Israel

unread,
Mar 16, 2015, 2:43:52 AM3/16/15
to python_in...@googlegroups.com
Yo,

Even though you are setting the current namespace, the other various commands are still expecting fully qualified namespace paths to match. But I think you would get what you want by using the relativeNames mode on the namespace command. As per the docs, you have to be careful to turn it back off after you have turned it on, since it changes the behavior of maya cmds. Here is a quick snippet you can try that I have used before. It uses a context manager to change the namespace, turn on relative mode, do whatever commands you want, and then turn it back off and restore the previous namespace:
from contextlib import contextmanager

@contextmanager
def namespaceContext(ns):
    cur = cmds.namespaceInfo(currentNamespace=True)
    on = cmds.namespace(q=True, relativeNames=True)

    cmds.namespace(set=ns)
    cmds.namespace(relativeNames=True)

    yield

    if on:
        cmds.namespace(relativeNames=False)
    cmds.namespace(set=cur)

# Test
cmds.namespace(set=":")
cmds.namespace(add="foo")
cmds.namespace(add="bar")

cmds.namespace(set=":foo")
cmds.sphere(name="mySphere")

cmds.namespace(set=":bar")
cmds.sphere(name="mySphere")

cmds.namespace(set=":")
cmds.ls("mySphere")

with namespaceContext(":"):
    print cmds.ls("mySphere")

with namespaceContext(":foo"):
    print cmds.ls("mySphere")

with namespaceContext(":bar"):
    print cmds.ls("mySphere")

Does that do what you wanted?

Justin



--
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/bb64548b-111e-483e-a609-e4d3015a8b15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Geordie Martinez

unread,
Mar 16, 2015, 2:47:45 AM3/16/15
to python_inside_maya
if the item is unique in the namespace you can refer to it by its short name:
import maya.cmds as mc
mc.select("ref_RN1:left_eye_geo",r=True)
you only need the long name if it's not unique. 

select it in the scene and look in your script editor. that should give you an idea.
if it has a "|" pipe in the name, then it's not unique. if there is no pipe in the name it's unique.





--

Justin Israel

unread,
Mar 16, 2015, 3:10:45 AM3/16/15
to python_inside_maya
I had a small typo in my context function (should have checked that the original state of relativeNames was *not* on before:

from contextlib import contextmanager @contextmanager def namespaceContext(ns): cur = cmds.namespaceInfo(currentNamespace=True) on = cmds.namespace(q=True, relativeNames=True) cmds.namespace(set=ns) cmds.namespace(relativeNames=True) yield if not on: cmds.namespace(relativeNames=False) cmds.namespace(set=cur)



Marcus Ottosson

unread,
Mar 16, 2015, 3:17:20 AM3/16/15
to python_in...@googlegroups.com

I would personally make it:

with namespace(":foo"):
    print cmds.ls("mySphere")


Otherwise, that’s poetry you’ve got there, Justin. :)

Justin Israel

unread,
Mar 16, 2015, 4:03:51 AM3/16/15
to python_in...@googlegroups.com

You are too sweet Marcus! Hah


--
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/CAFRtmOCGjGqK6OrLbuty6S69m_tSgEcRT%2B1rGEUo9m1dc7y76Q%40mail.gmail.com.

Panupat Chongstitwattana

unread,
Mar 16, 2015, 5:04:22 AM3/16/15
to python_in...@googlegroups.com
Thank you every one :) Haven't used the contextmanager in a while, great example Justin thank you very much.
Reply all
Reply to author
Forward
0 new messages