这部分讲如何搞定动画
设置关键帧
关键帧knob是关键,设置knob来接受动画
node = nuke.createNode(' blur' )
k = node['size']
k.setAnimated()
完成此步骤后就可以用 setValue和setValueAt来设置关键帧了
k.setValue( 5 ) // 在当前帧设置值为5
k.setValueAt( 55, 10)
k.setValueAt( 66, 100 ) 在10,100帧设置值为 55和66
k.setValueAt( 77, 100, 0 )
k.setValueAt( 88, 100, 1 ) 如果knob是数组,那么设置在第100帧设置 第一组为 77, 第二组为88
删除关键帧
查看一个knob是否为动画
node = nuke.createNode( 'Blur')
k = node['size']
k.isAnimated()
如果有动画或者表达式动画就返回true
k.hasExpression()
判断是否 有表达式
获取knob的动画曲线
animCurve = k.animation( 0 )
animCurve = k.animation( 1 )
获取一个knob内全部动画曲线
allCurves = k.animations()
打印动画曲线中的所有值
for key in animCurve.keys():
xValue = key.x
yValue = key.y
print 'ket at %s has value %s' % ( xValue, yValue)
清除动画曲线中的所有值
for curve in k.animations():
curve.clear()
例子
bake动画
这段脚本将所有knob以及附带的表达式bake到关键帧动画里面,首先创建获取帧范围的循环,并在节点间循环
def bakeSelectNodes():
ret = nuke.getFramesAndViews(' bake curves in selected nodes?', '%s-%s' % (nuke.root().firstFrame(), nuke.root().lastFrame() ) )
这会弹出对话框来,如下:
判断frame是否可用
def bakeSelectedNodes():
ret = nuke.getFramesAndViews( 'bake curves in selelcted nodes?', '%s-%s' % ( nuke.root().firstFrame(), nuke.root().lastFrame() ))
if not ret:
return
fRange = nuke.FrameRange( ret[0] )
views = ret[1]
在节点运行bakeExpressionKnobs遍历拷贝
def bakeSelectedNodes():
ret = nuke.getFramesAndViews('bake curves in selected node', '%s-%s' % ( nuke.root().firstFrame(), nuke.root().lastFrame() ))
if not ret:
return
fRange = nuke.FrameRange( ret[0] )
views = ret[1]
for n in nuke.selectedNodes():
bakeExpressionKnobs(n, fRange.first(), fRange.last(), fRange.increment(), views)
辅助函数
getCurves 返回给定knob的动画曲线
def getCurves( knob, views):
curves = []
for v in views:
curves.extend( knob.animations(v) )
return curves
bakeCurve 烘焙动画曲线
def bakeCurve( curve, first, last, inc):
for f in xrange(first, last, inc):
curve.setKey(f, curve.evaluate(f))
curve.setExpression( 'curve')
有了上面两个函数就可以写bakeExpressionKnobs了,需要5个参数
def bakeExpression( node, first, last, inc, views):
expKnobs = [k for k in node.knobs().values() if k.hasExpression() ]
然后就可以运行getCurves了
allCurves = []
for k in expKnobs:
allCurves += getCurves( k, views )
随后bake数据
for c in allCurves:
bakeCurve(c, first, last, inc)
完整的bakeExpressionKnobs()
def bakeExpressionKnobs( node, first, last, inc, views ):
expKnobs = [ k for k in node.knobs().values if k.hasExpression() ]
allCurves = []
for k in expKnobs:
allCurves += getCurves(k, views )
for c in allCurves:
bakeCurve( c, first, last, inc)
所有代码:
import nuke
def bakeCurve( curve, first, last, inc ):
'''bake an expresison curve into a keyframes curve'''
for f in xrange( first, last, inc ):
curve.setKey( f, curve.evaluate( f ) )
curve.setExpression( 'curve' )
####################################################
def getCurves( knob, views ):
'''return a list of all animation curves found in the given knob'''
curves = []
for v in views:
curves.extend( knob.animations( v ) )
return curves
####################################################
def bakeExpressionKnobs( node, first, last, inc, views ):
'''bake all knobs in node that carry expressions'''
# GET ALL KNOBS WITH EXPRESSIONS IN THEM
expKnobs = [ k for k in node.knobs().values() if k.hasExpression() ]
# GET ALL CURVES INSIDE THAT KNOB INCLUDING SPLIT FIELDS AND VIEWS
allCurves = []
for k in expKnobs:
allCurves += getCurves( k, views )
# BAKE ALL CURVES
for c in allCurves:
bakeCurve( c, first, last, inc )
####################################################
def bakeDependentNodes():
'''Add this to onUserDestroy callback - not yet implemented'''
parentNode = nuke.thisNode() # THIS IS GIVEN TO US BY THE CALLBACK, i.e. WHEN A NODE IS DELETED - WELL, NOT YET
depNodes = parentNode.dependent( nuke.EXPRESSIONS )
ret = nuke.getFramesAndViews( 'bake curves in dependent nodes?', '%s-%s' % (parentNode.firstFrame(), parentNode.lastFrame()) )
if not ret:
return
fRange = nuke.FrameRange( ret[0] )
views = ret[1]
for n in depNodes:
bakeExpressionKnobs( n, fRange.first(), fRange.last(), fRange.increment(), views )
####################################################
def bakeSelectedNodes():
'''bake selected nodes' knobs that carry expressions'''
ret = nuke.getFramesAndViews( 'bake curves in selected nodes?', '%s-%s' % (nuke.root().firstFrame(), nuke.root().lastFrame()) )
if not ret:
return
fRange = nuke.FrameRange( ret[0] )
views = ret[1]
for n in nuke.selectedNodes():
bakeExpressionKnobs( n, fRange.first(), fRange.last(), fRange.increment(), views )
选择节点并执行:
bakeSelectedNodes()