我在返回函数时遇到了一些麻烦。我想创建一个Nuke脚本,询问用户一个目录,并在写入节点中使用当前日期,小时等自动设置该目录的路径。这部分非常简单(这是DateWrite()
函数提供的代码)
现在,我想在渲染完成后打开这个目录。所以我必须使用Callbacks并调用一个打开给定目录的函数。
这是我遇到麻烦的地方:因为目录是在第一个函数中设置的,所以我试图用return
来获取这个函数的值。
它有效,但它强迫我使用第一个函数两次(这部分是openDirectoryAfterRender()
函数)
#Modules import
import nuke
import subprocess
# Create DateWrite function
def DateWrite():
# Create Variables
selectedNodes = nuke.selectedNodes() # Get Selection of all selected nodes
if len(selectedNodes) == 1:
filePath = nuke.getFilename('Set Output Directory') # Asks the user to set an OutPut directory for the Write Node
writeNode = nuke.createNode("Write") # Create a Write Node
writeNode['file'].setValue(filePath + "[file rootname [file tail [value root.name]]]_[date %y][date %m][date %d]_[date %H][date %M].png") # Set the Write Node with TCL
writeNode['afterRender'].setValue('openDirectoryAfterRender()') # Add a callback which will call the function openDirectoryAfterRender()
else:
nuke.message("No node selected or more than one node are selected.\nPlease select only one node.")
return filePath
# Create openDirectoryAfterRender
def openDirectoryAfterRender():
directoryToOpen = DateWrite() # Get the returned directory from DateWrite() -but also execute DateWrite another time-
directoryToOpen = directoryToOpen.replace('/','\\') # Replace the slashes with backslashes
subprocess.Popen('explorer %s' % directoryToOpen) # Open the chosen directory
我对Python和代码一般都是新手,所以这可能是一个noob问题。 我尝试了很多不同的解决方案,这是我能从我想要的最接近的解决方案。
非常感谢!
答案 0 :(得分:0)
我觉得你对回调感到困惑。您应该从另一个函数或外部函数调用DataWrite。这是您的代码的更新版本,并在nuke中进行测试,它可以正常工作
import nuke
import subprocess
# Create DateWrite function
def DateWrite():
# Create Variables
selectedNodes = nuke.selectedNodes() # Get Selection of all selected nodes
if len(selectedNodes) == 1:
filePath = nuke.getFilename('Set Output Directory') # Asks the user to set an OutPut directory for the Write Node
writeNode = nuke.createNode("Write") # Create a Write Node
writeNode['file'].setValue(filePath + "[file rootname [file tail [value root.name]]]_[date %y][date %m][date %d]_[date %H][date %M].png") # Set the Write Node with TCL
writeNode['afterRender'].setValue('openDirectoryAfterRender(%s)' % filePath) # Add a callback which will call the function openDirectoryAfterRender()
else:
nuke.message("No node selected or more than one node are selected.\nPlease select only one node.")
# Create openDirectoryAfterRender
def openDirectoryAfterRender(directoryToOpen):
directoryToOpen = directoryToOpen.replace('/','\\') # Replace the slashes with backslashes
subprocess.Popen('explorer %s' % directoryToOpen) # Open the chosen directory
directoryToOpen = DateWrite() # Get the returned directory from DateWrite() -but also execute DateWrite another time-