Last active
October 9, 2020 14:46
-
-
Save Onefabis/e2fb1fdf9b39e4959c75fe4ef7fbbf2b to your computer and use it in GitHub Desktop.
Rebake in-betweens for new poses in new animation layers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import maya.api.OpenMaya as om2 | |
import math | |
import maya.cmds as mc | |
import maya.mel as mel | |
def getDG( nType, getShape = False, upstream = True, pluglevel = True, sel=[] ): | |
kPlugLevel = pluglevel | |
direction = om2.MItDependencyGraph.kUpstream if upstream else om2.MItDependencyGraph.kDownstream | |
nodes = [] | |
if len( sel ) == 0: | |
selection = om2.MGlobal.getActiveSelectionList() | |
else: | |
selection = om2.MSelectionList() | |
for s in sel: | |
selection.add( s ) | |
for s in xrange( selection.length() ): | |
if getShape: | |
mObject = selection.getDagPath( s ).extendToShape().node() | |
else: | |
mObject = selection.getDependNode( s ) | |
mItDependencyGraph = om2.MItDependencyGraph( mObject, direction, kPlugLevel, 1 ) | |
while not mItDependencyGraph.isDone(): | |
currentItem = mItDependencyGraph.currentNode() | |
dependNodeFunc = om2.MFnDependencyNode(currentItem) | |
if any( [ True for x in nType if x == dependNodeFunc.typeName ] ): | |
name = dependNodeFunc.name() | |
nodes.append( name ) | |
mItDependencyGraph.next() | |
return nodes | |
def bakeLayer ( parentCurve, childCurve ): | |
minT = mc.playbackOptions( q=1, min=1 ) | |
maxT = mc.playbackOptions( q=1, max=1 ) | |
childKeys = mc.keyframe( childCurve, q=1, tc=1 ) | |
if childKeys: | |
childRanges = [] | |
if len( childKeys ) > 2: | |
cr = lambda childKeys, n=1: [ childKeys[ i:i+n+1 ] for i in xrange( len( childKeys ) ) if i !=len(childKeys)-1 ] | |
childRanges = cr(childKeys) | |
elif len( childKeys ) == 2: | |
childRanges = [childKeys] | |
else: | |
return | |
for c in childRanges: | |
mult = 1 | |
cutKeys = [] | |
if not mc.keyframe( parentCurve, t=(int(c[0]), ), q=1, vc=1 ): | |
mc.setKeyframe( parentCurve, t=( int(c[0]), ), i=1 ) | |
cutKeys.append( int(c[0]) ) | |
if not mc.keyframe( parentCurve, t=(int(c[1]), ), q=1, vc=1 ): | |
mc.setKeyframe( parentCurve, t=( int(c[1]), ), i=1 ) | |
cutKeys.append( int(c[1]) ) | |
parVals = mc.keyframe( parentCurve, t=(int(c[0]), int(c[1])), q=1, vc=1 ) | |
parentMinRange = min( parVals ) | |
parentMaxRange = max( parVals ) | |
childMinRange = mc.keyframe( childCurve, t=(int(c[0]),), q=1, vc=1 ) | |
childMaxRange = mc.keyframe( childCurve, t=(int(c[1]),), q=1, vc=1 ) | |
if childMinRange > childMaxRange: | |
mult = -1 | |
parentPercent = abs( parentMaxRange - parentMinRange ) / 100.0 | |
childPercent = abs( childMaxRange[0] - childMinRange[0] ) / 100.0 | |
parentDeltas = [] | |
for t in range( int(c[0]), int(c[1]) ): | |
if parentPercent != 0: | |
nFr = mc.keyframe( parentCurve, t=( t+1, ), q=1, vc=1 ) | |
cFr = mc.keyframe( parentCurve, t=( t, ), q=1, vc=1 ) | |
if not nFr: | |
mc.setKeyframe( parentCurve, i=1, t=( t+1, ) ) | |
nFr = mc.keyframe( parentCurve, t=( t+1, ), q=1, vc=1 ) | |
mc.cutKey( parentCurve, t=( t+1, ) , cl=1 ) | |
if not cFr: | |
mc.setKeyframe( parentCurve, i=1, t=( t, ) ) | |
cFr = mc.keyframe( parentCurve, t=( t, ), q=1, vc=1 ) | |
mc.cutKey( parentCurve, t=( t, ) , cl=1 ) | |
if nFr[0] > cFr[0]: | |
parentDeltas.append( ( nFr[0] - cFr[0] ) / parentPercent ) | |
else: | |
parentDeltas.append( ( cFr[0] - nFr[0] ) / parentPercent ) | |
else: | |
parentDeltas.append( 0.0 ) | |
for cl in cutKeys: | |
mc.cutKey( parentCurve, t=( cl, ), cl=1 ) | |
valDelta = ( childMaxRange[0] - childMinRange[0] ) / ( int(c[0]) - int(c[1]) ) | |
delta = 0 | |
for k in range( int(c[0]), int(c[1])): | |
if childPercent and parentPercent: | |
delta += parentDeltas[k-int(c[0])] | |
mc.setKeyframe( childCurve, t=(k+1,), v=childMinRange[0] + mult * delta * childPercent / ( sum(parentDeltas) / 100.0 ) ) | |
def recursiveSelect( lay, layerArray ): | |
children = mc.animLayer( lay, q=1, children=1 ) | |
for c in children: | |
if len(layerArray) == 0: | |
mc.warning( '"Base Animation" layer is active now. Please, select any additional layer' ) | |
return False | |
else: | |
layerArray[ len( layerArray ) ] = c | |
print layerArray | |
recursiveSelect( c, layerArray ) | |
def layerContSelect(selLayers): | |
layerArr = [] | |
if not selLayers: | |
mc.warning( 'There is no custom animation layers selected!' ) | |
return | |
for lay in selLayers: | |
if mc.objectType( lay ) == "animLayer": | |
if lay != 'BaseAnimation': | |
if lay == mc.animLayer( q=1, root=1 ): | |
layerArray = [] | |
recursiveSelect( lay, layerArray ) | |
layerContSelect( selLayers ) | |
break | |
else: | |
attrs = mc.animLayer( lay, q=1, at=1 ) | |
for a in attrs: | |
layerArr.append( mel.eval('plugNode %s' %a) ) | |
else: pass | |
return layerArr | |
selLayers = mc.treeView( "AnimLayerTabanimLayerEditor", q=1, si=1 ) | |
constraintNodes = layerContSelect( selLayers ) | |
if constraintNodes: | |
constraintNodes1 = getDG( [ "animBlendNodeAdditiveScale", "animBlendNodeAdditiveDL", "animBlendNodeAdditiveRotation" ], False, False, True, constraintNodes ) | |
for c in constraintNodes1: | |
constraintNodes2 = getDG( [ "animCurveTU", "animCurveTA", "animCurveTL" ], False, False, True, [c] ) | |
constraintNodes2 = constraintNodes2[::-1] | |
try: | |
if 'rotate' in c: | |
zr = lambda constraintNodes2, n=3: [constraintNodes2[i:i+n] for i in range(0, len(constraintNodes2), n)] | |
childRotate = zr(constraintNodes2) | |
parRotate = childRotate.pop(1) | |
for p in xrange( len( parRotate ) ): | |
for c in childRotate: | |
bakeLayer( parRotate[p], c[p] ) | |
else: | |
if len(constraintNodes2)>2: | |
bakeLayer( constraintNodes2[1], constraintNodes2[-1] ) | |
elif len(constraintNodes2)==2: | |
bakeLayer( constraintNodes2[1], constraintNodes2[0] ) | |
else: | |
pass | |
except: | |
mc.warning( 'There is no animation keyed controllers in layer!' ) | |
pass | |
else: | |
mc.warning( 'There is no custom animation layers selected!' ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment