所以第一部分对代码来说应该不会太重要,但仅供了解的人引用,我将其写在 The Foundry 的“Nuke”中。创建 pythonPanel GUI。
因此,当我创建面板时,它会启动:
class notePanel(nukescripts.PythonPanel):
def __init__(self):
nukescripts.PythonPanel.__init__(self, 'Note Panel', 'com.ohufx.notePanel')
使用init(self): 部分。
当我向此面板添加旋钮或 UI 控件时,我使用如下内容:
self.proj = nuke.String_Knob('proj', 'Project:', os.environ['SHOW'])
self.addKnob(self.proj)
self.proj.setFlag(nuke.STARTLINE)
效果很好。另外,当我创建在面板中运行的脚本时,我会执行以下操作:
def launchSGPage(self):
pr = self.proj.value()
sh = self.shot.value()
url = shotgun.getURL(pr,sh)
webbrowser.get('/usr/bin/google-chrome %s').open(url)
还使用 self,效果很好。
我的问题是,在第一步中,当我添加旋钮(如我创建的弦旋钮)并且我想使我的一个功能(如上面的 launchSGPage)成为在特定按钮上运行的脚本时,如果我这样做:
self.sgpage = nuke.PyScript_Knob('sgpage', 'Open SG page', 'self.launchSGPage()')
self.launchSGPage() 给了我一个错误。由于某种原因, self 在这种情况下不起作用,到目前为止,我让它工作的唯一方法是在将其添加到 ui 时命名我的面板(在本例中我将其命名为 nPanel)并使用它的名称来调用我的函数,如下所示:
def addnotePanel():
global nPanel
nPanel = notePanel()
return nPanel.addToPane()
所以我的脚本按钮看起来像:
self.sgpage = nuke.PyScript_Knob('sgpage', 'Open SG page', 'nPanel.launchSGPage()')
这适用于所有意图和目的,但对我来说似乎......肮脏和不干净。我不确定是什么,但有件事告诉我这有问题。任何人都可以为我清理出一种更好的方法来链接到此类中的函数,而不是通过全局变量名称吗?
最佳答案
单击按钮时,将调用面板的 knobChanged
方法。您可以检查按钮是否被单击,并像其他方法一样调用适当的方法:
class notePanel(nukescripts.PythonPanel):
def __init__(self):
nukescripts.PythonPanel.__init__(self, 'Note Panel', 'com.ohufx.notePanel')
self.sgpage = nuke.PyScript_Knob('sgpage', 'Open SG page', '')
def knobChanged(self, knob):
if knob is self.sgpage:
self.launchSGPage()
def launchSGPage(self):
pr = self.proj.value()
sh = self.shot.value()
url = shotgun.getURL(pr,sh)
webbrowser.get('/usr/bin/google-chrome %s').open(url)
关于python - 在 Python 类中使用 self(在 Nuke 中制作 GUI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19406218/