def selected_namespaces(self): """:return: list(Namespace, ...) list of Namespace objects which have been selected""" out = list() for item_name in noneToList(self.p_selectItem): if item_name == self.kSelectedNodes: continue # END skip sel node special item ns = Namespace(item_name) out.append(ns) assert ns.exists(), "Selected namespace did not exist: %s " % ns # END for each item return out
def namespace( self ): """:return: namespace object of the full namespace holding all objects in this reference""" fullpath = self.path(copynumber=1) refspace = cmds.file( fullpath, q=1, ns=1 ) parentspace = cmds.file( fullpath, q=1, pns=1 )[0] # returns lists, although its always just one string if parentspace: parentspace += ":" # END handle parent namespace return Namespace( ":" + parentspace + refspace )
def create(cls, filepath, namespace=None, load = True, **kwargs): """Create a reference with the given namespace :param filepath: path describing the reference file location :param namespace: if None, a unique namespace will be generated for you The namespace will contain all referenced objects. :param load: if True, the reference will be created in loaded state, other wise its loading is deferred :param kwargs: passed to file command :raise ValueError: if the namespace does already exist :raise RuntimeError: if the reference could not be created""" filepath = make_path(cls._splitCopyNumber(filepath)[0]) def nsfunc(base, i): if not i: return base return "%s%i" % (base,i) ns = namespace if not ns: # assure unique namespace nsbasename = filepath.stripext().basename() ns = Namespace.findUnique(nsbasename, incrementFunc = nsfunc) else: ns = Namespace(ns) # assure we have a namespace object ns = ns.relativeTo(Namespace(Namespace.rootpath)) if ns.exists(): raise ValueError("Namespace %s for %s does already exist" % (ns,filepath)) # assure we keep the current namespace prevns = Namespace.current() # removing duplicate **kwargs kwargs.pop('ns', None) kwargs.pop('reference', kwargs.pop('r', None)) kwargs.pop('deferReference', kwargs.pop('dr', None)) try: createdRefpath = cmds.file(filepath, ns=str(ns),r=1,dr=not load, **kwargs) finally: prevns.setCurrent() # END assure we keep the namespace return FileReference(createdRefpath)
def create( cls, filepath, namespace=None, load = True, **kwargs ): """Create a reference with the given namespace :param filepath: path describing the reference file location :param namespace: if None, a unique namespace will be generated for you The namespace will contain all referenced objects. :param load: if True, the reference will be created in loaded state, other wise its loading is deferred :param kwargs: passed to file command :raise ValueError: if the namespace does already exist :raise RuntimeError: if the reference could not be created""" filepath = make_path( cls._splitCopyNumber( filepath )[0] ) def nsfunc( base, i ): if not i: return base return "%s%i" % ( base,i ) ns = namespace if not ns: # assure unique namespace nsbasename = filepath.stripext().basename() ns = Namespace.findUnique( nsbasename, incrementFunc = nsfunc ) else: ns = Namespace( ns ) # assure we have a namespace object ns = ns.relativeTo( Namespace( Namespace.rootpath ) ) if ns.exists(): raise ValueError( "Namespace %s for %s does already exist" % (ns,filepath) ) # assure we keep the current namespace prevns = Namespace.current() # removing duplicate **kwargs kwargs.pop('ns', None) kwargs.pop('reference', kwargs.pop('r', None)) kwargs.pop('deferReference', kwargs.pop('dr', None)) try: createdRefpath = cmds.file( filepath, ns=str(ns),r=1,dr=not load, **kwargs ) finally: prevns.setCurrent( ) # END assure we keep the namespace return FileReference( createdRefpath )
def test_export_import( self ): def iter_dag(): return nt.iterDgNodes(nt.api.MFn.kDagNode, asNode=0) ah = AnimationHandle.create() ah.set_animation(iter_dag()) # test iter_animation managed = len(list(ah.iter_animation(asNode=0))) assert managed == len(ah.affectedBy) # test iter_animtion return types assert isinstance(ah.iter_animation().next(), nt.Node) assert isinstance(ah.iter_animation(asNode=0).next(), nt.api.MObject) # selection is maintained across exports slist = nt.toSelectionList(iter_dag()) nt.select(slist) ## EXPORT ## filename = ospath.join(tempfile.gettempdir(), "test_export2.ani.ma") assert filename == ah.to_file(filename, force=True, type="mayaAscii") # check if testselection is still alive assert len(slist)==len(nt.activeSelectionList()) # AnimationHandle deletes correctly when not referenced ahname = ah.name() ah.delete() assert not ah.isValid() ## IMPORT ## # try different namespaces - it should work no matter which namespace # is current namespaces=(":", "not:in:rootns", "not") # dummyAnimationHandle for iteration tests dummy=AnimationHandle() for namespace in namespaces: sns = Namespace.create(namespace) sns.setCurrent() # check return values of from_file and get AnimationHandle ahref, ahit = AnimationHandle.from_file(filename) assert isinstance(ahref, FileReference) loaded_ah = ahit.next() assert isinstance(loaded_ah, AnimationHandle) # expecting only one AnimationHandle from iterator (no dummyAnimationHandle) # which is in our scene already assert len(list(ahit)) == 0 # check if AnimationHandle is the one we saved before loaded_ah_ns = loaded_ah.namespace() assert loaded_ah_ns + ahname == Namespace.rootpath + loaded_ah.name() # check if AnimationHandle is from file we wanted assert ospath.realpath(filename) == ospath.realpath(loaded_ah.referenceFile()) # stored and loaded managed animCurves are in sync assert managed == len(loaded_ah.affectedBy) # AnimationHandle deletes correctly when referenced loaded_ah.delete() assert not loaded_ah.isValid() # END test different namespaces os.remove(filename)