Skip to content

Instantly share code, notes, and snippets.

@julik
Created July 30, 2012 15:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julik/3207646 to your computer and use it in GitHub Desktop.
Save julik/3207646 to your computer and use it in GitHub Desktop.
Offset and slip ALL OF EM KEYFRAMES on nodes
import nuke, re
def start_anim_at(node, frame):
"""
Will move all the animations of the passed node so that they start at the passed frame instead of the
current ont
"""
slip_by = frame - first_keyframe_location(node)
slip_animations_of_node(node, slip_by)
def first_keyframe_location(node):
"""
Returns the first frame which contains an animated keyframe for the selected node
"""
first_frames = []
# Walk all the knobs of the object and check if they are animated.
for knob_name in node.knobs():
k = node[knob_name]
if k.isAnimated():
for curve in k.animations():
first_frames.append(curve.keys()[0].x)
return min(first_frames)
def slip_animations_of_node(node, by_offset):
"""
This function will slip all the animations of the passed node by a passed
offset in frames
"""
# Offsets the frames in the passed curve TCL script
def offset_frames_in_curve(curve_str, offset):
def offset_replace(m):
if "." in m.group(1):
return "x%0.3f" % (float(m.group(1)) + offset)
else:
return "x%d" % (int(m.group(1)) + offset)
return re.sub(r"x([-+]?\d*\.\d+|\d+)", offset_replace, curve_str)
# Walk all the knobs of the object and check if they are animated.
for knob_name in node.knobs():
k = node[knob_name]
if k.isAnimated():
k.fromScript(offset_frames_in_curve(k.toScript(), int(by_offset)))
def align_camera_to_read():
"""
Will detect the first Camera node in selection and align it's keyframes to start
at the first frame of the first selected Read node. Useful if you got a camera track
starting at 1 and a Read node starting at an arbitrary frame
"""
read, cam = None, None
for node in nuke.selectedNodes():
if "camera" in re.node.Class().lowercase():
cam = node
if node.Class() == "Read":
read = node
if cam and read:
# Get the start frame of the Read
start = int(read["frame"].getValue())
# And BAM - let the camera start there
start_anim_at(cam, start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment