简体   繁体   中英

Only create a Pane if it doesn't already exist

I found the code below from https://community.foundry.com/discuss/topic/140355/only-create-a-pane-if-it-doesn-t-already-exist but I am struggling to make this work in Nuke 12

from Qt import QtCore, QtGui, QtWidgets

def get_nuke_main_window():
    """Returns Nuke's main window"""

    app = QtWidgets.QApplication.instance()
    for obj in app.topLevelWidgets():
        if obj.inherits('QMainWindow') and obj.metaObject().className() == 'Foundry::UI::DockMainWindow':
            return obj
    else:
        raise RuntimeError('Could not find DockMainWindow instance')

def show_qt_pane(widget_class, title, id):
    """ Either shows an existing QT pane, or make a new one.

    :param widget_class:
    :param title:
    :param id:
    :return:
    """
    # Check if pane already exists.
    qwindow = get_nuke_main_window()
    widgets = qwindow.findChildren(QtWidgets.QWidget, id)
    if widgets:
        # We found at least one instance, show the first one.
        widget = widgets[0]
        parent_widget = widgets[0].parentWidget()
        parent_widget.setCurrentWidget(widget)
        parent_widget.activateWindow()
        parent_widget.setFocus()
    else:
        # No instances were found, let's initialize a new one in a new Panel.
        panel = nukescripts.PythonPanel(title, id)
        widget = nuke.PyCustom_Knob(title, "", "__import__('nukescripts').panels.WidgetKnob({})".format(widget_class))
        panel.addKnob(widget)
        panel.show()

Can someone please translate this to Pyside2.QtCore - I am very inexperienced with QT and I can't make it work. I changed QtWidgets to QtGui and imported QtGui as import PySide2.QtWidgets as QtGui (I think I also had to remove the id variable from "widgets" but I am stuck on parent_widget.setCurrentWidget (widget) - I get

AttributeError: 'PySide2.QtWidgets.QMainWindow' object has no attribute 'setCurrentWidget'

You might have better luck thinking outside the box - why not add a variable to the nuke library and use that variable to store the status of your panel?

import nuke  
nuke.panelExists=False

You could set it to True/False in your create panel logic and check for it during creation. That variable will now be accessible anywhere you import the module.

Check out this article from my blog - I did a lot of digging around panels in nuke and built out something that could wrap any widget. It doesn't do exactly what you're looking for; however it's got details about the signals the panels emit when they move around (or are closed):

https://www.tks-designs.com/blog/?p=350

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM