示例#1
0
def show():        #check modifyed
    if not sceneName():
        PopupError('Save scene before continue')
        return
    if cmds.file(q=True, modified=True) and not objExists(submitter_variables.root_group_name):
        if confirmDialog( title='Continue ?',
                          message='Scene have unsaved changes\n\nContinue without saving?',
                          button=['Yes','No'],
                          defaultButton='Yes',
                          cancelButton='No',
                          dismissString='No' ) == 'No':
            return
    def showUI():
        from . import submitter_window
        reload(submitter_window)
        w = submitter_window.SubmitterWindowClass()
        w.show()
    load_ai()
    QTimer.singleShot(1000, showUI)
示例#2
0
def load(filename,
         insertTime=None,
         alterPlug=None,
         bufferKeys=True,
         targetPool=None):
    '''
    Loads a file containing animCurves (made with `save`) and hooks them up.
    
    :param func alterPlug:  If the input needs some sort of transformation, provide
        a function that takes the plug string, ex "someSphere.tx" and returns
        a plug string of how it maps back, ex "zCube.tx" or "zCube.ty" and
        a function to alter the curve (or None)

        def alterPlug( 'inputNode.attr' ):
            return 'transformed'
        
    :param bool bufferKeys: If True (default), will add keys a frame before and
        after the range.
    '''
    global TAGGING_ATTR
    global _loadAlterPlug

    existingSelection = selected()

    # Hook for easily providing an alterPlug via the GUI
    if _loadAlterPlug and not alterPlug:
        alterPlug = _loadAlterPlug

    # Using cmds for speed
    getAttr = cmds.getAttr
    objExists = cmds.objExists
    ls = cmds.ls
    # ---

    if insertTime is None:
        insertTime = currentTime(q=True)

    missingObj = set()
    missingAttr = []
    pasteError = []

    newNodes = cmds.file(filename, i=True, rnn=True)

    curves = cmds.ls(newNodes, type='animCurve')
    info = ls(newNodes, type='network')[0]

    start = getAttr(info + '.start')
    end = getAttr(info + '.end')
    length = end - start

    attr = '.' + TAGGING_ATTR

    singleObj = ''

    if len(existingSelection) == 1:
        targetObj = getAttr(curves[0] + attr).split('.')[0]
        for c in curves:
            loadedTarget = getAttr(c + attr).split('.')[0]
            # FKIK_SWITCH is a hack to deal with the switching attr if a single
            # obj is selected
            if loadedTarget != targetObj and not loadedTarget.endswith(
                    'FKIK_SWITCH'):
                break
        else:
            singleObj = targetObj

    if singleObj:
        targetObj = existingSelection[0].longName()

        def alter(plug):
            return targetObj + '.' + plug.split('.')[-1], None
    else:
        # Determine if there is a namespace mismatch
        if alterPlug:
            targets = [
                alterPlug(cmds.getAttr(crv + attr))[0].split('.')[0]
                for crv in curves
            ]
        else:
            targets = [
                cmds.getAttr(crv + attr).split('.')[0] for crv in curves
            ]

        changeNamespace = None

        newTargets = core.names.findAlternates(targets, targetPool)

        global JUNK
        JUNK = targets

        if newTargets.alteration:
            print('NS change', '--' * 20, newTargets.alteration)
            if newTargets.alteration[0] == 'add':

                def changeNamespace(plug):
                    return newTargets.alteration[1] + plug
            elif newTargets.alteration[0] == 'sub':

                def changeNamespace(plug):
                    return plug.replace(newTargets.alteration[1],
                                        newTargets.alteration[2])
            elif newTargets.alteration[0] == 'rem':

                def changeNamespace(plug):
                    return plug.replace(newTargets.alteration[1], '')

        # Build an alteration function if needed
        alter = None
        if alterPlug and changeNamespace:

            def alter(plug):
                newPlug, curveEditFunc = alterPlug(changeNamespace(plug))
                return newPlug, curveEditFunc
        elif alterPlug:
            alter = alterPlug
        elif changeNamespace:

            def alter(plug):
                return changeNamespace(plug), None

    if hasAttr(PyNode(info), 'staticValues'):
        keys = json.loads(
            core.text.asciiDecompress(getAttr(info + '.staticValues')))

        for plug, value in keys.items():
            try:
                if alter:
                    setAttr(alter(plug), value)
                else:
                    setAttr(plug, value)
            except Exception:
                pass

    # Finally, actually copy over the animation
    for node in curves:
        alterCurve = None
        if objExists(node + '.' + TAGGING_ATTR):
            dest = getAttr(node + '.' + TAGGING_ATTR)
            if alter:
                dest, alterCurve = alter(dest)

            if alterCurve:
                alterCurve(node)

            if objExists(dest):

                # If we aren't going to be able to paste, just punt.
                if not getAttr(dest, k=True):
                    pasteError.append(dest)
                    continue

                if bufferKeys or getAttr(node, s=1) <= 1:
                    setKeyframe(node, time=(insertTime - 1), insert=True)
                    setKeyframe(node,
                                time=(insertTime + length + 1),
                                insert=True)

                copyKey(node, time=(start, end), iub=True, option='curve')

                try:
                    pasteKey(dest,
                             time=(insertTime, insertTime + length),
                             option='replace')
                except Exception:
                    pasteError.append(dest)
            else:
                obj, attr = dest.split('.')
                if objExists(obj):
                    missingAttr.append(dest)
                else:
                    missingObj.add(obj)

    if missingObj:
        print(
            core.text.writeInBox("These objects don't exist:\n\n" +
                                 '\n'.join(missingObj)))
    if missingAttr:
        print(
            core.text.writeInBox("These attribute couldn't be found:\n\n" +
                                 '\n'.join(missingAttr)))
    if pasteError:
        print(
            core.text.writeInBox(
                "Errors occurred when pasting animation onto:\n\n" +
                '\n'.join(pasteError)))

    if missingObj or missingAttr or pasteError:
        warning('Completed but with errors. See script editor for details.')

    delete(newNodes)

    return SavedCurveInfo(insertTime, insertTime + length, length)