尝试将nukescripts.PythonPanel嵌入到PyQt设计的面板中

问题描述 投票:0回答:2

我在 The Foundry NUKE 中创建了 2 个面板。一种是使用

PyQt
设计器,另一种是使用
nukescripts.PythonPanel
。现在我正在尝试将
nukescripts.PythonPanel
嵌入到
PyQt
设计的面板中。

如果您运行第一个模块(测试),您将在面板中看到 2 个选项卡,第一个选项卡中有 Qt 小部件。我正在尝试将第二个模块(test2)面板嵌入到第二个选项卡中。

请找到以下模块进行测试。测试 - pyqt 设计的面板。测试 2 - nukescripts.PythonPanel

测试-

import nuke
import os
import sys
from nukescripts import panels
import nukescripts
from PyQt4 import Qt
from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(QtGui.QWidget):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.tabWidget = QtGui.QTabWidget(self.centralwidget)
        self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
        self.tab = QtGui.QWidget()
        self.tab.setObjectName(_fromUtf8("tab"))
        self.pushButton = QtGui.QPushButton(self.tab)
        self.pushButton.setGeometry(QtCore.QRect(20, 40, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.tab)
        self.pushButton_2.setGeometry(QtCore.QRect(110, 40, 75, 23))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_3 = QtGui.QPushButton(self.tab)
        self.pushButton_3.setGeometry(QtCore.QRect(30, 110, 75, 23))
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.pushButton_4 = QtGui.QPushButton(self.tab)
        self.pushButton_4.setGeometry(QtCore.QRect(120, 110, 75, 23))
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.tabWidget.addTab(self.tab, _fromUtf8(""))
        self.tab_2 = QtGui.QWidget()
        self.tab_2.setObjectName(_fromUtf8("tab_2"))
        self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
        self.horizontalLayout.addWidget(self.tabWidget)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "button1", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setText(QtGui.QApplication.translate("MainWindow", "button1", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_3.setText(QtGui.QApplication.translate("MainWindow", "button3", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_4.setText(QtGui.QApplication.translate("MainWindow", "button4", None, QtGui.QApplication.UnicodeUTF8))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), QtGui.QApplication.translate("MainWindow", "Tab 1", None, QtGui.QApplication.UnicodeUTF8))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), QtGui.QApplication.translate("MainWindow", "Tab 2", None, QtGui.QApplication.UnicodeUTF8))

class Main(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)

## make this work in a .py file and in 'copy and paste' into the script editor
moduleName = __name__
if moduleName == '__main__':
  moduleName = ''
else:
  moduleName = moduleName + '.'

panels.registerWidgetAsPanel( moduleName + 'Main', 'Mk test','com.ohufx.test')

测试2-

import nuke
import os
import re

class test( nukescripts.PythonPanel ):
    def __init__( self ):    
        nukescripts.PythonPanel.__init__( self, 'MkGlobal', 'com.ohufx.MkPanel')

        self.BeginChromaGroup= nuke.BeginTabGroup_Knob('ChromaPresets','ChromaPresets')
        self.GreenscreenPresets = nuke.Tab_Knob('GreenscreenPresets','GreenscreenPresets')
        self.Greenscreen = nuke.PyScript_Knob('Greenscreen','Greenscreen','Presets.GreenScreen()')
        #####################AdditiveKeyerPresets
        self.AdditiveKeyerPresets = nuke.Tab_Knob('AdditiveKeyerPresets','AdditiveKeyerImage' )
        self.AdditiveKeyerPresets.setFlag(nuke.STARTLINE)
        self.AdditiveKeyer_1 = nuke.PyScript_Knob('AdditiveKeyer_1','             AdditiveKeyer_1             ','Presets.AdditiveKeyer_1()')
        self.AdditiveKeyer_1.setFlag(nuke.STARTLINE)
        self.AdditiveKeyer_2 = nuke.PyScript_Knob('AdditiveKeyer_2','             AdditiveKeyer_2             ','Presets.AdditiveKeyer_2()')
        self.AdditiveKeyer_2.setFlag(nuke.STARTLINE)
        self.AdditiveKeyer_3 = nuke.PyScript_Knob('AdditiveKeyer_3','             AdditiveKeyer_3             ','Presets.AdditiveKeyer_3()')
        self.AdditiveKeyer_3.setFlag(nuke.STARTLINE)
        self.AdditiveKeyer_4 = nuke.PyScript_Knob('AdditiveKeyer_4','             AdditiveKeyer_4             ','Presets.AdditiveKeyer_4()')
        self.AdditiveKeyer_4.setFlag(nuke.STARTLINE)
        self.AdditiveKeyer_5 = nuke.PyScript_Knob('AdditiveKeyer_5','             AdditiveKeyer_5             ','Presets.AdditiveKeyer_5()')
        self.AdditiveKeyer_5.setFlag(nuke.STARTLINE)
        self.AdditiveKeyer_6 = nuke.PyScript_Knob('AdditiveKeyer_6','             AdditiveKeyer_6             ','Presets.AdditiveKeyer_6()')
        self.AdditiveKeyer_6.setFlag(nuke.STARTLINE)
        self.AdditiveKeyer_7 = nuke.PyScript_Knob('AdditiveKeyer_7','             AdditiveKeyer_7             ','Presets.AdditiveKeyer_7()')
        self.AdditiveKeyer_7.setFlag(nuke.STARTLINE)

        for k in (
    self.BeginChromaGroup,
    self.GreenscreenPresets,self.Greenscreen,

    self.AdditiveKeyerPresets,
    self.AdditiveKeyer_1,self.AdditiveKeyer_2,self.AdditiveKeyer_3,self.AdditiveKeyer_4,self.AdditiveKeyer_5,self.AdditiveKeyer_6,self.AdditiveKeyer_7):

            self.addKnob(k)

p=test()
p.show()
python pyqt panel nuke
2个回答
0
投票

我认为您不能将

PythonPanel
对象嵌入到
QWidget
对象中。由于
PythonPanel
只是 nuke 对
QWidget
的轻微修改,您可能需要将第二个模块 (test2) 重建为
QWidget


0
投票

您可以将 PyQT/Pyside2 插入 nukescripts.PythonPanel 像这样的东西:

import nuke
from PySide2 import QtCore, QtWidgets


class MyPyKnob(QtWidgets.QSpinBox):
    def __init__(self):
        super(MyPyKnob, self).__init__()

        self.setValue(56)

        self.myValue = 0

    def makeUI(self):
        return self

class PyLineEdit(QtWidgets.QLineEdit):
    def makeUI(self):
        return self

class SimpleClass(object):
    def makeUI(self):
        return QtWidgets.QPushButton('Test')
 
class Horizontal_box(object):
    def makeUI(self):
        return(QtWidgets.QVBoxLayout())

class MyDial(QWidget):
    def __init__(self):
        super(MyDial, self).__init__()

        h_layout = QtWidgets.QHBoxLayout()

        self.dial = QtWidgets.QDial()

        self.dial.setRange(0, 100)

        h_layout.addWidget(self.dial)

        self.dial.valueChanged.connect(self.value_changed)
        self.dial.sliderMoved.connect(self.slider_position)
        self.dial.sliderPressed.connect(self.slider_pressed)
        self.dial.sliderReleased.connect(self.slider_released)

        self.dialB = QtWidgets.QDial()

        self.dialB.setRange(0, 100)

        h_layout.addWidget(self.dialB)
        
        self.setLayout(h_layout)

        self.dialB.valueChanged.connect(self.value_changed)
        self.dialB.sliderMoved.connect(self.slider_position)
        self.dialB.sliderPressed.connect(self.slider_pressed)
        self.dialB.sliderReleased.connect(self.slider_released)

    def makeUI(self):
        return self

    def value_changed(self, i):
        print('{0:.2f}'.format(i))

    def slider_position(self, p):
        print("position", p)

    def slider_pressed(self):
        print("Pressed!")

    def slider_released(self):
        print("Released")

class MySlider(QWidget):
    def __init__(self):
        super(MySlider, self).__init__()

        h_layout = QtWidgets.QHBoxLayout()

        self.slider = QtWidgets.QSlider()
        self.slider = QSlider(Qt.Horizontal, self)
        self.slider.setRange(0, 100)

        self.slider.setMinimum(10)
        self.slider.setMaximum(30)
        self.slider.setValue(20)

        h_layout.addWidget(self)

        self.slider.valueChanged.connect(self.value_changed)
        self.slider.sliderMoved.connect(self.slider_position)
        self.slider.sliderPressed.connect(self.slider_pressed)
        self.slider.sliderReleased.connect(self.slider_released)

    def makeUI(self):
        return self.slider

    def value_changed(self, i):
        print('{0:.2f}'.format(i))

    def slider_position(self, p):
        print("position", p)

    def slider_pressed(self):
        print("Pressed!")

    def slider_released(self):
        print("Released")

class MyTable(QWidget):

    def __init__(self):
        super(MyTable, self).__init__()

        # Set up the layout for the widget
        layout = QtWidgets.QVBoxLayout()

        nodes = nuke.selectedNodes()

        # Set up QTableView
        self.table_view = QtWidgets.QTableView()
        model = QtGui.QStandardItemModel()
        model.setHorizontalHeaderLabels(['Node name', 'Node label'])  # Set column headers

        for node in nodes:

            blur_size_knob = node['label'].value()
    
            node_name_item = QtGui.QStandardItem(node.name())
            size_value_item = QtGui.QStandardItem(str(blur_size_knob))
    
            model.appendRow([node_name_item, size_value_item])


        self.table_view.setModel(model)
        self.table_view.resizeColumnsToContents()
        self.table_view.setSortingEnabled(True)

        #self.table_view.valueChanged.connect(self.valueChangedKnob)
        
        layout.addWidget(self.table_view)
        
        h_layout = QtWidgets.QHBoxLayout()

        # Set a default value to the spinbox
        self.spin_box = QtWidgets.QSpinBox()
        self.spin_box.setValue(14)
        self.spin_box.setRange(0, 25)
        self.spin_box.valueChanged.connect(self.valueChangedKnob)
        
        h_layout.addWidget(self.spin_box)

        # Set a default value to the spinbox
        self.spin_boxB = QtWidgets.QSpinBox()
        self.spin_boxB.setValue(12)
        self.spin_boxB.setRange(0, 25)
        self.spin_boxB.valueChanged.connect(self.valueChangedKnob)
        
        h_layout.addWidget(self.spin_boxB)

        layout.addLayout(h_layout)

        self.setLayout(layout)

    def makeUI(self):
        return self

    def valueChangedKnob(self, knob):
        #print(self.spin_box.value()) # when knob is not an argument
        print(knob)

class MyCustomPanel(nukescripts.PythonPanel):
    def __init__( self):

        nukescripts.PythonPanel.__init__(self, 'My Custom Panel')

        knob_custom = nuke.PyCustom_Knob("MyWidget", 'spin', "MyPyKnob()")
        self.addKnob(knob_custom)

        knob_edit = nuke.PyCustom_Knob("MyEdit", "Edit", "PyLineEdit()")
        self.addKnob(knob_edit)

        knob = nuke.PyCustom_Knob("test", "Test", "SimpleClass()")
        knob.setFlag(nuke.STARTLINE)
        self.addKnob(knob)

        knob = nuke.PyCustom_Knob("table", " ", "MyTable()")
        knob.setFlag(nuke.STARTLINE)
        self.addKnob(knob)

        knob = nuke.PyCustom_Knob("table", " ", "MySlider()")
        knob.setFlag(nuke.STARTLINE)
        self.addKnob(knob)

        knob = nuke.PyCustom_Knob("table", " ", "MyDial()")
        knob.setFlag(nuke.STARTLINE)
        self.addKnob(knob)

    def run_panel(self):
        self.setMinimumSize(800, 600)
        self.result = self.showModalDialog()
        return(self.result)


res = MyCustomPanel().run_panel()

if res:
    print('ok')
© www.soinside.com 2019 - 2024. All rights reserved.