/

Maya – Remove all namespaces

When you gather all the namespaces in the scene, make sure you avoid the Maya built-in namespaces. Then start removing, deepest first. I usually use this to clean a scene after I have imported all references. If you still have references in the scene, this may cause unwanted object renames. In that case, proceed with caution.

from maya import cmds

# Set root namespace
cmds.namespace(setNamespace=':')

# Collect all namespaces except for the Maya built ins.
all_namespaces = [x for x in cmds.namespaceInfo(listOnlyNamespaces=True, recurse=True) if x != "UI" and x != "shared"]

if all_namespaces:
	# Sort by hierarchy, deepest first.
	all_namespaces.sort(key=len, reverse=True)
	for namespace in all_namespaces:
		# When a deep namespace is removed, it also removes the root. So check here to see if these still exist.
		if cmds.namespace(exists=namespace) is True:
			cmds.namespace(removeNamespace=namespace, mergeNamespaceWithRoot=True)

Similar Posts

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *