0

我在返回函数时遇到了一些麻烦。我想创建一个 Nuke 脚本,询问用户一个目录,并自动在写入节点中使用当前日期、小时等设置该目录的路径。这部分非常简单(这是DateWrite()提供的代码中的函数)

现在,我想在渲染完成后打开这个目录。所以我必须使用回调并调用一个打开给定目录的函数。

这是我遇到麻烦的地方:由于目录是在第一个函数中设置的,所以我尝试使用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 和一般代码都很陌生,所以这可能是一个菜鸟问题。我尝试了很多不同的解决方案,这个是我能得到的最接近我想要的解决方案。

非常感谢 !

4

1 回答 1

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-
于 2017-04-01T01:20:52.180 回答