May 3, 2013

Remove Namespaces Script

When you import in Maya it's hard to avoid namespaces. They pile up and 9 times out of 10 you don't want them.

Here's a simple script that will remove namespaces for you. Select any item in the namespace that you want to remove. If there are multiple nested namespaces, it will remove them one at a time.





#
# remove namespaces
#
# uses selected objects and only removes top namespace
import pymel.core as pm

def remove_namespaces() :
    # gather all namespaces from selection
    all_ns = []
    for obj in pm.selected() :
        if obj.namespace() :
            all_ns.append(obj.namespace())
    
    # remove dupes
    all_ns = list(set(all_ns)) 
    
    # try to remove the first namespace
    for whole_ns in all_ns :
        ns = whole_ns.split(':')[0]
        try :
            pm.namespace(mv=[ns,':'],f=1)
            if ns in pm.namespaceInfo(lon=1) :
                pm.namespace(rm=ns)
            print 'Namespace "%s" removed.'%ns
        except :
            warning('Namespace "%s" is not removable. Possibly from a reference.'%ns)
    
    return 1

remove_namespaces()

0 Comments:

Post a Comment