def animatedSnapFunc(nodeToSnap, vertexSelection, knobsToAnimate, knobsToVerify, minVertices=1, snapFunc=s3d.translateToPointsVerified): '''A wrapper to call the relevant snap functions within a framerange loop''' temp = None try: s3d.verifyNodeToSnap(nodeToSnap, knobsToVerify) # verify vertex selection once before the loop s3d.verifyVertexSelection(vertexSelection, minVertices) # now ask for a framerange frames = getFrameRange() if not frames: return # Exit eary if cancelled or empty framerange # Add a CurveTool for the forced-evaluation hack temp = nuke.nodes.CurveTool() # Set the anim flag on knobs for knob in [nodeToSnap[x] for x in knobsToAnimate]: # reset animated status if knob.isAnimated(): knob.clearAnimated() knob.setAnimated() # Set up Progress Task task = nuke.ProgressTask("animatedSnap3D") task.setMessage("Matching position of %s to selected vertices" % nodeToSnap.name()) # Loop through the framerange for frame in frames: if task.isCancelled(): break # Execute the CurveTool node to force evaluation of the tree nuke.execute(temp, frame, frame) # this is repetitive, but the vertex selection needs to be computed again # in order to get the vertices at the right context (time) vertexSelection = s3d.getSelection() # this is also repetitive. Selection should be already verified # but checking again in case topology has changed between frames s3d.verifyVertexSelection(vertexSelection, minVertices) # Call the passed snap function from the nukescripts.snap3d module snapFunc(nodeToSnap, vertexSelection) except ValueError, e: nuke.message(str(e))
def animatedSnapFunc(nodeToSnap, vertexSelection, knobsToAnimate, knobsToVerify, minVertices = 1, snapFunc = s3d.translateToPointsVerified): '''A wrapper to call the relevant snap functions within a framerange loop''' temp = None try: s3d.verifyNodeToSnap(nodeToSnap, knobsToVerify) # verify vertex selection once before the loop s3d.verifyVertexSelection(vertexSelection, minVertices) # now ask for a framerange frames = getFrameRange() if not frames: return # Exit eary if cancelled or empty framerange # Add a CurveTool for the forced-evaluation hack temp = nuke.nodes.CurveTool() # Set the anim flag on knobs for knob in [nodeToSnap[x] for x in knobsToAnimate]: # reset animated status if knob.isAnimated(): knob.clearAnimated() knob.setAnimated() # Set up Progress Task task = nuke.ProgressTask("animatedSnap3D") task.setMessage("Matching position of %s to selected vertices" % nodeToSnap.name()) # Loop through the framerange for frame in frames: if task.isCancelled(): break # Execute the CurveTool node to force evaluation of the tree nuke.execute(temp, frame, frame) # this is repetitive, but the vertex selection needs to be computed again # in order to get the vertices at the right context (time) vertexSelection = s3d.getSelection() # this is also repetitive. Selection should be already verified # but checking again in case topology has changed between frames s3d.verifyVertexSelection(vertexSelection, minVertices) # Call the passed snap function from the nukescripts.snap3d module snapFunc(nodeToSnap, vertexSelection) except ValueError, e: nuke.message(str(e))
def translateRotateScaleToPointsAnimated(nodeToSnap): return animatedSnapFunc(nodeToSnap, s3d.getSelection(),\ ["translate", "rotate", "scaling"],\ ["translate", "rotate", "scaling", "xform_order", "rot_order"],\ minVertices = 3, snapFunc = s3d.translateRotateScaleToPointsVerified)
def translateToPointsAnimated(nodeToSnap): return animatedSnapFunc(nodeToSnap, s3d.getSelection(), \ ["translate"],\ ["translate", "xform_order"],\ minVertices = 1, snapFunc = s3d.translateToPointsVerified)
def animated_snap(transforms=None, node=None, vertices=None, frange=None): """A wrapper to call the relevant snap functions within a frame range loop Args: transforms=None : [str] A list of transforms to apply to the snapped object. Should be one or more of the following: translate, rotate or scaling Default: ['translate'] node=None : (<nuke.Node>) The Nuke node to apply the transforms to. Default: nuke.thisNode() vertices=None : [<nuke.Vertex>] The vertices to use to get the transformation. Default: snap3d.getSelection() frames=None : (<nuke.FrameRange>) Provide a FrameRange object to suppress dialog. Returns: None Raises: N/A """ min_verts = 1 if not node: node = nuke.thisNode() if not vertices: vertices = snap3d.getSelection() if not transforms: transforms = ['translate'] snap_func = snap3d.translateToPointsVerified knobs = list(transforms) knobs.append('xform_order') if 'rotate' in knobs: knobs.append("rot_order") snap_func = snap3d.translateRotateToPointsVerified if 'scaling' in knobs: min_verts = 3 snap_func = snap3d.translateRotateScaleToPointsVerified # Verify valid selections before we enter the loop try: snap3d.verifyNodeToSnap(node, knobs) snap3d.verifyVertexSelection(vertices, min_verts) except ValueError as err: nuke.message(err) return if not frange: # Ask for a frame range frange = _get_frange() if not frange: # Exit early if cancelled or empty frange return # Add a CurveTool for the forced-evaluation hack temp = nuke.nodes.CurveTool() # Set the animated flag on knobs for knob in [node[knob_name] for knob_name in transforms]: # Reset animated status if knob.isAnimated(): knob.clearAnimated() knob.setAnimated() # Set up Progress Task task = nuke.ProgressTask("Snapping") task.setMessage( "Matching position of {node_name} to selected vertices".format( node_name=node.name())) # Loop through the framerange for frame in frange: if task.isCancelled(): break progress = _frange_percent(frame, frange) task.setProgress(progress) # Execute the CurveTool node to force evaluation of the tree nuke.execute(temp, frame, frame) # The vertex selection needs to be computed per frame # in order to get the vertices at the right context (time) vertices = snap3d.getSelection() # Checking vertex selection again in case topology has changed try: snap3d.verifyVertexSelection(vertices, min_verts) except ValueError: nuke.message( "Number of vertices selected has dropped below {verts}." "This is most likely due to changes in geometry topology." "\n" "Please select new vertices and start again from frame " "{frame} on.".format(verts=min_verts, frame=frame)) break else: # Call the passed snap function from the nukescripts.snap3d module snap_func(node, vertices) if temp: nuke.delete(temp)
def animated_snap(transforms=None, node=None, vertices=None, frange=None): """A wrapper to call the relevant snap functions within a frame range loop Args: transforms=None : [str] A list of transforms to apply to the snapped object. Should be one or more of the following: translate, rotate or scaling Default: ['translate'] node=None : (<nuke.Node>) The Nuke node to apply the transforms to. Default: nuke.thisNode() vertices=None : [<nuke.Vertex>] The vertices to use to get the transformation. Default: snap3d.getSelection() frames=None : (<nuke.FrameRange>) Provide a FrameRange object to suppress dialog. Returns: None Raises: N/A """ min_verts = 1 if not node: node = nuke.thisNode() if not vertices: vertices = snap3d.getSelection() if not transforms: transforms = ['translate'] snap_func = snap3d.translateToPointsVerified knobs = list(transforms) knobs.append('xform_order') if 'rotate' in knobs: knobs.append("rot_order") snap_func = snap3d.translateRotateToPointsVerified if 'scaling' in knobs: min_verts = 3 snap_func = snap3d.translateRotateScaleToPointsVerified # Verify valid selections before we enter the loop try: snap3d.verifyNodeToSnap(node, knobs) snap3d.verifyVertexSelection(vertices, min_verts) except ValueError as err: nuke.message(err) return if not frange: # Ask for a frame range frange = _get_frange() if not frange: # Exit early if cancelled or empty frange return # Add a CurveTool for the forced-evaluation hack temp = nuke.nodes.CurveTool() # Set the animated flag on knobs for knob in [node[knob_name] for knob_name in transforms]: # Reset animated status if knob.isAnimated(): knob.clearAnimated() knob.setAnimated() # Set up Progress Task task = nuke.ProgressTask("Snapping") task.setMessage( "Matching position of {node_name} to selected vertices".format( node_name=node.name() ) ) # Loop through the framerange for frame in frange: if task.isCancelled(): break progress = _frange_percent(frame, frange) task.setProgress(progress) # Execute the CurveTool node to force evaluation of the tree nuke.execute(temp, frame, frame) # The vertex selection needs to be computed per frame # in order to get the vertices at the right context (time) vertices = snap3d.getSelection() # Checking vertex selection again in case topology has changed try: snap3d.verifyVertexSelection(vertices, min_verts) except ValueError: nuke.message( "Number of vertices selected has dropped below {verts}." "This is most likely due to changes in geometry topology." "\n" "Please select new vertices and start again from frame " "{frame} on.".format( verts=min_verts, frame=frame ) ) break else: # Call the passed snap function from the nukescripts.snap3d module snap_func(node, vertices) if temp: nuke.delete(temp)