Remove namespace from object

3,537 views
Skip to first unread message

Stefan Andersson

unread,
Sep 3, 2014, 11:53:05 AM9/3/14
to python_in...@googlegroups.com
Hi All,

I'm having a brain lock down... I'm trying to removed namespaces (both nested and not nested) from shaders. But can't for the world in me figure out how to check the namespace of the selected object, or query a node to return the namespaces....

I need to get a string back of all the namespaces that a object or a shader has.

any help would be appreciated. or a RTFM to a specific page :)

regards
stefan



--
-- -- - [ not morse code ] -- - - -- -- - -- - -- -- - - - --- - -- - - - --- -
stefan andersson | vfx supervisor
Important Looking Pirates  | http://www.ilpvfx.com

Marcus Ottosson

unread,
Sep 3, 2014, 12:12:50 PM9/3/14
to python_in...@googlegroups.com

Hi Stefan,

The namespace is in the name, so renaming a node to a name without namespace will effectively remove its namespace.

cmds.rename('namespace:my_node', 'my_node')

So if you’ve got many shaders you’d like to remove namespaces from, simply rename them all.

for shader in cmds.ls(type='shadingDependNode'):
    try:
        # Note rsplit instead of split
        namespace, name = shader.rsplit(":", 1)
    except:
        namespace, name = None, shader

    if namespace:
        try:
            cmds.rename(shader, name)
        except RuntimeError:
            # Can't rename some shaders, like lambert1
            pass

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 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/CAKW24e1X8N1As7CCePs_ijzMHj641rP7gCM_C3yNZ96QMqsdKA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



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

Stefan Andersson

unread,
Sep 3, 2014, 2:49:08 PM9/3/14
to python_in...@googlegroups.com
Hello Marcus!
Well, that only renames the node. So child nodes will still have the namespace intact.

So right now I have this which moves the namespace eventually into the root


oSel = cmds.ls(sl=True)[0].split(":")
if len(oSel) >1:
    for i in range(len(oSel)-1):
        cmds.namespace(mv=(oSel[i], ":"), f=True)

Still trying to find a clever way of removing a selected object/shader (and their child nodes) into the root.

regards
stefan




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



--

Marcus Ottosson

unread,
Sep 4, 2014, 3:07:42 AM9/4/14
to python_in...@googlegroups.com

Hello Marcus!
Well, that only renames the node. So child nodes will still have the namespace intact.

Yes it does. A namespace in Maya is actually nothing more than some additional character in its name. The cmds.namespace() is really just a fancy cmds.rename if all you’re using it for is renaming nodes. One of its benefits lie in the creation or removal of namespace objects, those you can see under Window -> General Editors -> Namespace Editor. But even those are mere conveniences for ultimately renaming nodes, as the “root” namespace really just means “no colons (:) in the name”

With this in mind, the following snippet would look at your selection, and remove namespaces from all children below.

def remove_namespaces(node):

    try:
        # Note rsplit instead of split

        namespace, name = node.rsplit(":", 1)
    except:
        namespace, name = None, node

    if namespace:
        try:
            cmds.rename(node, name)
        except RuntimeError:
            # Can't remove namespaces from read-only nodes
            # E.g. namespaces embedded in references
            pass

def remove_namespaces_from_selection():
    for node in cmds.ls(sl=1):
        # Remove namespaces of all children first
        for descendent in cmds.listRelatives(node, allDescendents=True):
            remove_namespaces(descendent)

        # Finally, remove namespace from current selection
        remove_namespaces(node)


remove_namespaces_from_selection()

It wouldn’t look at shaders though, for that I’d recommend also listing connection to each node capable of having a shader.

Does that help at all?




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



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

Marcus Ottosson

unread,
Sep 4, 2014, 3:10:57 AM9/4/14
to python_in...@googlegroups.com

As a shot in the dark, if the origin of your namespaces is from importing a scene, you have the option of merging names as opposed to storing it all within a namespace. Namespaces are originally for dealing with name clashes.

From the docs:

Namespaces are primarily used to resolve name-clash issues in Maya, where a new object has the same name as an existing object (from importing a file, for example). - Docs

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

Stefan Andersson

unread,
Sep 4, 2014, 4:57:34 AM9/4/14
to python_in...@googlegroups.com
Hey Marcus!
Thanks for the code, all code helps so I appreciate you sharing that. I'm trying to trap what other artists are doing... sometimes they have stolen shaders from other scenes or just duplicated networks, there are 1000's of ways they are doing it. But I would like to do a bit of cleanup before they submit the shader asset, and I also want to do it in a way that doesn't obstruct their workflow. 

I could go fascist and block "illegal workflows", but I suspect they would be blocked and the quality of the images would eventually go down :)

I'm actually looking into parsing the exported ma file instead, I had a thought that it might be a bad thing to temper with the work file that the artist is working on.

any thoughts on shader publishing?

regards
stefan andersson





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



--
-- -- - [ not morse code ] -- - - -- -- - -- - -- -- - - - --- - -- - - - --- -
stefan andersson | vfx supervisor
Important Looking Pirates  | http://www.ilpvfx.com

Marcus Ottosson

unread,
Sep 4, 2014, 5:15:03 AM9/4/14
to python_in...@googlegroups.com

any thoughts on shader publishing?

Actually, yes. Me and a few Pipeline TDs here and on GitHub have been working on exactly this for the past month; i.e. ensuring quality before sharing work, including shaders.

You are welcome to join; odds are we’d be able to create something better together than on our own. Have a look here:

https://github.com/abstractfactory/pyblish

GitHub is performing maintenance on their Wiki backend at the moment so links from the sidebar isn’t working as they should. Once they are finished, you can find more info here:

https://github.com/abstractfactory/pyblish/wiki

In a nutshell, the workflow is:

  1. Artist created content
  2. Artist publishes content using Pyblish
  3. Pyblish passes content through validations, which is a plugin-system of classes that either accepts or declines the content, written by either you or someone else.
  4. If validation passes, content is exported
  5. If not, artist is given a friendly message on what to do about it.

The end result is, nothing leaves Maya unless it follows your naming and namespace conventions, or whatever else you are looking to validate.

Feel free to add me on Hangouts and I’ll get you up and running with it, some of us are already using with it in production.

Best,
Marcus




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



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

Gyurma

unread,
Sep 4, 2014, 5:18:07 AM9/4/14
to python_in...@googlegroups.com
Hi Stefan,

I've gone through this pain (admittedly not a namespace fan) the problems encountered: non unique nodes under groups (creating path "|"), renaming does not remove the namespace info from the scene file but keeps the empty namespaces (if you go to the namespace editor you will still find all the namespaces there), multilevel namespaces (multi level pain.....) however I think the renaming is the only non-destructive way to get rid of the ugly namespaces

happy to share what I've written (that goes through the whole scene) just warn you I'm not a "born" scripter/programmer my script is probably not very pretty.....

cheers,
 Gyuri



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



--
Gyuri Kiss
www.gyurma.com

David Martinez

unread,
Sep 4, 2014, 5:21:49 AM9/4/14
to python_in...@googlegroups.com
I agree with Marcus here,

I think that the more people we can get involved the better. It is important that we see the kind of problems that we are facing in production in order to try and find something that will be useful for all of us.  :-)


--
David Martinez - Technical Animator
 


Stefan Andersson

unread,
Sep 4, 2014, 7:55:52 AM9/4/14
to python_in...@googlegroups.com
Thanks guys for the encouraging words about your pain... and that we all share the same pain.... we should have secret meetings under bridges. :)

Namespaces and all of that is horrible, but also useful. It does prevent users from having non-unique names, and when used properly it's actually a nice thing. But, our world doesn't exist under the perfect project. I've managed to do the quick hack for now, we are in the middle of switching to a new pipeline and the tech department is busy, hence that I have to do quick hacks myself.

best regards
stefan andersson






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

Mark Jackson

unread,
Sep 4, 2014, 10:52:43 AM9/4/14
to python_inside_maya
one of the problems with namespaces is what happens when you're running them in conjunction to referencing, it all becomes a hell of a lot more complex. I keep meaning to add a load of namespace and reference utils in Red9 just haven't had the time.

Marcus, that sounds an interesting project, I'll take a look at that

cheers

Mark



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



--
-------------------------------------
Mark Jackson
Technical Animation Director
http://markj3d.blogspot.com/

Marcus Ottosson

unread,
Sep 4, 2014, 11:18:48 AM9/4/14
to python_in...@googlegroups.com

Marcus, that sounds an interesting project, I’ll take a look at that

Hey Mark, thanks for the kind words. It’d be my pleasure to have you with us. Let me know if you want any help getting set up.




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



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

Reply all
Reply to author
Forward
0 new messages