def delete( self, move_to_namespace = rootpath, autocreate=True ): """Delete this namespace and move it's obejcts to the given move_to_namespace :param move_to_namespace: if None, the namespace to be deleted must be empty If Namespace, objects in this namespace will be moved there prior to namespace deletion move_to_namespace must exist :param autocreate: if True, move_to_namespace will be created if it does not exist yet :note: can handle sub-namespaces properly :raise RuntimeError: :todo: Implement undo !""" if self == self.rootpath: raise ValueError( "Cannot delete root namespace" ) if not self.exists(): # its already gone - all fine return # assure we have a namespace type if move_to_namespace: move_to_namespace = self.__class__( move_to_namespace ) # assure we do not loose the current namespace - the maya methods could easily fail previousns = Namespace.current( ) cleanup = CallOnDeletion( None ) if previousns != self: # cannot reset future deleted namespace cleanup.callableobj = lambda : previousns.setCurrent() # recurse into children for deletion for childns in self.children( ): childns.delete( move_to_namespace = move_to_namespace ) # make ourselves current self.setCurrent( ) if move_to_namespace: self.moveNodes( move_to_namespace, autocreate=autocreate ) # finally delete the namespace cmds.namespace( rm=self )
def create( cls, namespaceName ): """Create a new namespace :param namespaceName: the name of the namespace, absolute or relative - it may contain subspaces too, i.e. :foo:bar. fred:subfred is a relative namespace, created in the currently active namespace :note: if the target namespace already exists, it will be returned :return: the create Namespace object""" newns = cls( namespaceName ) if newns.exists(): # skip work return newns cleanup = CallOnDeletion( None ) if newns.isAbsolute(): # assure root is current if we are having an absolute name previousns = Namespace.current() cls( Namespace.rootpath ).setCurrent( ) cleanup.callableobj = lambda : previousns.setCurrent() # create each token accordingly ( its not root here ) tokens = newns.split( newns._sep ) for i,token in enumerate( tokens ): # skip the root namespac base = cls( ':'.join( tokens[:i+1] ) ) if base.exists( ): continue # otherwise add the baes to its parent ( that should exist # put operation on the queue - as we create empty namespaces, we can delete # them at any time op = undo.GenericOperation( ) op.setDoitCmd( cmds.namespace, p=base.parent() , add=base.basename() ) op.setUndoitCmd(cmds.namespace, rm=base ) op.doIt( ) # END for each token return newns