Manage namespace in python maya.
py
from maya import cmds
# Filter reserved namespaces
# these namespaces can not be touched they are always there
_ignored_namespaces = ('UI', 'shared')
def get_namespaces(ignored_namespace=_ignored_namespaces):
"""Get namespace.
:param ignored_namespace: Tuple of ignored namespace.
:type ignored_namespace: tuple[str]
"""
cmds.namespace(setNamespace="::")
namespaces_raw = cmds.namespaceInfo(listOnlyNamespaces=True, recurse=True)
# Filter reserved namespaces
# these namespaces can not be touched they are always there
namespaces = filter(lambda x: x not in ignored_namespace, namespaces_raw)
return namespaces
def remove_namespaces(namespaces, ignored_namespace=_ignored_namespaces):
"""Remove namespace.
:param ignored_namespace:
:return:
"""
cmds.namespace(setNamespace="::")
namespaces_raw = cmds.namespaceInfo(listOnlyNamespaces=True, recurse=True)
namespaces = filter(lambda x: x not in _ignored_namespaces, namespaces_raw)
for namespace in reversed(sorted(namespaces)):
cmds.namespace(moveNamespace=(namespace, ':'), force=True)
# Remove the namespace to free the group name we want.
cmds.namespace(removeNamespace=namespace, mergeNamespaceWithRoot=True)