Skip to content

Instantly share code, notes, and snippets.

@MitchellKehn
Created December 8, 2020 06:17
Show Gist options
  • Save MitchellKehn/f197855f53304162109d671fce9d8e84 to your computer and use it in GitHub Desktop.
Save MitchellKehn/f197855f53304162109d671fce9d8e84 to your computer and use it in GitHub Desktop.
[Camera Smooth] Create a copy of the selected camera node, using a weighted average of surrounding frames #nuke
"""
Smooth Camera
Creates a new camera linked to the selected one, with a weighted average of the position/rotation curves.
"""
import nuke
def deselectAll():
for node in nuke.selectedNodes():
node.setSelected(False)
def createSmoothCopy(cam):
"""
Create a copy cam
@type cam: nuke.Node
@return:
"""
answ = nuke.getInput("how many frames to smooth by", "3")
frameDist = int(answ)
deselectAll()
cam.setSelected(True)
nuke.nodeCopy("%clipboard")
cam.setSelected(False)
nuke.nodePaste("%clipboard%")
newCam = nuke.selectedNode()
for knobName in ("translate", "rotate"):
knob = newCam[knobName]
for channel, channelName in enumerate("xyz"):
knob.clearAnimated(channel)
w_sum = 1
numerator = "{}.{}.{}(frame)".format(cam.fullName(), knobName, channelName)
for i in range(frameDist):
w = 1.0 / (i+2)
numerator += " + {:0.2f}*{}.{}.{}(frame+{})".format(w, cam.fullName(), knobName, channelName, i+1)
numerator += " + {:0.2f}*{}.{}.{}(frame-{})".format(w, cam.fullName(), knobName, channelName, i+1)
w_sum += 2*w
expr = "({}) / {}".format(numerator, w_sum)
knob.setExpression(expr, channel)
return newCam
if __name__ == '__main__':
createSmoothCopy(nuke.selectedNode())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment