For me namespaces were just a giant pain in the ass. They always seemed to get in the way and they were always cropping up because maya likes to use them by default and even if you have them turned off on import they will still crop up due to cross version issues or references or how the file was saved last. I never truly understood namespaces in Maya until I started writing scripts to deal with them. I knew that they were there to prevent name clashes between different assets and that they manifest themselves as prefixes on all of the objects that they effect. But what I didn’t understand is that namespaces are hierarchical organizations very much like those of a child parent relationship. Namespaces aren’t just strings on the front of the object name, they are also a relationship. I still don’t know exactly what they are. They are not nodes, they are not DAG objects, they don’t appear in the Hypergraph.
Once you make friends with namespaces you can use them to your advantage. Know that they are malleable, they can be created, combined and deleted. You can use them to select the objects that reside in them. You can move those objects from one namespace to another. However, they are finicky, writing scripts to manipulate them is difficult.
Here is a script that I wrote to import all of the references in a scene and remove their namespaces:
- def importReference(*args):
- references = cmds.ls(type = 'reference')
- print('importReference(), the references are:' + str(references))
- exportFile = references.pop(0)
- for eachReference in references:
- referenceFileName = cmds.referenceQuery(eachReference, filename = True)
- cmds.file(referenceFileName, importReference = True)
- cmds.namespace(set=':')#setting the namespace to the root
- namespaces = cmds.namespaceInfo(listOnlyNamespaces = True)#listing all namespaces in the scene
- for eachNamespace in namespaces:
- #removeNamespace and mergeNamespaceWithRoot work together to empty the namespace of objects (mergeNamespaceWithRoot) and then remove the namespace from the scene (removeNamespace)
- #you can't delete namespaces with objects in them.
- cmds.namespace(removeNamespace = eachNamespace, mergeNamespaceWithRoot = True)