Skip to content

Instantly share code, notes, and snippets.

@julik
Created March 17, 2014 21:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julik/9608767 to your computer and use it in GitHub Desktop.
Save julik/9608767 to your computer and use it in GitHub Desktop.
import nuke,os
IMPULSE_FILTER_ENUM = 0
__version__ = (0,0,1)
def grow():
"""
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
"""
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(name="GrowCrop",reformat=True)
c["box"].setValue((xmin, ymin, xmax, ymax))
c.setInput(0, sel)
# Generate a node that "uncrops" to match the old placement in the canvas
t = nuke.nodes.Transform(name="UndoReformat")
t["filter"].setValue(IMPULSE_FILTER_ENUM)
t["translate"].setValue((xmin, ymin))
t.setInput(0, c)
t["ypos"].setValue(t["ypos"].getValue() - 20) # Leave some room
# Return the frame to it's old position
nuke.frame(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment