Skip to content

Commit

Permalink
Implemented new ShotGrid write node, and formatted with black/pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesvink committed Nov 5, 2021
1 parent a8a730e commit 4014a1d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 68 deletions.
4 changes: 2 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def init_app(self):
self.handler = self.tk_nuke_template.NukeTemplateHandler()

# Add callbacks
self.handler.addCallbacks()
self.handler.add_callbacks()


def destroy_app(self):
Expand All @@ -50,4 +50,4 @@ def destroy_app(self):
self.log_debug("Destroying tk-nuke-template app")

# Remove any callbacks that were registered by the handler
self.handler.removeCallbacks()
self.handler.remove_callbacks()
138 changes: 72 additions & 66 deletions python/tk_nuke_template/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
# standard toolkit logger
logger = sgtk.platform.get_logger(__name__)

class NukeTemplateHandler():

class NukeTemplateHandler:
"""
Main application
"""
Expand All @@ -27,115 +28,120 @@ def __init__(self):
Constructor
"""

# most of the useful accessors are available through the Application class instance
# it is often handy to keep a reference to this. You can get it via the following method:
self.app = sgtk.platform.current_bundle()


# logging happens via a standard toolkit logger
logger.info("Launching Template Generation Application...")

def checkForPlaceholder(self):
def check_for_placeholder(self):
logger.info("Checking for placeholder node.")
# Checks if script contains the placeholder node
placeholderFound = False
placeholder_found = False

for node in nuke.allNodes('ModifyMetaData'):
if node.name() == 'createTemplatePlaceholder':
placeholderFound = True
for node in nuke.allNodes("ModifyMetaData"):
if node.name() == "createTemplatePlaceholder":
placeholder_found = True

if placeholderFound:
if placeholder_found:
# If node is found, initiate template generation
logger.info("Placeholder node found. Initiating template generation...")
self.generateTemplate()
self.generate_template()


def generateTemplate(self):
def generate_template(self):
# Script to process nodes
#nuke.message("Current Context: %s" % self._app.context)
allNodes = nuke.allNodes()
# nuke.message("Current Context: %s" % self._app.context)
all_nodes = nuke.allNodes()

# Creating variable
writeNode = ''
write_node = ""

# Handling for when no viewer node exists
viewerNode = False
viewer_node = False

# Delete unnecessary nodes
for node in allNodes:
if node.Class() == 'WriteTank':
writeNode = nuke.toNode(node.name())
if node.name() == 'ShotgunWriteNodePlaceholder':
writeNode = nuke.toNode(node.name())
if node.Class() == 'Viewer':
viewerNode = nuke.toNode(node.name())

if not node.Class() in ['Read', 'WriteTank', 'ShotgunWriteNodePlaceholder', 'createTemplatePlaceholder', 'Merge', 'TimeOffset', 'Viewer'] and not node.name() == 'ShotgunWriteNodePlaceholder':
for node in all_nodes:
if node.Class() == "Group":
if node["isShotGridWriteNode"]:
write_node = nuke.toNode(node.name())
if node.name() == "ShotGridWriteNodePlaceholder":
write_node = nuke.toNode(node.name())
if node.Class() == "Viewer":
viewer_node = nuke.toNode(node.name())

if (
not node.Class()
in [
"Read",
"WriteTank",
"Group",
"ShotgunWriteNodePlaceholder",
"createTemplatePlaceholder",
"Merge",
"TimeOffset",
"Viewer",
]
and not node.name() == "ShotGridWriteNodePlaceholder"
):
nuke.delete(node)


### Replacing nodes
# Calculating ShotGrid template paths
templatePath = self.app.get_template("template_nuke_script")
currentPath = nuke.root().name()
templatePath = templatePath.apply_fields(currentPath)
templatePath = templatePath.replace(os.sep, '/')
template_path = self.app.get_template("template_nuke_script")
current_path = nuke.root().name()
template_path = template_path.apply_fields(current_path)
template_path = template_path.replace(os.sep, "/")

# Pasting template file
template = nuke.nodePaste(templatePath)
nuke.nodePaste(template_path)
template = nuke.selectedNodes()

# Get current location of read node
readNode = nuke.toNode('Read1')
readNodeX = readNode['xpos'].value()
readNodeY = readNode['ypos'].value()
read_node = nuke.toNode("Read1")
read_node_x = read_node["xpos"].value()
read_node_y = read_node["ypos"].value()

# Get current location of dot
plateNoOp = nuke.toNode('plateNoOp')
XplateNoOp = plateNoOp['xpos'].value()
YplateNoOp = plateNoOp['ypos'].value()
plate_noop = nuke.toNode("plateNoOp")
xplate_no_op = plate_noop["xpos"].value()
yplate_no_op = plate_noop["ypos"].value()

# Calculate postion adjustment
xDifference = XplateNoOp - readNodeX
yDifference = YplateNoOp - readNodeY
x_difference = xplate_no_op - read_node_x
y_difference = yplate_no_op - read_node_y

# Replace pasted nodes
for node in template:
xPos = node['xpos'].value()
yPos = node['ypos'].value()
node['xpos'].setValue(xPos-xDifference)
node['ypos'].setValue(yPos-yDifference+25)
x_pos = node["xpos"].value()
y_pos = node["ypos"].value()
node["xpos"].setValue(x_pos - x_difference)
node["ypos"].setValue(y_pos - y_difference + 25)

### Reconnecting nodes
# Connect read node
plateNoOp.setInput(0, readNode)
nuke.delete(plateNoOp)
plate_noop.setInput(0, read_node)
nuke.delete(plate_noop)

# Reposition write node
writeNoOp = nuke.toNode('writeNoOp')
xWriteNoOp = writeNoOp['xpos'].value()
yWriteNoOp = writeNoOp['ypos'].value()
write_no_op = nuke.toNode("writeNoOp")
x_write_no_op = write_no_op["xpos"].value()
y_write_no_op = write_no_op["ypos"].value()

if not writeNode == '':
writeNode.setInput(0, writeNoOp)
writeNode['xpos'].setValue(xWriteNoOp)
writeNode['ypos'].setValue(yWriteNoOp)
if not write_node == "":
write_node.setInput(0, write_no_op)
write_node["xpos"].setValue(x_write_no_op)
write_node["ypos"].setValue(y_write_no_op)

nuke.delete(writeNoOp)
nuke.delete(write_no_op)

# Reposition viewer node
if viewerNode:
viewerNode['xpos'].setValue(xWriteNoOp)
viewerNode['ypos'].setValue(yWriteNoOp+200)
if viewer_node:
viewer_node["xpos"].setValue(x_write_no_op)
viewer_node["ypos"].setValue(y_write_no_op + 200)

# Unselect all nodes
for node in nuke.selectedNodes():
node['selected'].setValue(False)

node["selected"].setValue(False)

def addCallbacks(self):
def add_callbacks(self):
# Add callbacks when changing context
nuke.addOnScriptLoad(self.checkForPlaceholder, nodeClass="Root")
nuke.addOnScriptLoad(self.check_for_placeholder, nodeClass="Root")

def removeCallbacks(self):
nuke.removeOnScriptLoad(self.checkForPlaceholder, nodeClass="Root")
def remove_callbacks(self):
nuke.removeOnScriptLoad(self.check_for_placeholder, nodeClass="Root")

0 comments on commit 4014a1d

Please sign in to comment.