Skip to content

Instantly share code, notes, and snippets.

@tokejepsen
Last active October 5, 2021 15:25
Show Gist options
  • Save tokejepsen/f1149cfdcad3ef6bf0eef5fd4a5bbf25 to your computer and use it in GitHub Desktop.
Save tokejepsen/f1149cfdcad3ef6bf0eef5fd4a5bbf25 to your computer and use it in GitHub Desktop.
Hold Frame Analyzer
"""
Select the node to analyze for held frames.
Outputs a FrameHold node with animation synced to the held frames.
"incremental" > If True only increment held values by 1. If False held values are the frames.
"operation" > choice between "Auto Crop" and "Avg Intensities". Different footage can require different operations. If one operation misses a frame movement, then try the other.
"""
import nuke
incremental = False
operation = "Avg Intensities"
first_frame = int(nuke.root()["first_frame"].value())
last_frame = int(nuke.root()["last_frame"].value())
selection = nuke.selectedNode()
# Create and evaluate curve tool node.
curve_tool_node = nuke.createNode("CurveTool")
curve_tool_node.setInput(0, selection)
curve_tool_node['resetROI'].execute()
operation_mapping = {"Auto Crop": 0, "Avg Intensities": 1}
curve_tool_node["operation"].setValue(operation_mapping[operation])
nuke.execute(curve_tool_node, first_frame, last_frame)
# Generate FrameHold node.
frame_hold_node = nuke.createNode("FrameHold")
frame_hold_node["first_frame"].setAnimated()
previous_frame_value = 0
hold_value = first_frame
operation_values_mapping = {"Auto Crop": "autocropdata", "Avg Intensities": "intensitydata"}
if incremental:
hold_value = first_frame - 1
for frame in range(first_frame, last_frame + 1):
frame_value = sum(curve_tool_node[operation_values_mapping[operation]].getValueAt(frame))
if previous_frame_value != frame_value:
if incremental:
hold_value += 1
else:
hold_value = frame
previous_frame_value = frame_value
frame_hold_node["first_frame"].setValueAt(hold_value, frame)
# Clean up.
nuke.delete(curve_tool_node)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment