示例#1
0
    def saveValidator(self, **kwargs):
        """
        The save validator is called when an input field has changed.

        :type kwargs: dict
        :rtype: list[dict]
        """
        fields = super(AnimItem, self).saveValidator(**kwargs)

        # Validate the by frame field
        if kwargs.get("byFrame") == '' or kwargs.get("byFrame", 1) < 1:
            msg = "The by frame value cannot be less than 1!"
            fields.extend([{"name": "byFrame", "error": msg}])

        # Validate the frame range field
        start, end = kwargs.get("frameRange", (0, 1))
        if start >= end:
            msg = "The start frame cannot be greater " \
                  "than or equal to the end frame!"
            fields.extend([{"name": "frameRange", "error": msg}])

        # Validate the current selection field
        selection = maya.cmds.ls(selection=True) or []
        if selection and mutils.getDurationFromNodes(selection) <= 0:
            msg = "No animation was found on the selected object/s! " \
                  "Please create a pose instead!"
            fields.extend([{
                "name": "contains",
                "error": msg,
            }])

        return fields
示例#2
0
 def accept(self):
     """
     :raise AnimationPluginError:
     """
     msg = 'An error has occurred while saving the animation! Please check the script editor for the traceback.'
     mayaBasePlugin.CreateWidget.accept(self)
     try:
         record = self.record()
         gSelectedAnimLayers = maya.mel.eval('$a = $gSelectedAnimLayers;')
         if len(gSelectedAnimLayers) > 1:
             msg = 'More than one animation layer selected! Please select only one animation layer for export!'
             raise AnimationPluginError(msg)
         if self.startFrame() is None or self.endFrame() is None:
             msg = 'Please specify a start and end frame!'
             raise AnimationPluginError(msg)
         if self.startFrame() >= self.endFrame():
             msg = 'The start frame cannot be greater than or equal to the end frame!'
             raise AnimationPluginError(msg)
         if mutils.getDurationFromNodes(
                 nodes=maya.cmds.ls(selection=True) or []) <= 0:
             msg = 'No animation was found on the selected objects! Please create a pose instead!'
             raise AnimationPluginError(msg)
         path = studioLibrary.tempDir(make=True,
                                      clean=True,
                                      subdir='animation.anim')
         objects = maya.cmds.ls(selection=True)
         bakeConnected = int(self.ui.bakeCheckBox.isChecked())
         a = mutils.Animation.createFromObjects(objects)
         a.save(path,
                time=(self.startFrame(), self.endFrame()),
                bakeConnected=bakeConnected)
         content = a.paths()
         record.set('start', self.startFrame())
         record.set('end', self.endFrame())
         if self._sequence:
             sequence = os.path.dirname(self._sequence)
             if os.path.exists(sequence):
                 content.append(sequence)
         record.save(content=content, icon=self.thumbnail())
     except Exception:
         self.record().window().setError(msg)
         raise
示例#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)
示例#4
0
文件: animation.py 项目: jonntd/mira
    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)
示例#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)
示例#6
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()
        logger.debug(("objects = {0}").format(objects))

        fileType = fileType or DEFAULT_FILE_TYPE

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

        # Check selected animation layers
        validateAnimLayers()

        # 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(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
        validCurves = []
        deleteObjects = []

        msg = u"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"):

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

                    # if not FIX_SAVE_ANIM_REFERENCE_LOCKED_ERROR:
                    #     mutils.disconnectAll(transform[0])

                    # deleteObjects.append(transform[0])
                    # maya.cmds.pasteKey(transform[0])

                    attrs = []
                    name_type_list = maya.cmds.ls(name, showType = True)
                    if name_type_list[1] == BLEND_SHAPE_TYPE:
                        attrs = self.getBlendshapeParamList(name)
                    else:
                        attrs = maya.cmds.listAttr(name, unlocked=True, keyable=True) or []
                    attrs = list(set(attrs) - set(['translate', 'rotate', 'scale']))

                    logger.debug(("attrs = {0}").format(attrs))
                    for attr in attrs:
                        logger.debug(("transform name = {0}, attr name = {1}").format(name, attr))
                        dstAttr = mutils.Attribute(name, attr)
                        dstCurve = dstAttr.animCurve()

                        if dstCurve:

                            # dstCurve = maya.cmds.rename(dstCurve, "CURVE")
                            # deleteObjects.append(dstCurve)

                            # srcAttr = mutils.Attribute(name, attr)
                            # srcCurve = srcAttr.animCurve()

                            # if 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)

                            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))
                                validCurves.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 validCurves:
                maya.cmds.select(validCurves)
                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 deleteObjects:
                maya.cmds.delete(deleteObjects)

        self.setPath(path)
示例#7
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
        validateAnimLayers()

        # 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(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
        validCurves = []
        deleteObjects = []

        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)

                    if not FIX_SAVE_ANIM_REFERENCE_LOCKED_ERROR:
                        mutils.disconnectAll(transform)

                    deleteObjects.append(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:
                        dstAttr = mutils.Attribute(transform, attr)
                        dstCurve = dstAttr.animCurve()

                        if dstCurve:

                            dstCurve = maya.cmds.rename(dstCurve, "CURVE")
                            deleteObjects.append(dstCurve)

                            srcAttr = mutils.Attribute(name, attr)
                            srcCurve = srcAttr.animCurve()

                            if 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)

                            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))
                                validCurves.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 validCurves:
                maya.cmds.select(validCurves)
                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 deleteObjects:
                maya.cmds.delete(deleteObjects)

        self.setPath(path)
示例#8
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()
        logger.debug(("objects = {0}").format(objects))

        fileType = fileType or DEFAULT_FILE_TYPE

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

        # Check selected animation layers
        validateAnimLayers()

        # 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(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
        validCurves = []
        # deleteObjects = []

        # msg = u"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"):

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

                    self.listAttachedAnimCurves(name)
                    for attr in attrs:
                        dstAttr = mutils.Attribute(name, attr)
                        dstCurve = dstAttr.animCurve()
                        logger.debug(("dstCurve = {0}").format(dstCurve))

                        if dstCurve:

                            srcAttr = mutils.Attribute(name, attr)
                            srcCurve = srcAttr.animCurve()

                            if maya.cmds.keyframe(dstCurve,
                                                  query=True,
                                                  time=(start, end),
                                                  keyframeCount=True):
                                self.setAnimCurve(name, attr, dstCurve)
                                validCurves.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 validCurves:
                maya.cmds.select(validCurves)
                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()

        self.setPath(path)