On this page

Overview

This example creates a custom UI window inside Houdini using PySide6. PySide6 is included with Houdini so you don’t need to install it.

Tip

If you want a custom interface in a Houdini pane tab, use a python panel.

Implementation

Warning

PySide6 code must be executed from Houdini’s main thread. This means from the scene file’s Houdini module, which is run when the scene file is loaded, or from a shelf tool’s Script tab.

Do not run the example code below in a Python shell. It will not work and may even crash Houdini.

  1. Right-click an empty space on the shelf and choose New tool.

    Set the tool’s Name to pyside, the Label to PySide6 Demo, and the Icon to MISC_python.

  2. On the tool’s Script tab, paste the following code:

    from PySide6 import QtCore
    from PySide6 import QtWidgets
    
    class FontDemo(QtWidgets.QWidget):
        def __init__(self, parent=None):
            QtWidgets.QWidget.__init__(self, parent)
    
            hbox = QtWidgets.QHBoxLayout()
    
            self.setGeometry(500, 300, 250, 110)
            self.setWindowTitle('Font Demo')
    
            button = QtWidgets.QPushButton('Change Font', self)
            button.setFocusPolicy(QtCore.Qt.NoFocus)
            button.move(20, 20)
    
            hbox.addWidget(button)
    
            button.clicked.connect(self.showDialog)
    
            self.label = QtWidgets.QLabel('This is some sample text', self)
            self.label.move(130, 20)
    
            hbox.addWidget(self.label, 1)
            self.setLayout(hbox)
    
        def showDialog(self):
            ok, font = QtWidgets.QFontDialog.getFont()
            if ok:
                self.label.setFont(font)
    
    dialog = FontDemo()
    dialog.show()
    
  3. Click Accept.

  4. Click the new PySide6 Demo shelf tool.

    A custom window will open containing a button and a label. Click the button to show a font chooser dialog that will change the font of the label text.

Window lifetimes

A PySide6 window only exists as long as there is a reference to it in Python.

When you are writing your own code, if you create a window inside a function, and don’t store it somewhere persistent in memory (such as hou.session), the window will disappear when Python garbage collects the window at the end of the function.

In this example, the window continues to exist after the tool script finishes running, but this is a side effect of the fact Houdini doesn’t actually recycle the script context until the tool is clicked again.

One method for keeping the dialog alive without explicitly storing it somewhere persistent in memory is to parent the dialog to the main Houdini window (see hou.ui.mainQtWindow). This causes the main window to manage the dialog and keep it alive for the lifetime of the window. If you want to clean up the dialog when it is closed then you must write a closeEvent() method on the dialog to unparent it from the main window.

Using PyQt6 instead of PySide6

If you want to use PyQt6 instead of PySide6, you must install PyQt6. Note that commercial uses of PyQt6 require a commercial license, see the PyQt6 website for more information.

Windows

If using PyQt6 on Windows, make sure to install the library to the Python included with Houdini (in Houdini install location›/python).

To use PyQt6 in the example, replace references to PySide6 with PyQt6:

from PyQt6 import QtCore
from PyQt6 import QtWidgets

HOM Cookbook