Space Cleaner

Download:

Space Cleaner (12.8 KB) Download

Description:

Space Cleaner will cycle through each recursive namespace moving its contents to the root space and then deleting the empty namespaces. Defualt namespaces ( UI and shared) will be skipped as well as any namespaces that are being used by referenced files.

Installation:

Copy spaceCleaner.py to your Maya scripts directory, usually – documents\maya\version\scripts\

Create a shelf button, hotkey, etc with the following commands:

from spaceCleaner import *
spaceCleaner()

Source:

import maya.cmds as cmds

# Main Command
def spaceCleaner():
	root = ":"
	protect = ["UI","shared"]
	refs = cmds.file(r=True, q=True)
	for r in refs: protect.append(r)
	
	spaces = cmds.namespaceInfo(lon=True)
	spaces = list(set(spaces) - set(protect))

	cmds.namespace(set=root)
	while (len(spaces) > 0 ):
		cmds.namespace(set=root)
		
		for s in spaces:
			print ("removing namespace: " + s)
			cmds.namespace(mv=(s, root), f=True)
			cmds.namespace(rm=s)

		spaces = cmds.namespaceInfo(lon=True)
		spaces = list(set(spaces) - set(protect))
		
	print ("Finished cleaning up namespaces!");
	
	
# End Script