Nuke 11 and Pyside2

If you have made Nuke tools that rely heavily on Pyside, the move to PySide2 in Nuke 11 causes a few issues.

However, I’ve found that I can make the change quite easily with a few lines at the top and a bit of Search and Replace. A good guide has already been written on Nukepedia here, including a mention of the Qt.py project, but for my tool Animation Maker, this is how I chose to do it.

My original import of the two main Qt modules was like this:

import PySide.QtCore as QtCore
import PySide.QtGui as QtGui

I’ve now changed it to this:

try:
    # < Nuke 11
    import PySide.QtCore as QtCore
    import PySide.QtGui as QtGui
    import PySide.QtGui as QtGuiWidgets
except:
    # >= Nuke 11
    import PySide2.QtCore as QtCore
    import PySide2.QtGui as QtGui
    import PySide2.QtWidgets as QtGuiWidgets

You’ll notice I’ve imported the same module twice under different names in both cases, and this is key to avoiding peppering your code with if/else statements. The reason you can’t just do a straight swap from PySide to PySide2, is that most of QtGui is now called QtWidgets. And the reason you can’t just import them both as the name QtGui, is because not all of QtGui has moved to QtWidgets! Anything that isn’t actually a widget, like QColor, is still in QtGui.

So I created a joint name of QtGuiWidgets, to contain QtGui in Pyside and QtWidgets in Pyside2. Then kept the QtGui namespace there for both. It’s unnecessary for Pyside, but it just allows my code to refer to either. You can call your combined module whatever you like: QtCombined, QtGui2, QtGW, QtGwidgets…. whatever makes sense.

It then remained for me to do a search for QtGui in my code and replace all of them with QtGuiWidgets. I then ran the code to find any that it failed on, and put those back to QtGui. Once I’d found them all, it worked.

Example:

QtGui.QPushButton()         #old
QtGuiWidgets.QPushButton()  #new

I did find one example where a method name had changed, so I had to do a try/except on that line, but aside from that everything works the same.

Here’s some more examples of methods that need to be moved to the combined module name (probably not a complete list):

QtGuiWidgets.QWidget()
QtGuiWidgets.QLabel()
QtGuiWidgets.QComboBox()
QtGuiWidgets.QGroupBox()
QtGuiWidgets.QRadioButton()
QtGuiWidgets.QMessageBox()
QtGuiWidgets.QLineEdit()
QtGuiWidgets.QGraphicsView()
QtGuiWidgets.QGraphicsScene()
QtGuiWidgets.QGraphicsSimpleTextItem()
QtGuiWidgets.QGraphicsEllipseItem()
QtGuiWidgets.QGraphicsLineItem()
QtGuiWidgets.QGraphicsDropShadowEffect()

And here’s some more examples of methods that still belong to QtGui (also probably not a complete list):

QtGui.QFont()
QtGui.QPainter()
QtGui.QColor()
QtGui.QBrush()
QtGui.QKeyEvent()
QtGui.QPen()

Incidentally that one changed method name I came across was solved with this:

try:
    self.graphic.setAcceptsHoverEvents(False)
except:
    #PySide2
    self.graphic.setAcceptHoverEvents(False)

 

That’s it. You may find that this approach isn’t suited to your projects, but hope this helps someone out there.

Advertisements

Animation Maker 1.3

Now compatible with Nuke 11 (and everything below).

Also fixed a few bugs that meant multivalue knobs weren’t behaving correctly. If you added a second or third AnimationMaker tab to the same knob on different values, the latter would wipe the other expressions.

Get version 1.3 here, and just copy it over your existing version.

animmaker13.png