def loadData(self, onlySelectedNodes=False, crashFolder=None, *args): cmds.waitCursor(state=True) cmds.refresh(suspend=True) cmds.undoInfo(openChunk=True) utilMod.startProgressBar("aTools Animation Crash Recovery - Loading data...") self.pause = True savedData = self.getSavedData(crashFolder, onlySelectedNodes) if savedData: animData = savedData["animData"]["data"] attrData = savedData["attrData"]["data"] self.applyAttrData(attrData) self.applyAnimData(animData) if not crashFolder: self.loadInfoData() utilMod.setProgressBar(endProgress=True) self.pause = False cmds.undoInfo(closeChunk=True) cmds.refresh(suspend=False) cmds.waitCursor(state=False)
def updateMotionTrail(self, lightningMode=False): showProgress = lightningMode totalDirty = self.getTotalDirty() progressInfo = None G.deferredManager.removeFromQueue("motionTrailUpdate") if totalDirty == 0: return if showProgress: firstStep = 0 thisStep = 0 estimatedTime = None totalSteps = totalDirty status = "aTools - Creating Motion Trail..." startChrono = None progressInfo = [ startChrono, firstStep, thisStep, totalSteps, estimatedTime, status ] utilMod.startProgressBar(status) self.updateCache(lightningMode, progressInfo) if showProgress: utilMod.setProgressBar(endProgress=True)
def getDataFromFiles(self, animAttr, folder, infoFiles, fileModTime, ext, progressInfo, onlySelectedNodes): currSel = animMod.getObjsSel() data = {"data":[], "modTime":None} infoFileModTimeList = [] startChrono, firstStep, thisStep, totalSteps, estimatedTime, status = progressInfo initialStep = thisStep for n, loopFile in enumerate(infoFiles): if cmds.progressBar(G.progBar, query=True, isCancelled=True ): utilMod.setProgressBar(endProgress=True) return thisStep = n+initialStep startChrono = utilMod.chronoStart(startChrono, firstStep, thisStep, totalSteps, estimatedTime, status) infoFileStr = loopFile.replace(":", "_aTools_")[0:-(len(ext)+1)] infoFilePath = aToolsMod.getSaveFilePath("%s%s%s"%(folder, os.sep, infoFileStr), ext=ext) infoFileModTimeList.append(os.path.getmtime(infoFilePath)) if infoFileModTimeList[-1] > fileModTime: #load only what is newer object = loopFile.replace("_aTools_", ":")[0:-(len(ext)+1)] value = aToolsMod.loadFileWithUser(folder, infoFileStr, ext=ext) if onlySelectedNodes: if animAttr == "anim": obj = value["objects"][0] else: obj = object.split(".")[0] if obj not in currSel: continue data["data"].append({"_modTime":infoFileModTimeList[-1],"object":object, "value":value}) estimatedTime = utilMod.chronoEnd(startChrono, firstStep, thisStep, totalSteps) #file mod date data["data"].sort() #sort newer files last if len(infoFileModTimeList) > 0: data["modTime"] = max(infoFileModTimeList) progressInfo = [startChrono, firstStep, thisStep, totalSteps, estimatedTime, status] #blend animation data================= return [data, progressInfo]
def getSavedData(self, crashFolder=None, onlySelectedNodes=False, *args): idFolder = aToolsMod.getSceneId() crashFolder = self.baseLatestFolderName if not crashFolder else crashFolder folder = "%s%s%s%s%s"%(self.baseFolderName, os.sep, idFolder, os.sep, crashFolder) filePath = cmds.file(query=True, sceneName=True) fileModTime = None if os.path.isfile(filePath): fileModTime = os.path.getmtime(filePath) curveFiles = aToolsMod.readFilesWithUser(folder, ext=self.curveExt) attrFiles = aToolsMod.readFilesWithUser(folder, ext=self.attrExt) status = "aTools Animation Crash Recovery - Step 1/3 - Loading crash files..." utilMod.startProgressBar(status) totalSteps = len(curveFiles + attrFiles) firstStep = 0 thisStep = 0 estimatedTime = None startChrono = None progressInfo = [startChrono, firstStep, thisStep, totalSteps, estimatedTime, status] data = self.getDataFromFiles("anim", folder, curveFiles, fileModTime, self.curveExt, progressInfo, onlySelectedNodes) if not data: return animData = data[0] progressInfo = data[1] data = self.getDataFromFiles("attr", folder, attrFiles, fileModTime, self.attrExt, progressInfo, onlySelectedNodes) attrData = data[0] if not data: return utilMod.setProgressBar(endProgress=True) return {"fileModTime":fileModTime, "animData":animData, "attrData":attrData}
def applyMirror(self, pasteAndCycle=False): cmds.waitCursor(state=True) range = animMod.getTimelineRange() range[1] = int(range[1]) total = range[1] - range[0] getCurves = animMod.getAnimCurves() animCurves = getCurves[0] getFrom = getCurves[1] invertRules = self.getInvertRules() if animCurves: status = "aTools - Applying mirror..." utilMod.startProgressBar(status) totalSteps = len(animCurves) firstStep = 0 thisStep = 0 estimatedTime = None startChrono = None mirrorCurves = animMod.getMirrorObjs(animCurves) keyValues = animMod.getTarget("keyValues", animCurves, getFrom) keyTimes = animMod.getTarget("keyTimes", animCurves, getFrom) currValues = animMod.getTarget("currValues", animCurves, getFrom) keysIndexSel = animMod.getTarget("keysIndexSel", animCurves, getFrom) keyTangentsAngle = animMod.getTarget("keyTangentsAngle", animCurves, getFrom) keyTangentsType = animMod.getTarget("keyTangentsType", animCurves, getFrom) currTime = cmds.currentTime(query=True) if keysIndexSel: #create dummy key #objects = animMod.getObjsSel() #mirrorObjs = animMod.getMirrorObjs(objects) #animMod.createDummyKey(mirrorObjs) for thisStep, aCurve in enumerate(animCurves): startChrono = utilMod.chronoStart(startChrono, firstStep, thisStep, totalSteps, estimatedTime, status) mCurve = mirrorCurves[thisStep] isCenterCurve = (mCurve == None) mirrorInvertValue = self.mirrorInvert( aCurve, isCenterCurve, invertRules) if mCurve and cmds.objExists(mCurve): tCurve = mCurve else: tCurve = aCurve if not cmds.objExists(tCurve): continue animMod.createDummyKey([tCurve]) if len(keysIndexSel[thisStep]) > 0: #delete keys cmds.cutKey( tCurve, time=( keyTimes[thisStep][keysIndexSel[thisStep][0]], keyTimes[thisStep][keysIndexSel[thisStep][-1]] ), clear=True) for key in keysIndexSel[thisStep]: keyValue = keyValues[thisStep][ key] * mirrorInvertValue inTangAngleValue = keyTangentsAngle[thisStep][key][ 0] * mirrorInvertValue outTangAngleValue = keyTangentsAngle[thisStep][ key][1] * mirrorInvertValue #apply keys if pasteAndCycle: t = keyTimes[thisStep][key] + (total / 2.) if t == range[1]: #repeat key at first frame t1 = t - total time = (t1, t1) cmds.setKeyframe(tCurve, time=time, value=keyValue) cmds.keyTangent(tCurve, time=time, inAngle=inTangAngleValue, outAngle=outTangAngleValue) cmds.keyTangent( tCurve, time=time, inTangentType=keyTangentsType[thisStep] [key][0], outTangentType=keyTangentsType[ thisStep][key][1]) elif t > range[1]: #fist half t -= total time = (t, t) else: time = (keyTimes[thisStep][key], keyTimes[thisStep][key]) cmds.setKeyframe(tCurve, time=time, value=keyValue) cmds.keyTangent(tCurve, time=time, inAngle=inTangAngleValue, outAngle=outTangAngleValue) cmds.keyTangent( tCurve, time=time, inTangentType=keyTangentsType[thisStep][key] [0], outTangentType=keyTangentsType[thisStep][key] [1]) else: #no keys#invert translate x keyValue = currValues[thisStep] * mirrorInvertValue #apply keys cmds.setKeyframe(tCurve, time=(currTime, currTime), value=keyValue) animMod.deleteDummyKey([tCurve]) estimatedTime = utilMod.chronoEnd(startChrono, firstStep, thisStep, totalSteps) #delete dummy key #animMod.deleteDummyKey(mirrorObjs) self.selectMirrorObjs(False) utilMod.setProgressBar(endProgress=True) animMod.refresh() cmds.waitCursor(state=False)
def setTween(self, percent, *args): #utilMod.timer("s") G.TM_lastTweenCommand = "self.setTween(%s)" % percent getCurves = animMod.getAnimCurves() animCurves = getCurves[0] getFrom = getCurves[1] if animCurves: status = "aTools - Tween Machine..." utilMod.startProgressBar(status) totalSteps = len(animCurves) firstStep = 0 thisStep = 0 estimatedTime = None startChrono = None cmds.waitCursor(state=True) cmds.refresh(suspend=True) keysSel = animMod.getTarget("keysSel", animCurves, getFrom) keyTimes = animMod.getTarget("keyTimes", animCurves) timelineTime = None #keysSelMerged = utilMod.mergeLists(keysSel) # reverse order to get ease in and out smoothly if 0 < percent <= 50 or percent == 100: for loopVal in keysSel: loopVal.reverse() #utilMod.timer() """ if len(keysSelMerged) == 0: if not timelineTime: timelineTime = [animMod.getTimelineTime()] cmds.setKeyframe(animCurves, time=timelineTime[0]) elif len(keysSelMerged) == 1: cmds.setKeyframe(animCurves, time=keysSelMerged[0]) """ for thisStep, loopCurve in enumerate(animCurves): if cmds.progressBar(G.progBar, query=True, isCancelled=True): utilMod.setProgressBar(endProgress=True) break startChrono = utilMod.chronoStart(startChrono, firstStep, thisStep, totalSteps, estimatedTime, status) if not keysSel[thisStep]: if not timelineTime: timelineTime = [animMod.getTimelineTime()] time = timelineTime else: time = [(loopTime, loopTime) for loopTime in keysSel[thisStep]] # if all keys selected, use timeline time instead if len(time) == len(keyTimes[thisStep]): if not timelineTime: timelineTime = [animMod.getTimelineTime()] time = timelineTime for loopTime in time: prevKeyTime = cmds.findKeyframe(loopCurve, time=loopTime, which="previous") nextKeyTime = cmds.findKeyframe(loopCurve, time=loopTime, which="next") if prevKeyTime == nextKeyTime and prevKeyTime != loopTime[ 0] and percent != "linear_next" and percent != "linear_prev": # if there is no previous or next key and at least one key cmds.setKeyframe(loopCurve, time=loopTime) elif prevKeyTime != time[0]: if percent == "linear_prev": prevKeyTime = nextKeyTime nextKeyTime = cmds.findKeyframe(loopCurve, time=(prevKeyTime, prevKeyTime), which="next") prevKeyVal = cmds.keyframe(loopCurve, query=True, time=(prevKeyTime, prevKeyTime), valueChange=True)[0] nextKeyVal = cmds.keyframe(loopCurve, query=True, time=(nextKeyTime, nextKeyTime), valueChange=True)[0] if nextKeyTime == prevKeyTime: value = prevKeyVal else: value = prevKeyVal + ( (nextKeyVal - prevKeyVal) / (nextKeyTime - prevKeyTime) * (loopTime[0] - prevKeyTime)) elif percent == "linear_next": nextKeyTime = prevKeyTime prevKeyTime = cmds.findKeyframe(loopCurve, time=(nextKeyTime, nextKeyTime), which="previous") prevKeyVal = cmds.keyframe(loopCurve, query=True, time=(prevKeyTime, prevKeyTime), valueChange=True)[0] nextKeyVal = cmds.keyframe(loopCurve, query=True, time=(nextKeyTime, nextKeyTime), valueChange=True)[0] if nextKeyTime == prevKeyTime: value = prevKeyVal else: value = prevKeyVal + ( (nextKeyVal - prevKeyVal) / (nextKeyTime - prevKeyTime) * (loopTime[0] - prevKeyTime)) else: animMod.eulerFilterCurve([loopCurve]) prevKeyVal = cmds.keyframe(loopCurve, query=True, time=(prevKeyTime, prevKeyTime), valueChange=True)[0] nextKeyVal = cmds.keyframe(loopCurve, query=True, time=(nextKeyTime, nextKeyTime), valueChange=True)[0] #print "prevKeyVal", prevKeyVal, nextKeyVal #if prevKeyVal == nextKeyVal: #if not time[0] in keysSel[thisStep]: cmds.setKeyframe(loopCurve, time=loopTime) #continue if percent == "linear": value = prevKeyVal + ( (nextKeyVal - prevKeyVal) / (nextKeyTime - prevKeyTime) * (loopTime[0] - prevKeyTime)) else: value = ((nextKeyVal - prevKeyVal) / 100. * percent) + prevKeyVal tangentType = cmds.keyTangent(loopCurve, query=True, outTangentType=True, time=(prevKeyTime, prevKeyTime))[0] inTangentType = tangentType.replace("fixed", "auto").replace( "step", "auto") outTangentType = tangentType.replace("fixed", "auto") if not time[0] in keysSel[thisStep]: cmds.setKeyframe(loopCurve, time=loopTime) cmds.keyframe(loopCurve, edit=True, time=loopTime, valueChange=value) cmds.keyTangent(loopCurve, edit=True, time=loopTime, inTangentType=inTangentType, outTangentType=outTangentType) #keycolor if 1 <= percent <= 99 or percent == "linear": cmds.keyframe( loopCurve, edit=True, time=loopTime, tickDrawSpecial=self.getColoredKeys()) if getFrom == "graphEditor": #curvesToSelect.append([loopCurve, loopTime]) cmds.selectKey(loopCurve, addTo=True, time=loopTime) estimatedTime = utilMod.chronoEnd(startChrono, firstStep, thisStep, totalSteps) #utilMod.timer() """ #APPLY if len(curvesToKey) > 0: cmds.setKeyframe(curvesToKey) for loopVar in curvesToValue: cmds.keyframe(loopVar[0], edit=True, time=loopVar[1], valueChange=loopVar[2]) cmds.keyTangent(loopVar[0], edit=True, time=loopVar[1], inTangentType=loopVar[3], outTangentType=loopVar[4]) for loopVar in curvesToColor: cmds.keyframe(loopVar[0], edit=True, time=loopVar[1], tickDrawSpecial=self.getColoredKeys()) for loopVar in curvesToSelect: cmds.selectKey(loopVar[0], addTo=True, time=loopVar[1]) """ cmds.refresh(suspend=False) cmds.waitCursor(state=False) utilMod.setProgressBar(endProgress=True)
def applyAnimData(animData, pasteInPlace=True, onlySelectedNodes=False, showProgress=None, status=None): if animData: status = "aTools - Applying animation data..." if not status else status objects = animData["objects"] if not onlySelectedNodes: #print "objects1", objects if len(objects) > 0: objects = [ loopObj for loopObj in objects if loopObj is not None and cmds.objExists(loopObj) ] #print "objects2", objects if len(objects) > 0: cmds.select(objects) else: objects = getObjsSel() if not objects: cmds.warning("No objects to apply.") return cmds.refresh(suspend=True) if showProgress: utilMod.startProgressBar(status) if pasteInPlace: currKey = cmds.currentTime(query=True) for aData in animData["animData"]: allKeys = [] keys = aData["keyframeData"] for n, key in enumerate(keys): timeChange = aData["keyframeData"][n][0] allKeys.append(timeChange) firstKey = 0 if allKeys: firstKey = min(allKeys) lastKey = max(allKeys) cutIn = currKey + firstKey cuOut = lastKey + firstKey else: cutIn = -49999 cuOut = 50000 objsAttrs = [loopItem["objAttr"] for loopItem in animData["animData"]] existObjsAttrs = [ loopObjAttr for loopObjAttr in objsAttrs if cmds.objExists(loopObjAttr) ] createDummyKey(existObjsAttrs) cmds.cutKey(existObjsAttrs, time=(cutIn, cuOut), clear=True) if showProgress: totalSteps = 0 firstStep = 0 thisStep = 0 estimatedTime = None startChrono = None for loopObjAttr in existObjsAttrs: index = objsAttrs.index(loopObjAttr) aData = animData["animData"][index] keys = aData["keyframeData"] totalSteps = totalSteps + len(keys) for loopObjAttr in existObjsAttrs: index = objsAttrs.index(loopObjAttr) aData = animData["animData"][index] weighted = aData["curveData"][0] infinity = aData["curveData"][1] keys = aData["keyframeData"] for n, key in enumerate(keys): if showProgress: if cmds.progressBar(G.progBar, query=True, isCancelled=True): refresh() utilMod.setProgressBar(endProgress=True) return startChrono = utilMod.chronoStart(startChrono, firstStep, thisStep, totalSteps, estimatedTime, status) #read values timeChange = aData["keyframeData"][n][0] valueChange = aData["keyframeData"][n][1] breakdown = aData["keyframeData"][n][2] inTangentType = aData["tangentData"][n][0] outTangentType = aData["tangentData"][n][1] ix = aData["tangentData"][n][2] iy = aData["tangentData"][n][3] ox = aData["tangentData"][n][4] oy = aData["tangentData"][n][5] lock = aData["tangentData"][n][6] weightLock = aData["tangentData"][n][7] if pasteInPlace: timeChange = timeChange - firstKey + currKey time = (timeChange, timeChange) # create key cmds.setKeyframe(loopObjAttr, time=time, value=valueChange, noResolve=True) if n == 0: cmds.keyTangent(loopObjAttr, weightedTangents=weighted) cmds.setInfinity(loopObjAttr, edit=True, preInfinite=infinity[0], postInfinite=infinity[1]) if breakdown: cmds.keyframe(loopObjAttr, edit=True, time=time, breakdown=True) cmds.keyTangent(loopObjAttr, time=time, ix=ix, iy=iy, ox=ox, oy=oy, lock=lock) if weighted: cmds.keyTangent(loopObjAttr, time=time, weightLock=weightLock) cmds.keyTangent(loopObjAttr, time=time, inTangentType=inTangentType, outTangentType=outTangentType) if showProgress: estimatedTime = utilMod.chronoEnd(startChrono, firstStep, thisStep, totalSteps) thisStep += 1 deleteDummyKey(existObjsAttrs) if showProgress: refresh() utilMod.setProgressBar(endProgress=True)
def getAnimData(animCurves=None, showProgress=None): if animCurves is None: getCurves = getAnimCurves(True) animCurves = getCurves[0] getFrom = getCurves[1] else: getFrom = None if not animCurves: return if getFrom is None: keysSel = getTarget("keysSel", animCurves, getFrom, rangeAll=True) else: keysSel = getTarget("keysSel", animCurves, getFrom) if utilMod.isEmpty(keysSel): return if showProgress: utilMod.startProgressBar("aTools - Saving animation data...") objsAttrs = getTarget("", animCurves=animCurves) objects = objsAttrs[0] attributes = objsAttrs[1] animData = {"objects": objects, "animData": []} if showProgress: firstStep = 0 totalSteps = len(animCurves) estimatedTime = None status = "aTools - Saving animation data..." startChrono = None for thisStep, loopCurve in enumerate(animCurves): if showProgress: startChrono = utilMod.chronoStart(startChrono, firstStep, thisStep, totalSteps, estimatedTime, status) if objects[thisStep] is None: continue if len(keysSel[thisStep]) == 0: continue weighted = cmds.keyTangent(loopCurve, query=True, weightedTangents=True) if weighted is not None: weighted = weighted[0] objAttr = "%s.%s" % (objects[thisStep], attributes[thisStep]) infinity = cmds.setInfinity(objAttr, query=True, preInfinite=True, postInfinite=True) animData["animData"].append({ "objAttr": objAttr, "curveData": [weighted, infinity], "keyframeData": [], "tangentData": [] }) time = (keysSel[thisStep][0], keysSel[thisStep][-1]) timeChange = cmds.keyframe(loopCurve, query=True, time=time, timeChange=True) valueChange = cmds.keyframe(loopCurve, query=True, time=time, valueChange=True) breakdowns = cmds.keyframe(loopCurve, query=True, time=time, breakdown=True) inTangentType = cmds.keyTangent(loopCurve, query=True, time=time, inTangentType=True) outTangentType = cmds.keyTangent(loopCurve, query=True, time=time, outTangentType=True) ix = cmds.keyTangent(loopCurve, query=True, time=time, ix=True) iy = cmds.keyTangent(loopCurve, query=True, time=time, iy=True) ox = cmds.keyTangent(loopCurve, query=True, time=time, ox=True) oy = cmds.keyTangent(loopCurve, query=True, time=time, oy=True) lock = cmds.keyTangent(loopCurve, query=True, time=time, lock=True) weightLock = cmds.keyTangent(loopCurve, query=True, time=time, weightLock=True) for n, loopKey in enumerate(keysSel[thisStep]): breakdown = (timeChange[n] in breakdowns) if breakdowns else [] keyframe = [timeChange[n], valueChange[n], breakdown] tangent = [ inTangentType[n], outTangentType[n], ix[n], iy[n], ox[n], oy[n], lock[n], weightLock[n] ] animData["animData"][-1]["keyframeData"].append(keyframe) animData["animData"][-1]["tangentData"].append(tangent) if showProgress: estimatedTime = utilMod.chronoEnd(startChrono, firstStep, thisStep, totalSteps) if showProgress: utilMod.setProgressBar(endProgress=True) return animData
def setTangent(self, tangentType, tangentInOut="inOut", targetKeys="selected", *args): #utilMod.timer(mode="s", function="MAIN FUNCTION") cmds.waitCursor(state=True) """ cmds.undoInfo(openChunk=True) cmds.undoInfo(closeChunk=True) cmds.undoInfo(openChunk=True) cmds.undoInfo(closeChunk=True) cmds.undoInfo(openChunk=True) cmds.undoInfo(closeChunk=True) cmds.undoInfo(openChunk=True) """ #tangentType = flow, bounce, auto, etc #targetKeys = all, selected #tangentInOut = inOut, in, out #set default tangent type if tangentType == "flow": cmds.keyTangent(edit=True, g=True, inTangentType="auto", outTangentType="auto") elif tangentType == "step": cmds.keyTangent(edit=True, g=True, outTangentType=tangentType) elif tangentType != "bounce": cmds.keyTangent(edit=True, g=True, inTangentType=tangentType, outTangentType=tangentType) # get target curves getCurves = animMod.getAnimCurves() animCurves = getCurves[0] getFrom = getCurves[1] #if there is no curves, exit if animCurves: status = "aTools - Tangents..." utilMod.startProgressBar(status) totalSteps = len(animCurves) firstStep = 0 thisStep = 0 estimatedTime = None startChrono = None index = None time = None if targetKeys == "all": # apply for all keys time = (-50000, 500000) if not self.isDefaultTangent(tangentType): index = animMod.getTarget("keyIndexTimes", animCurves, getFrom) self.applyTangent(animCurves, tangentType, getFrom, time, index) elif targetKeys == "selected": #apply on a range if getFrom == "timeline": time = animMod.getTimelineRange() time = (time[0], time[1]) #flow and bounce if not self.isDefaultTangent(tangentType): index = animMod.getTarget("keysIndexSel", animCurves, getFrom) self.applyTangent(animCurves, tangentType, getFrom, time, index, tangentInOut) else: if self.isDefaultTangent( tangentType ): # if the tangent types are default maya types #apply individually on each key keysSel = animMod.getTarget("keysSel", animCurves, getFrom) for thisStep, aCurve in enumerate(animCurves): if cmds.progressBar(G.progBar, query=True, isCancelled=True): utilMod.setProgressBar(endProgress=True) break startChrono = utilMod.chronoStart( startChrono, firstStep, thisStep, totalSteps, estimatedTime, status) for loopKey in keysSel[thisStep]: time = (loopKey, loopKey) self.applyTangent(aCurve, tangentType, getFrom, time, index, tangentInOut) estimatedTime = utilMod.chronoEnd( startChrono, firstStep, thisStep, totalSteps) else: #flow and bounce index = animMod.getTarget("keysIndexSel", animCurves, getFrom) self.applyTangent(animCurves, tangentType, getFrom, time, index, tangentInOut) else: # first and last frame keyTimes = animMod.getTarget("keyTimes", animCurves, getFrom) keyIndexTimes = animMod.getTarget("keyIndexTimes", animCurves, getFrom) self.bounceEnds(animCurves, tangentType, getFrom, tangentInOut, keyTimes, keyIndexTimes, targetKeys) utilMod.setProgressBar(endProgress=True) #cmds.undoInfo(closeChunk=True) cmds.waitCursor(state=False)
def align(self, sourceObjs, targetObj, frames=None, translate=True, rotate=True, showProgress=False, selectSorceObjs=False): if not sourceObjs or not targetObj: return cmds.refresh(suspend=True) currFrame = cmds.currentTime(query=True) constraints = [] setValues = [] modes = [] status = "aTools - Aligning nodes..." if translate: modes.append({"mode": "translate", "constrain": "pointConstraint"}) if rotate: modes.append({"mode": "rotate", "constrain": "orientConstraint"}) if showProgress: utilMod.startProgressBar(status) if not frames: getCurves = animMod.getAnimCurves() animCurves = getCurves[0] getFrom = getCurves[1] if animCurves: keysSel = animMod.getTarget("keysSel", animCurves, getFrom) frames = utilMod.mergeLists(keysSel) if frames == []: frames = [currFrame] else: frames = [currFrame] if showProgress: totalSteps = len(sourceObjs + frames) firstStep = 0 thisStep = 0 estimatedTime = None startChrono = None #get values for thisStep, loopSourceObj in enumerate(sourceObjs): if showProgress: startChrono = utilMod.chronoStart(startChrono, firstStep, thisStep, totalSteps, estimatedTime, status) setValues.append({"modes": [], "values": [], "skips": []}) for loopMode in modes: mode = loopMode["mode"] constrainType = loopMode["constrain"] allAttrs = cmds.listAttr(loopSourceObj, settable=True, keyable=True) skip = [ loopXyz for loopXyz in ["x", "y", "z"] if "%s%s" % (mode, loopXyz.upper()) not in allAttrs ] contrainFn = eval("cmds.%s" % constrainType) with G.aToolsBar.createAToolsNode: constraints.append( contrainFn(targetObj, loopSourceObj, skip=skip)[0]) setValues[-1]["modes"].append(mode) setValues[-1]["values"].append([ cmds.getAttr("%s.%s" % (loopSourceObj, mode), time=loopKey)[0] for loopKey in frames ]) setValues[-1]["skips"].append(skip) if showProgress: estimatedTime = utilMod.chronoEnd(startChrono, firstStep, thisStep, totalSteps) #del constraints for loopConstrain in constraints: cmds.delete(loopConstrain) for n, loopKey in enumerate(frames): if showProgress: thisStep = thisStep + n + 1 startChrono = utilMod.chronoStart(startChrono, firstStep, thisStep, totalSteps, estimatedTime, status) for nn, loopSourceObj in enumerate(sourceObjs): loopSetValue = setValues[nn] values = loopSetValue["values"] skips = loopSetValue["skips"] for nnn, loopMode in enumerate(modes): mode = loopMode["mode"] xyz = [ loopXyz for loopXyz in ["x", "y", "z"] if loopXyz not in skips[nnn] ] for nnnn, loopXyz in enumerate(xyz): attr = "%s%s" % (mode, loopXyz.upper()) value = values[nnn][n][nnnn] if len(frames) > 1: cmds.setKeyframe(loopSourceObj, attribute=attr, time=(loopKey, loopKey), value=value) if currFrame == loopKey: cmds.setAttr("%s.%s" % (loopSourceObj, attr), value) #euler filter if n == len(frames) - 1 and rotate: animCurves = utilMod.mergeLists([ cmds.keyframe(loopSourceObj, query=True, name=True) for loopSourceObj in sourceObjs ]) animMod.eulerFilterCurve(animCurves) if showProgress: estimatedTime = utilMod.chronoEnd(startChrono, firstStep, thisStep, totalSteps) if showProgress: utilMod.setProgressBar(endProgress=True) if selectSorceObjs: cmds.select(sourceObjs) cmds.refresh(suspend=False)
def smartSnapKeys(): getCurves = animMod.getAnimCurves() animCurves = getCurves[0] if not animCurves or len(animCurves) == 0: return getFrom = getCurves[1] keyTimes = animMod.getTarget("keyTimes", animCurves, getFrom) keysSel = animMod.getTarget("keysSel", animCurves, getFrom) hasDecimalKeys = False for loopKey in utilMod.mergeLists(keysSel): if loopKey != round(loopKey) > 0: hasDecimalKeys = True break if not hasDecimalKeys: return keyTangentsType = animMod.getTarget("keyTangentsType", animCurves, getFrom) firstStep = 0 totalSteps = len(animCurves) estimatedTime = None status = "aTools - Smart Snap Curves..." startChrono = None utilMod.startProgressBar(status) for thisStep, loopCurve in enumerate(animCurves): startChrono = utilMod.chronoStart(startChrono, firstStep, thisStep, totalSteps, estimatedTime, status) if None in [keyTimes[thisStep], keysSel[thisStep]]: continue stepKeys = [ loopKey for nn, loopKey in enumerate(keyTimes[thisStep]) if loopKey != round(loopKey) and loopKey in keysSel[thisStep] and keyTangentsType[thisStep][nn][1] == "step" ] linearKeys = [ loopKey for nn, loopKey in enumerate(keyTimes[thisStep]) if loopKey != round(loopKey) and loopKey in keysSel[thisStep] and keyTangentsType[thisStep][nn][1] == "linear" ] decimalKeys = [ loopKey for nn, loopKey in enumerate(keyTimes[thisStep]) if loopKey != round(loopKey) and loopKey in keysSel[thisStep] and loopKey not in stepKeys + linearKeys ] for loopKey in stepKeys: cmds.snapKey(loopCurve, time=(loopKey, loopKey)) for loopKey in linearKeys: cmds.snapKey(loopCurve, time=(loopKey, loopKey)) if len(decimalKeys) == 0: continue if not getFrom: if cmds.keyframe(query=True, selected=True) != None: getFrom = "graphEditor" #inLinearKeys = [round(loopKey) for nn, loopKey in enumerate(keyTimes[thisStep]) if keyTangentsType[thisStep][nn][0] == "linear"] #outLinearKeys = [round(loopKey) for nn, loopKey in enumerate(keyTimes[thisStep]) if keyTangentsType[thisStep][nn][1] == "linear"] createKeys = list(set([round(loopKey) for loopKey in decimalKeys])) selectKeys = [] #print "inlinearKeys", inLinearKeys, outLinearKeys if getFrom == "graphEditor": selectKeys = list( set([ round(loopKey) for loopKey in keysSel[thisStep] if round(loopKey) in createKeys ])) for loopKey in createKeys: cmds.setKeyframe(loopCurve, time=(loopKey, loopKey), insert=True) for loopKey in selectKeys: cmds.selectKey(loopCurve, addTo=True, time=(loopKey, loopKey)) for loopKey in decimalKeys: cmds.cutKey(loopCurve, time=(loopKey, loopKey)) #for loopKey in outLinearKeys: cmds.keyTangent(loopCurve, edit=True, time=(loopKey, loopKey), outTangentType="linear") #for loopKey in inLinearKeys: cmds.keyTangent(loopCurve, edit=True, time=(loopKey, loopKey), inTangentType="linear") estimatedTime = utilMod.chronoEnd(startChrono, firstStep, thisStep, totalSteps) utilMod.setProgressBar(endProgress=True)