Skip to content

Instantly share code, notes, and snippets.

@julik
Created August 24, 2011 08:47
Show Gist options
  • Save julik/1167600 to your computer and use it in GitHub Desktop.
Save julik/1167600 to your computer and use it in GitHub Desktop.
Automatically grow the bbox of a Nuke node unioning animation
import nuke,os
"""
Will make a Crop node that crops the frame to the union of the animated BBox of the nude currently selected.
For example, if you have a Tracker node that stabilizes an image this function will grow your bounding box
to include the whole stabilized image
"""
def grow_bbox():
sel = nuke.selectedNode()
xmin = 0
xmax = 0
ymin = 0
ymax = 0
# The trick is that we need to execute() some node every time we go to a next frame, to
# update the context.
# not any node can be executed(). Now, for Nuke 6.2+ you can feed the knobs an input context
# to evaluate the knobs at specific frames BUT bbox is not a knob, it's the node's property.
# So we create a CurveTool that we can execute() on every frame.
c = nuke.nodes.CurveTool()
f = nuke.frame()
# Python ranges are NOT inclusive therefore +1
for i in range(nuke.root().firstFrame(), nuke.root().lastFrame() + 1):
nuke.execute(c, i, i) # Workaround to update the tree
bbox = sel.bbox()
rx = bbox.x() + bbox.w()
ry = bbox.y() + bbox.h()
if xmin > bbox.x():
xmin = bbox.x()
if xmax < rx:
xmax = rx
if ymin > bbox.y():
ymin = bbox.y()
if ymax < ry:
ymax = ry
nuke.delete(c)
c = nuke.nodes.Crop()
c["name"].setValue("GrowCrop")
c["reformat"].setValue(True)
c["box"].setValue((xmin, ymin, xmax, ymax))
c.setInput(0, sel)
# Return the frame to it's old position
nuke.frame(f)
if nuke.GUI:
toolbar = nuke.menu("Nodes")
heMenu = toolbar.findItem("Useful")
ico = os.path.dirname(os.path.abspath(__file__)) + "/grow_bbox.png"
heMenu.addCommand("Union bbox across frames", command="grow_bbox()", icon=ico, tooltip = str(grow_bbox.__doc__))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment