Skip to content

Instantly share code, notes, and snippets.

@mottosso
Last active May 11, 2016 20:46
Show Gist options
  • Save mottosso/b03b30388957db27104c to your computer and use it in GitHub Desktop.
Save mottosso/b03b30388957db27104c to your computer and use it in GitHub Desktop.
Remove all namespaces
from maya import cmds
def scene_has_referenced_namespace():
"""Return True if scene has referenced namespace(s)"""
for reference in cmds.ls(type="reference"):
for node in cmds.referenceQuery(reference, nodes=True):
if ":" in node:
return True
return False
def get_namespaces():
"""Return all namespaces, excluding defaults"""
namespaces = cmds.namespaceInfo(
":", listOnlyNamespaces=True, recurse=True)
namespaces.reverse()
return [ns for ns in namespaces if ns not in ("UI", "shared")]
def get_nodes(namespace=":"):
return cmds.namespaceInfo(namespace, listNamespace=True) or list()
def remove(rename=None):
"""Remove all namespaces; pass clashes to `rename`
Attributes:
rename (callable, optional): Function with which to process name clash.
Defaults to replacing ":" with "_"
Example:
remove(rename=lambda name: name.replace(":", "_"))
Returns:
Two dictionaries of clashing names and errors; e.g. {"ns:name": "ns_name"}, {}
"""
if scene_has_referenced_namespace():
raise ValueError("Scene has referenced namespaces; "
"import or remove references before proceeding")
if rename is None:
rename = lambda name: name.replace(":", "_")
clashes = {}
errors = {}
namespaces = get_namespaces()
nodes_in_root = get_nodes(":")
try:
namespace = namespaces[0]
while namespace:
nodes_in_namespace = get_nodes(namespace)
try:
node = nodes_in_namespace[0]
node_without_namespace = node.rsplit(":", 1)[-1]
while node_without_namespace in nodes_in_root:
new_name = rename(node_without_namespace)
clashes[node] = new_name
try:
cmds.rename(node, new_name)
except:
errors[node] = new_name
nodes_in_namespace = get_nodes(namespace)
node = nodes_in_namespace[0]
node_without_namespace = node.rsplit(":", 1)[-1]
except IndexError:
print "\"%s\" is clean." % namespace
cmds.namespace(removeNamespace=":" + namespace,
mergeNamespaceWithRoot=True)
namespaces = get_namespaces()
namespace = namespaces[0]
except IndexError:
print "Successfully removed all namespaces"
return clashes, errors
# Tests
def test_nested_namespaces():
nodes = {
"ns1": [
"node1",
"node2",
"node3"
],
"ns2": [
"node1",
"node2",
"node4"
],
"ns1:ns3": [
"node5",
"node1",
"node2"
],
"ns1:ns3:ns4:ns5:ns6": [
"node1",
"node2",
"node3"
]
}
for namespace, names in nodes.iteritems():
cmds.namespace(add=namespace)
cmds.namespace(set=namespace)
for name in names:
cmds.createNode("transform", name=name)
cmds.namespace(set=":")
remove()
assert not get_namespaces()
def test_referenced_file():
cmds.file(new=True, force=True)
cmds.namespace(add="ns1")
cmds.namespace(set="ns1")
node = cmds.createNode("transform", name="transform")
cmds.select(node)
cmds.namespace(set=":")
cmds.file(rename="_test.ma")
cmds.file(exportAll=True, force=True)
path = cmds.file(sceneName=True, query=True)
cmds.file(rename="__test.ma")
cmds.file(path, reference=True, namespace=":")
try:
remove()
except ValueError as e:
return
raise Exception("Error was not raised")
failures = dict()
for test in (test_nested_namespaces, test_referenced_file):
cmds.file(new=True, force=True)
try:
test()
except Exception as e:
failures[test] = e
if not failures:
print "Tests completed successfully"
else:
print "Tests failed:"
for test, fail in failures.iteritems():
print "\t\"%s\" failed: \"%s\"" % (test.__name__, fail)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment