Esempio n. 1
0
 def setAnimationKey(self, srcCurve, dstAttribute, time, option, source = None, connect = False):
     """
     @type srcCurve: str
     @type dstAttribute: mutils.Attribute
     @type connect: bool
     @type option: Option
     @type time: (int, int)
     @type source: (int, int)
     """
     startTime, endTime = time
     if dstAttribute.exists() and dstAttribute.isUnlocked():
         if option == Option.Replace:
             maya.cmds.cutKey(dstAttribute.fullname(), time=time)
         if maya.cmds.copyKey(srcCurve, time=source):
             try:
                 if option != Option.Replace:
                     time = (startTime, startTime)
                 maya.cmds.pasteKey(dstAttribute.fullname(), option=option, time=time, connect=connect)
                 if option == Option.ReplaceCompletely:
                     dstCurve = mutils.animCurve(dstAttribute.fullname())
                     if dstCurve:
                         curveColor = maya.cmds.getAttr(srcCurve + '.curveColor')
                         preInfinity = maya.cmds.getAttr(srcCurve + '.preInfinity')
                         postInfinity = maya.cmds.getAttr(srcCurve + '.postInfinity')
                         useCurveColor = maya.cmds.getAttr(srcCurve + '.useCurveColor')
                         maya.cmds.setAttr(dstCurve + '.preInfinity', preInfinity)
                         maya.cmds.setAttr(dstCurve + '.postInfinity', postInfinity)
                         maya.cmds.setAttr((dstCurve + '.curveColor'), *curveColor[0])
                         maya.cmds.setAttr(dstCurve + '.useCurveColor', useCurveColor)
             except RuntimeError as e:
                 log.error('Cannot set animation for destination %s' % dstAttribute.fullname())
Esempio n. 2
0
 def setAnimationKey(self,
                     srcCurve,
                     dstAttribute,
                     time,
                     option,
                     source=None,
                     connect=False):
     """
     :type srcCurve: str
     :type dstAttribute: mutils.Attribute
     :type connect: bool
     :type option: PasteOption
     :type time: (int, int)
     :type source: (int, int)
     """
     startTime, endTime = time
     if dstAttribute.exists() and dstAttribute.isUnlocked():
         if option == PasteOption.Replace:
             maya.cmds.cutKey(dstAttribute.fullname(), time=time)
         if maya.cmds.copyKey(srcCurve, time=source):
             try:
                 if option != PasteOption.Replace:
                     time = (startTime, startTime)
                 maya.cmds.pasteKey(dstAttribute.fullname(),
                                    option=option,
                                    time=time,
                                    connect=connect)
                 if option == PasteOption.ReplaceCompletely:
                     dstCurve = mutils.animCurve(dstAttribute.fullname())
                     if dstCurve:
                         curveColor = maya.cmds.getAttr(srcCurve +
                                                        '.curveColor')
                         preInfinity = maya.cmds.getAttr(srcCurve +
                                                         '.preInfinity')
                         postInfinity = maya.cmds.getAttr(srcCurve +
                                                          '.postInfinity')
                         useCurveColor = maya.cmds.getAttr(srcCurve +
                                                           '.useCurveColor')
                         maya.cmds.setAttr(dstCurve + '.preInfinity',
                                           preInfinity)
                         maya.cmds.setAttr(dstCurve + '.postInfinity',
                                           postInfinity)
                         maya.cmds.setAttr((dstCurve + '.curveColor'),
                                           *curveColor[0])
                         maya.cmds.setAttr(dstCurve + '.useCurveColor',
                                           useCurveColor)
             except RuntimeError as e:
                 logger.error('Cannot set animation for destination %s' %
                              dstAttribute.fullname())
Esempio n. 3
0
    def save(self,
             path,
             time=None,
             sampleBy=1,
             fileType="",
             bakeConnected=True):
        """
        Save all animation data from the objects set on the Anim object.

        :type path: str
        :type time: (int, int) or None
        :type sampleBy: int
        :type fileType: str
        :type bakeConnected: bool
        
        :rtype: None
        """
        objects = self.objects().keys()

        fileType = fileType or DEFAULT_FILE_TYPE

        if not time:
            time = mutils.selectedObjectsFrameRange(objects)
        start, end = time

        # Check selected animation layers
        gSelectedAnimLayers = maya.mel.eval(
            '$gSelectedAnimLayers=$gSelectedAnimLayers')
        if len(gSelectedAnimLayers) > 1:
            msg = "More than one animation layer is selected! Please select only one animation layer for export!"
            raise AnimationTransferError(msg)

        # Check frame range
        if start is None or end is None:
            msg = "Please specify a start and end frame!"
            raise AnimationTransferError(msg)

        if start >= end:
            msg = "The start frame cannot be greater than or equal to the end frame!"
            raise AnimationTransferError(msg)

        # Check if animation exists
        if mutils.getDurationFromNodes(nodes=objects or []) <= 0:
            msg = "No animation was found on the specified object/s! Please create a pose instead!"
            raise AnimationTransferError(msg)

        self.setMetadata("endFrame", end)
        self.setMetadata("startFrame", start)

        end += 1
        dstCurves = []
        validAnimCurves = []

        msg = "Animation.save(path={0}, time={1}, bakeConnections={2}, sampleBy={3})"
        msg = msg.format(path, str(time), str(bakeConnected), str(sampleBy))
        logger.debug(msg)

        try:
            if bakeConnected:
                maya.cmds.undoInfo(openChunk=True)
                mutils.bakeConnected(objects,
                                     time=(start, end),
                                     sampleBy=sampleBy)

            for name in objects:
                if maya.cmds.copyKey(name,
                                     time=(start, end),
                                     includeUpperBound=False,
                                     option="keys"):

                    # Might return more than one object when duplicating shapes or blendshapes
                    transform, = maya.cmds.duplicate(name,
                                                     name="CURVE",
                                                     parentOnly=True)

                    dstCurves.append(transform)
                    mutils.disconnectAll(transform)
                    maya.cmds.pasteKey(transform)

                    attrs = maya.cmds.listAttr(
                        transform, unlocked=True, keyable=True) or []
                    attrs = list(
                        set(attrs) - set(['translate', 'rotate', 'scale']))

                    for attr in attrs:
                        fullname = ("%s.%s" % (transform, attr))
                        dstCurve, = maya.cmds.listConnections(
                            fullname, destination=False) or [None]

                        if dstCurve:
                            # Filter to only animCurves since you can have proxy attributes
                            if not maya.cmds.nodeType(dstCurve).startswith(
                                    "animCurve"):
                                continue

                            dstCurve = maya.cmds.rename(dstCurve, "CURVE")
                            srcCurve = mutils.animCurve("%s.%s" % (name, attr))
                            if srcCurve and "animCurve" in maya.cmds.nodeType(
                                    srcCurve):
                                preInfinity = maya.cmds.getAttr(srcCurve +
                                                                ".preInfinity")
                                postInfinity = maya.cmds.getAttr(
                                    srcCurve + ".postInfinity")
                                curveColor = maya.cmds.getAttr(srcCurve +
                                                               ".curveColor")
                                useCurveColor = maya.cmds.getAttr(
                                    srcCurve + ".useCurveColor")

                                maya.cmds.setAttr(dstCurve + ".preInfinity",
                                                  preInfinity)
                                maya.cmds.setAttr(dstCurve + ".postInfinity",
                                                  postInfinity)
                                maya.cmds.setAttr(dstCurve + ".curveColor",
                                                  *curveColor[0])
                                maya.cmds.setAttr(dstCurve + ".useCurveColor",
                                                  useCurveColor)

                            dstCurves.append(dstCurve)

                            if maya.cmds.keyframe(dstCurve,
                                                  query=True,
                                                  time=(start, end),
                                                  keyframeCount=True):
                                self.setAnimCurve(name, attr, dstCurve)
                                maya.cmds.cutKey(dstCurve,
                                                 time=(MIN_TIME_LIMIT,
                                                       start - 1))
                                maya.cmds.cutKey(dstCurve,
                                                 time=(end + 1,
                                                       MAX_TIME_LIMIT))
                                validAnimCurves.append(dstCurve)

            fileName = "animation.ma"
            if fileType == "mayaBinary":
                fileName = "animation.mb"

            mayaPath = os.path.join(path, fileName)
            posePath = os.path.join(path, "pose.json")
            mutils.Pose.save(self, posePath)

            if validAnimCurves:
                maya.cmds.select(validAnimCurves)
                logger.info("Saving animation: %s" % mayaPath)
                maya.cmds.file(mayaPath,
                               force=True,
                               options='v=0',
                               type=fileType,
                               uiConfiguration=False,
                               exportSelected=True)
                self.cleanMayaFile(mayaPath)

        finally:
            if bakeConnected:
                # HACK! Undo all baked connections. :)
                maya.cmds.undoInfo(closeChunk=True)
                maya.cmds.undo()
            elif dstCurves:
                maya.cmds.delete(dstCurves)

        self.setPath(path)
Esempio n. 4
0
    def save(self, path, time=None, bakeConnected=True, sampleBy=1):
        """
        Save all animation data from the objects set on the Anim object.
        
        :type path: str
        :type time: (int, int) or NOne
        :type bakeConnected: bool
        :type sampleBy: int
        """
        objects = self.objects().keys()
        if not time:
            time = mutils.animationFrameRange(objects)
        start, end = time
        gSelectedAnimLayers = maya.mel.eval('$a = $gSelectedAnimLayers;')
        if len(gSelectedAnimLayers) > 1:
            msg = 'More than one animation layer is selected! Please select only one animation layer for export!'
            raise AnimationTransferError(msg)
        if start is None or end is None:
            msg = 'Please specify a start and end frame!'
            raise AnimationTransferError(msg)
        if start >= end:
            msg = 'The start frame cannot be greater than or equal to the end frame!'
            raise AnimationTransferError(msg)
        if mutils.getDurationFromNodes(nodes=objects or []) <= 0:
            msg = 'No animation was found on the specified object/s! Please create a pose instead!'
            raise AnimationTransferError(msg)
        self.setMetadata('endFrame', end)
        self.setMetadata('startFrame', start)
        end += 1
        dstCurves = []
        validAnimCurves = []
        msg = 'Animation.save(path={0}, time={1}, bakeConnections={2}, sampleBy={3})'
        msg = msg.format(path, str(time), str(bakeConnected), str(sampleBy))
        logger.debug(msg)
        try:
            if bakeConnected:
                maya.cmds.undoInfo(openChunk=True)
                mutils.bakeConnected(objects,
                                     time=(start, end),
                                     sampleBy=sampleBy)
            for name in objects:
                if maya.cmds.copyKey(name,
                                     time=(start, end),
                                     includeUpperBound=False,
                                     option='keys'):
                    transform, = maya.cmds.duplicate(name,
                                                     name='CURVE',
                                                     parentOnly=True)
                    dstCurves.append(transform)
                    mutils.disconnectAll(transform)
                    maya.cmds.pasteKey(transform)
                    attrs = maya.cmds.listAttr(
                        transform, unlocked=True, keyable=True) or []
                    attrs = list(
                        set(attrs) - set(['translate', 'rotate', 'scale']))
                    for attr in attrs:
                        fullname = '%s.%s' % (transform, attr)
                        dstCurve, = maya.cmds.listConnections(
                            fullname, destination=False) or [None]
                        if dstCurve:
                            dstCurve = maya.cmds.rename(dstCurve, 'CURVE')
                            srcCurve = mutils.animCurve('%s.%s' % (name, attr))
                            if srcCurve and 'animCurve' in maya.cmds.nodeType(
                                    srcCurve):
                                preInfinity = maya.cmds.getAttr(srcCurve +
                                                                '.preInfinity')
                                postInfinity = maya.cmds.getAttr(
                                    srcCurve + '.postInfinity')
                                curveColor = maya.cmds.getAttr(srcCurve +
                                                               '.curveColor')
                                useCurveColor = maya.cmds.getAttr(
                                    srcCurve + '.useCurveColor')
                                maya.cmds.setAttr(dstCurve + '.preInfinity',
                                                  preInfinity)
                                maya.cmds.setAttr(dstCurve + '.postInfinity',
                                                  postInfinity)
                                maya.cmds.setAttr((dstCurve + '.curveColor'),
                                                  *curveColor[0])
                                maya.cmds.setAttr(dstCurve + '.useCurveColor',
                                                  useCurveColor)
                            dstCurves.append(dstCurve)
                            if maya.cmds.keyframe(dstCurve,
                                                  query=True,
                                                  time=(start, end),
                                                  keyframeCount=True):
                                self.setAnimCurve(name, attr, dstCurve)
                                maya.cmds.cutKey(dstCurve,
                                                 time=(MIN_TIME_LIMIT,
                                                       start - 1))
                                maya.cmds.cutKey(dstCurve,
                                                 time=(end + 1,
                                                       MAX_TIME_LIMIT))
                                validAnimCurves.append(dstCurve)

            mayaPath = os.path.join(path, 'animation.mb')
            posePath = os.path.join(path, 'pose.json')
            mutils.Pose.save(self, posePath)
            if validAnimCurves:
                maya.cmds.select(validAnimCurves)
                logger.info('Saving animation: %s' % mayaPath)
                maya.cmds.file(mayaPath,
                               force=True,
                               options='v=0',
                               type='mayaBinary',
                               uiConfiguration=False,
                               exportSelected=True)
                self.cleanMayaFile(mayaPath)
        finally:
            if bakeConnected:
                maya.cmds.undoInfo(closeChunk=True)
                maya.cmds.undo()
            elif dstCurves:
                maya.cmds.delete(dstCurves)

        self.setPath(path)
Esempio n. 5
0
    def save(self, path, time = None, compress = False, bakeConnected = True, sampleBy = 1):
        """
        @type time: (int, int)
        """
        objects = self.objects().keys()
        if not time:
            time = mutils.animationRange(objects)
        start, end = time
        gSelectedAnimLayers = maya.mel.eval('$a = $gSelectedAnimLayers;')
        if len(gSelectedAnimLayers) > 1:
            msg = 'More than one animation layer is selected! Please select only one animation layer for export!'
            raise AnimationTransferError(msg)
        if start is None or end is None:
            msg = 'Please specify a start and end frame!'
            raise AnimationTransferError(msg)
        if start >= end:
            msg = 'The start frame cannot be greater than or equal to the end frame!'
            raise AnimationTransferError(msg)
        if mutils.getDurationFromNodes(nodes=objects or []) <= 0:
            msg = 'No animation was found on the specified object/s! Please create a pose instead!'
            raise AnimationTransferError(msg)
        self.setMetadata('endFrame', end)
        self.setMetadata('startFrame', start)
        end += 1
        dstCurves = []
        validAnimCurves = []
        log.debug('Animation.save(path=%s, time=%s, compress=%s, bakeConnections=%s, sampleBy=%s)' % (path,
         str(time),
         str(compress),
         str(bakeConnected),
         str(sampleBy)))
        try:
            if bakeConnected:
                maya.cmds.undoInfo(openChunk=True)
                mutils.bakeConnected(objects, time=(start, end), sampleBy=sampleBy)
            for name in objects:
                if maya.cmds.copyKey(name, time=(start, end), includeUpperBound=False, option='keys'):
                    transform, = maya.cmds.duplicate(name, name='CURVE', parentOnly=True)
                    dstCurves.append(transform)
                    mutils.disconnectAll(transform)
                    maya.cmds.pasteKey(transform)
                    attrs = maya.cmds.listAttr(transform, unlocked=True, keyable=True) or []
                    attrs = list(set(attrs) - set(['translate', 'rotate', 'scale']))
                    for attr in attrs:
                        fullname = '%s.%s' % (transform, attr)
                        dstCurve, = maya.cmds.listConnections(fullname, destination=False) or [None]
                        if dstCurve:
                            dstCurve = maya.cmds.rename(dstCurve, 'CURVE')
                            srcCurve = mutils.animCurve('%s.%s' % (name, attr))
                            if srcCurve and 'animCurve' in maya.cmds.nodeType(srcCurve):
                                preInfinity = maya.cmds.getAttr(srcCurve + '.preInfinity')
                                postInfinity = maya.cmds.getAttr(srcCurve + '.postInfinity')
                                curveColor = maya.cmds.getAttr(srcCurve + '.curveColor')
                                useCurveColor = maya.cmds.getAttr(srcCurve + '.useCurveColor')
                                maya.cmds.setAttr(dstCurve + '.preInfinity', preInfinity)
                                maya.cmds.setAttr(dstCurve + '.postInfinity', postInfinity)
                                maya.cmds.setAttr((dstCurve + '.curveColor'), *curveColor[0])
                                maya.cmds.setAttr(dstCurve + '.useCurveColor', useCurveColor)
                            dstCurves.append(dstCurve)
                            if maya.cmds.keyframe(dstCurve, query=True, time=(start, end), keyframeCount=True):
                                self.setAttrCurve(name, attr, dstCurve)
                                maya.cmds.cutKey(dstCurve, time=(MIN_TIME_LIMIT, start - 1))
                                maya.cmds.cutKey(dstCurve, time=(end + 1, MAX_TIME_LIMIT))
                                validAnimCurves.append(dstCurve)

            mayaPath = os.path.join(path, 'animation.ma')
            posePath = os.path.join(path, 'pose.json')
            mutils.Pose.save(self, posePath)
            if validAnimCurves:
                maya.cmds.select(validAnimCurves)
                log.info('Saving animation: %s' % mayaPath)
                maya.cmds.file(mayaPath, force=True, options='v=0', type='mayaAscii', uiConfiguration=False, exportSelected=True)
                self.cleanMayaFile(mayaPath)
        finally:
            if bakeConnected:
                maya.cmds.undoInfo(closeChunk=True)
                maya.cmds.undo()
            elif dstCurves:
                maya.cmds.delete(dstCurves)

        self.setPath(path)
Esempio n. 6
0
    def save(self,
             path,
             time=None,
             compress=False,
             bakeConnected=True,
             sampleBy=1):
        """
        @type time: (int, int)
        """
        objects = self.objects().keys()
        if not time:
            time = mutils.animationRange(objects)
        start, end = time
        self.setMetadata('endFrame', end)
        self.setMetadata('startFrame', start)
        end += 1
        dstCurves = []
        validAnimCurves = []
        log.debug(
            'Animation.save(path=%s, time=%s, compress=%s, bakeConnections=%s, sampleBy=%s)'
            % (path, str(time), str(compress), str(bakeConnected),
               str(sampleBy)))
        try:
            if bakeConnected:
                maya.cmds.undoInfo(openChunk=True)
                mutils.bakeConnected(objects,
                                     time=(start, end),
                                     sampleBy=sampleBy)
            for name in objects:
                if maya.cmds.copyKey(name,
                                     time=(start, end),
                                     includeUpperBound=False,
                                     option='keys'):
                    transform, = maya.cmds.duplicate(name,
                                                     name='CURVE',
                                                     parentOnly=True)
                    dstCurves.append(transform)
                    mutils.disconnectAll(transform)
                    maya.cmds.pasteKey(transform)
                    attrs = maya.cmds.listAttr(
                        transform, unlocked=True, keyable=True) or []
                    attrs = list(
                        set(attrs) - set(['translate', 'rotate', 'scale']))
                    for attr in attrs:
                        fullname = '%s.%s' % (transform, attr)
                        dstCurve, = maya.cmds.listConnections(
                            fullname, destination=False) or [None]
                        if dstCurve:
                            dstCurve = maya.cmds.rename(dstCurve, 'CURVE')
                            srcCurve = mutils.animCurve('%s.%s' % (name, attr))
                            if srcCurve and 'animCurve' in maya.cmds.nodeType(
                                    srcCurve):
                                preInfinity = maya.cmds.getAttr(srcCurve +
                                                                '.preInfinity')
                                postInfinity = maya.cmds.getAttr(
                                    srcCurve + '.postInfinity')
                                curveColor = maya.cmds.getAttr(srcCurve +
                                                               '.curveColor')
                                useCurveColor = maya.cmds.getAttr(
                                    srcCurve + '.useCurveColor')
                                maya.cmds.setAttr(dstCurve + '.preInfinity',
                                                  preInfinity)
                                maya.cmds.setAttr(dstCurve + '.postInfinity',
                                                  postInfinity)
                                maya.cmds.setAttr((dstCurve + '.curveColor'),
                                                  *curveColor[0])
                                maya.cmds.setAttr(dstCurve + '.useCurveColor',
                                                  useCurveColor)
                            dstCurves.append(dstCurve)
                            if maya.cmds.keyframe(dstCurve,
                                                  query=True,
                                                  time=(start, end),
                                                  keyframeCount=True):
                                self.setAttrCurve(name, attr, dstCurve)
                                maya.cmds.cutKey(dstCurve,
                                                 time=(MIN_TIME_LIMIT,
                                                       start - 1))
                                maya.cmds.cutKey(dstCurve,
                                                 time=(end + 1,
                                                       MAX_TIME_LIMIT))
                                validAnimCurves.append(dstCurve)

            mayaPath = os.path.join(path, 'animation.ma')
            posePath = os.path.join(path, 'pose.json')
            mutils.Pose.save(self, posePath)
            if validAnimCurves:
                maya.cmds.select(validAnimCurves)
                log.info('Saving animation: %s' % mayaPath)
                maya.cmds.file(mayaPath,
                               force=True,
                               options='v=0',
                               type='mayaAscii',
                               uiConfiguration=False,
                               exportSelected=True)
                self.cleanMayaFile(mayaPath)
        finally:
            if bakeConnected:
                maya.cmds.undoInfo(closeChunk=True)
                maya.cmds.undo()
            elif dstCurves:
                maya.cmds.delete(dstCurves)

        self.setPath(path)