Example #1
0
 def updateCache(self,
                 objects=None,
                 namespaces=None,
                 dstAttrs=None,
                 ignoreConnected=False,
                 onlyConnected=False,
                 cache=True,
                 mirrorTable=None):
     """
     @type objects: list[str]
     @type namespaces: list[str]
     @type dstAttrs: list[str]
     """
     cacheKey = str(objects) + str(namespaces) + str(dstAttrs) + str(
         ignoreConnected) + str(maya.cmds.currentTime(query=True))
     if self._cacheKey != cacheKey or not cache:
         self._cache = []
         self._cacheKey = cacheKey
         dstObjects = objects
         srcObjects = self.objects()
         usingNamespaces = not objects and namespaces
         if mirrorTable:
             self.setMirrorTable(mirrorTable)
         mutils.matchObjects(srcObjects,
                             dstObjects=dstObjects,
                             dstNamespaces=namespaces,
                             callback=self.cacheNode,
                             dstAttrs=dstAttrs,
                             ignoreConnected=ignoreConnected,
                             onlyConnected=onlyConnected,
                             usingNamespaces=usingNamespaces)
     if not self.cache():
         raise mutils.NoMatchFoundError(
             'No objects match when loading data')
Example #2
0
    def readMayaPath(self, path):
        """
        @type path: str
        """
        if not os.path.exists(path):
            return
        f = open(path)
        lines = f.readlines()
        f.close()
        curve = None
        srcObjects = {}
        for i, line in enumerate(lines):
            if line.startswith('createNode animCurve'):
                curve = line.split('"')[1]
            elif curve and '.fullname' in line:
                try:
                    name, attr = line.split('"')[5].split('.')
                except IndexError as msg:
                    name, attr = lines[i + 1].split('"')[1].split('.')

                srcObjects.setdefault(name, {})
                srcObjects[name][attr] = curve
                curve = None

        matches = mutils.matchObjects(srcObjects=srcObjects.keys(),
                                      dstObjects=self.objects().keys())
        for srcNode, dstNode in matches:
            for attr in srcObjects[srcNode.name()]:
                self.setAttrCurve(dstNode.name(), attr,
                                  srcObjects[srcNode.name()][attr])
Example #3
0
    def load(self, objects=None, namespaces=None, **kwargs):
        """
        @type objects:
        @type namespaces: list[str]
        @type kwargs:
        """
        validNodes = []
        dstObjects = objects
        srcObjects = self.objects()
        matches = mutils.matchObjects(srcObjects,
                                      dstObjects=dstObjects,
                                      dstNamespaces=namespaces)
        if matches:
            for srcNode, dstNode in matches:
                try:
                    if '*' in dstNode.name():
                        validNodes.append(dstNode.name())
                    else:
                        dstNode.stripFirstPipe()
                        validNodes.append(dstNode.toShortName())
                except mutils.NoObjectFoundError as msg:
                    log.debug(msg)
                except mutils.MoreThanOneObjectFoundError as msg:
                    log.debug(msg)

        if validNodes:
            maya.cmds.select(validNodes, **kwargs)
        else:
            raise mutils.NoMatchFoundError(
                'No objects match when loading data')
Example #4
0
    def readMayaPath(self, path):
        """
        @type path: str
        """
        if not os.path.exists(path):
            return
        f = open(path)
        lines = f.readlines()
        f.close()
        curve = None
        srcObjects = {}
        for i, line in enumerate(lines):
            if line.startswith('createNode animCurve'):
                curve = line.split('"')[1]
            elif curve and '.fullname' in line:
                try:
                    name, attr = line.split('"')[5].split('.')
                except IndexError as msg:
                    name, attr = lines[i + 1].split('"')[1].split('.')

                srcObjects.setdefault(name, {})
                srcObjects[name][attr] = curve
                curve = None

        matches = mutils.matchObjects(srcObjects=srcObjects.keys(), dstObjects=self.objects().keys())
        for srcNode, dstNode in matches:
            for attr in srcObjects[srcNode.name()]:
                self.setAttrCurve(dstNode.name(), attr, srcObjects[srcNode.name()][attr])
Example #5
0
    def load(self, objects = None, namespaces = None, **kwargs):
        """
        @type objects:
        @type namespaces: list[str]
        @type kwargs:
        """
        validNodes = []
        dstObjects = objects
        srcObjects = self.objects()
        matches = mutils.matchObjects(srcObjects, dstObjects=dstObjects, dstNamespaces=namespaces)
        if matches:
            for srcNode, dstNode in matches:
                try:
                    if '*' in dstNode.name():
                        validNodes.append(dstNode.name())
                    else:
                        dstNode.stripFirstPipe()
                        validNodes.append(dstNode.toShortName())
                except mutils.NoObjectFoundError as msg:
                    log.debug(msg)
                except mutils.MoreThanOneObjectFoundError as msg:
                    log.debug(msg)

        if validNodes:
            maya.cmds.select(validNodes, **kwargs)
        else:
            raise mutils.NoMatchFoundError('No objects match when loading data')
Example #6
0
    def load(self, objects = None, namespaces = None, option = MirrorOption.Swap, animation = True, time = None):
        """
        @param objects: list[str]
        @param namespaces: list[str]
        @param option: mirrorOptions
        """
        srcObjects = self.objects().keys()
        matches = mutils.matchObjects(srcObjects=srcObjects, dstObjects=objects, dstNamespaces=namespaces)
        foundObject = False
        results = {}
        for srcNode, dstNode in matches:
            dstObj = dstNode.name()
            dstObj2 = self.mirrorObject(dstObj) or dstObj
            if dstObj2 not in results:
                mirrorAxis = self.mirrorAxis(srcNode.name())
                results[dstObj] = dstObj2
                dstObjExists = maya.cmds.objExists(dstObj)
                dstObj2Exists = maya.cmds.objExists(dstObj2)
                if dstObjExists and dstObj2Exists:
                    foundObject = True
                    if animation:
                        self.transferAnimation(dstObj, dstObj2, mirrorAxis=mirrorAxis, option=option, time=time)
                    else:
                        self.transferStatic(dstObj, dstObj2, mirrorAxis=mirrorAxis, option=option)
                else:
                    if not dstObjExists:
                        log.debug('Cannot find destination object %s' % dstObj)
                    if not dstObj2Exists:
                        log.debug('Cannot find mirrored destination object %s' % dstObj2)

        if not foundObject:
            raise mutils.NoMatchFoundError('No objects match when loading data')
Example #7
0
    def load(self, objects = None, namespaces = None, option = MirrorOption.Swap, animation = True, time = None):
        """
        @param objects: list[str]
        @param namespaces: list[str]
        @param option: mirrorOptions
        """
        srcObjects = self.objects().keys()
        matches = mutils.matchObjects(srcObjects=srcObjects, dstObjects=objects, dstNamespaces=namespaces)
        foundObject = False
        results = {}
        for srcNode, dstNode in matches:
            dstObj = dstNode.name()
            dstObj2 = self.mirrorObject(dstObj) or dstObj
            if dstObj2 not in results:
                mirrorAxis = self.mirrorAxis(srcNode.name())
                results[dstObj] = dstObj2
                dstObjExists = maya.cmds.objExists(dstObj)
                dstObj2Exists = maya.cmds.objExists(dstObj2)
                if dstObjExists and dstObj2Exists:
                    foundObject = True
                    if animation:
                        self.transferAnimation(dstObj, dstObj2, mirrorAxis=mirrorAxis, option=option, time=time)
                    else:
                        self.transferStatic(dstObj, dstObj2, mirrorAxis=mirrorAxis, option=option)
                else:
                    if not dstObjExists:
                        log.debug('Cannot find destination object %s' % dstObj)
                    if not dstObj2Exists:
                        log.debug('Cannot find mirrored destination object %s' % dstObj2)

        if not foundObject:
            raise mutils.NoMatchFoundError('No objects match when loading data')
Example #8
0
    def matchObjects(self, expectedResult, srcObjects = None, dstObjects = None, dstNamespaces = None):
        """
        """
        result = []
        for srcNode, dstNode in mutils.matchObjects(srcObjects, dstObjects=dstObjects, dstNamespaces=dstNamespaces):
            result.append((srcNode.name(), dstNode.name()))

        if result != expectedResult:
            raise Exception('Result does not match the expected result: %s != %s' % (str(result), expectedResult))
Example #9
0
    def matchObjects(self, expectedResult, srcObjects = None, dstObjects = None, dstNamespaces = None):
        """
        """
        result = []
        for srcNode, dstNode in mutils.matchObjects(srcObjects, dstObjects=dstObjects, dstNamespaces=dstNamespaces):
            result.append((srcNode.name(), dstNode.name()))

        if result != expectedResult:
            raise Exception('Result does not match the expected result: %s != %s' % (str(result), expectedResult))
Example #10
0
 def updateCache(self, objects = None, namespaces = None, dstAttrs = None, ignoreConnected = False, onlyConnected = False, cache = True, mirrorTable = None):
     """
     @type objects: list[str]
     @type namespaces: list[str]
     @type dstAttrs: list[str]
     """
     cacheKey = str(objects) + str(namespaces) + str(dstAttrs) + str(ignoreConnected) + str(maya.cmds.currentTime(query=True))
     if self._cacheKey != cacheKey or not cache:
         self._cache = []
         self._cacheKey = cacheKey
         dstObjects = objects
         srcObjects = self.objects()
         usingNamespaces = not objects and namespaces
         if mirrorTable:
             self.setMirrorTable(mirrorTable)
         mutils.matchObjects(srcObjects, dstObjects=dstObjects, dstNamespaces=namespaces, callback=self.cacheNode, dstAttrs=dstAttrs, ignoreConnected=ignoreConnected, onlyConnected=onlyConnected, usingNamespaces=usingNamespaces)
     if not self.cache():
         raise mutils.NoMatchFoundError('No objects match when loading data')
Example #11
0
    def load(self, objects = None, namespaces = None, start = None, sourceTime = None, attrs = None, currentTime = None, option = Option.ReplaceCompletely, connect = False, mirrorTable = None):
        """
        @type objects: list[str]
        @type namespaces: list[str]
        @type start: int
        @type sourceTime: (int, int)
        @type attrs: list[str]
        @type option: Option
        @type currentTime: bool
        """
        if option == Option.ReplaceAll:
            option = Option.ReplaceCompletely
        log.debug('Animation.load(objects=%s, option=%s, namespaces=%s, srcTime=%s, currentTime=%s)' % (len(objects),
         str(option),
         str(sourceTime),
         str(namespaces),
         str(currentTime)))
        if currentTime and start is None:
            start = maya.cmds.currentTime(query=True)
        try:
            srcCurves = self.open()
            srcObjects = self.objects().keys()
            srcTime = self.srcTime(sourceTime, srcCurves)
            dstTime = self.dstTime(srcTime, start)
            if mirrorTable:
                self.setMirrorTable(mirrorTable)
            maya.cmds.flushUndo()
            maya.cmds.undoInfo(openChunk=True)
            matches = mutils.matchObjects(srcObjects=srcObjects, dstObjects=objects, dstNamespaces=namespaces)
            if not matches:
                raise mutils.NoMatchFoundError('No objects match when loading data')
            if option != Option.ReplaceCompletely:
                insertSourceKeyframe(srcCurves, srcTime)
            for srcNode, dstNode in matches:
                dstNode.stripFirstPipe()
                for attr in self.attrs(srcNode.name()):
                    if attrs is not None and attr not in attrs:
                        continue
                    dstAttr = mutils.Attribute(dstNode.name(), attr)
                    srcCurve = self.attrCurve(srcNode.name(), attr, withNamespace=True)
                    if not dstAttr.exists():
                        log.debug('Skipping attribute: The destination attribute "%s.%s" does not exist!' % (dstAttr.name(), dstAttr.attr()))
                        continue
                    if srcCurve is None:
                        type_ = self.attrType(srcNode.name(), attr)
                        value = self.attrValue(srcNode.name(), attr)
                        srcAttr = mutils.Attribute(dstNode.name(), attr, type=type_, value=value)
                        self.setStaticKey(srcAttr, dstAttr, dstTime, option)
                    else:
                        self.setAnimationKey(srcCurve, dstAttr, time=dstTime, option=option, source=srcTime, connect=connect)

        finally:
            self.close()
            maya.cmds.undoInfo(closeChunk=True)
Example #12
0
    def matchObjects(self, objects = None, namespaces = None, selection = False, callback = None):
        """
        @param objects:
        @param namespaces:
        @param selection:
        @param callback:
        @return:
        """
        srcObjects = self.objects().keys()
        matches = mutils.matchObjects(srcObjects=srcObjects, dstObjects=objects, dstNamespaces=namespaces, selection=selection)
        results = {}
        for srcNode, dstNode in matches:
            dstObj = dstNode.name()
            mirrorAxis = self.mirrorAxis(srcNode.name())
            callback(srcNode.name(), dstObj, mirrorAxis)

        return results
Example #13
0
    def matchObjects(self, objects = None, namespaces = None, selection = False, callback = None):
        """
        @param objects:
        @param namespaces:
        @param selection:
        @param callback:
        @return:
        """
        srcObjects = self.objects().keys()
        matches = mutils.matchObjects(srcObjects=srcObjects, dstObjects=objects, dstNamespaces=namespaces, selection=selection)
        results = {}
        for srcNode, dstNode in matches:
            dstObj = dstNode.name()
            mirrorAxis = self.mirrorAxis(srcNode.name())
            callback(srcNode.name(), dstObj, mirrorAxis)

        return results
Example #14
0
    def load(self,
             objects=None,
             namespaces=None,
             start=None,
             sourceTime=None,
             attrs=None,
             currentTime=None,
             option=Option.ReplaceCompletely,
             connect=False,
             mirrorTable=None):
        """
        @type objects: list[str]
        @type namespaces: list[str]
        @type start: int
        @type sourceTime: (int, int)
        @type attrs: list[str]
        @type option: Option
        @type currentTime: bool
        """
        if option == Option.ReplaceAll:
            option = Option.ReplaceCompletely
        log.debug(
            'Animation.load(objects=%s, option=%s, namespaces=%s, srcTime=%s, currentTime=%s)'
            % (len(objects), str(option), str(sourceTime), str(namespaces),
               str(currentTime)))
        if currentTime and start is None:
            start = maya.cmds.currentTime(query=True)
        try:
            srcCurves = self.open()
            srcObjects = self.objects().keys()
            srcTime = self.srcTime(sourceTime, srcCurves)
            dstTime = self.dstTime(srcTime, start)
            if mirrorTable:
                self.setMirrorTable(mirrorTable)
            maya.cmds.flushUndo()
            maya.cmds.undoInfo(openChunk=True)
            matches = mutils.matchObjects(srcObjects=srcObjects,
                                          dstObjects=objects,
                                          dstNamespaces=namespaces)
            if not matches:
                raise mutils.NoMatchFoundError(
                    'No objects match when loading data')
            if option != Option.ReplaceCompletely:
                insertSourceKeyframe(srcCurves, srcTime)
            for srcNode, dstNode in matches:
                dstNode.stripFirstPipe()
                for attr in self.attrs(srcNode.name()):
                    if attrs is not None and attr not in attrs:
                        continue
                    dstAttr = mutils.Attribute(dstNode.name(), attr)
                    srcCurve = self.attrCurve(srcNode.name(),
                                              attr,
                                              withNamespace=True)
                    if not dstAttr.exists():
                        log.debug(
                            'Skipping attribute: The destination attribute "%s.%s" does not exist!'
                            % (dstAttr.name(), dstAttr.attr()))
                        continue
                    if srcCurve is None:
                        type_ = self.attrType(srcNode.name(), attr)
                        value = self.attrValue(srcNode.name(), attr)
                        srcAttr = mutils.Attribute(dstNode.name(),
                                                   attr,
                                                   type=type_,
                                                   value=value)
                        self.setStaticKey(srcAttr, dstAttr, dstTime, option)
                    else:
                        self.setAnimationKey(srcCurve,
                                             dstAttr,
                                             time=dstTime,
                                             option=option,
                                             source=srcTime,
                                             connect=connect)

        finally:
            self.close()
            maya.cmds.undoInfo(closeChunk=True)