Skip to main content
 首页 » 编程设计

python之Nuke 中的 Pyside 小部件不保持值

2025年01月19日8bhlsheji

我正在尝试将自定义 Pyside 小部件添加到 Nuke 中的节点。 Nuke 通过 PyCustom_Knob 包装器允许这样做。

我能够创建小部件并显示它,但它不会保留其值。 每次我关闭面板并重新打开时,它都会重置。我如何让它保持其设定值?我忘记了什么?

我正在关注 this教程。 (有同样的问题)

这是我当前的代码:

from PySide import QtGui, QtCore 
 
class myPyKnob(QtGui.QSpinBox): 
 
    def __init__(self, node): 
        super(self.__class__, self).__init__() 
 
        #Set a default value to the spinbox 
        self.setValue(1) 
 
        self.myValue = 0 
        self.valueChanged.connect(self.valueChangedKnob) 
 
    #Needed by Nuke to add the widget 
    def makeUI(self): 
        return self 
 
    def updateValue(self): 
        pass 
 
    def valueChangedKnob(self): 
        self.myValue = self.value() 
        print(self.myValue) 
        print(self.value()) 
 
# This takes the selected node and adds the widget using PyCustom_Knob 
if __name__ == '__main__': 
    node = nuke.selectedNode() 
    knob = nuke.PyCustom_Knob( "MyWidget", "", "myPyKnob(nuke.thisNode())" )  
    node.addKnob(knob) 

Here是演示问题的视频链接:

Nuke Docs : PySide Widget 在最底部

谢谢

请您参考如下方法:

我很确定您必须想出自己的方法来将数据存储在 Py_custom 旋钮上——听起来 nuke 默认情况下不会这样做。

您可以像我经常做的那样,将您的数据存储在脚本 Root() 的隐藏旋钮上。您的代码可能如下所示:

from PySide import QtGui, QtCore 
 
class myPyKnob(QtGui.QSpinBox): 
 
    def __init__(self, node): 
        super(self.__class__, self).__init__() 
 
        #each knob will need a custom name 
        self.name=node.name()+"_pyCustomKnob" 
 
        #we'll check for a stored result at the root and set it here 
        #this should work when a script is loaded as well 
        try: 
             value=nuke.Root().toKnob(self.name).value() 
        except: 
             value=1 
        self.setValue(value) 
 
        self.valueChanged.connect(self.valueChangedKnob) 
 
    #Needed by Nuke to add the widget 
    def makeUI(self): 
        return self 
 
    def updateValue(self): 
        pass 
 
    def valueChangedKnob(self): 
        myValue = self.value() 
        #store the current result on the root 
        try: 
            nuke.Root().toKnob(self.name).setValue(myValue) 
        except KeyError: 
            #knob doesnt exist so we need to make it 
            storageKnob=nuke.Int_Knob(self.name) 
            nuke.Root().addKnob(storageKnob) 
            storageKnob.setVisible(False) 
            storageKnob.setValue(myValue) 
 
# This takes the selected node and adds the widget using PyCustom_Knob 
if __name__ == '__main__': 
    node = nuke.selectedNode() 
    knob = nuke.PyCustom_Knob( "MyWidget", "", "myPyKnob(nuke.thisNode())" )  
    node.addKnob(knob) 

您可能必须更聪明地选择唯一名称,因为如果用户在创建此旋钮后更改节点的名称,它将继续使用其旧名称调用自己。