Skip to main content
Participant
September 21, 2021
Question

Painter crashes on startup - python plugin delete_ui_element

  • September 21, 2021
  • 4 replies
  • 1261 views

Hello,

I'm writing a Python plugin for Substance Painter and I discover a bug that creates crash on startup:

I have a virtualenv containing these versions:

PySide2==5.12.5
Qt.py==1.3.3
shiboken2==5.12.5

 
I add virtualenv's site-packages to my PYTHONPATH and launch Painter with this script inside startup folder :

 

from Qt import QtWidgets
import substance_painter


PLUGIN_WIDGETS = []


def display_dialog():
    main_window = substance_painter.ui.get_main_window()
    dialog = QtWidgets.QDialog(main_window)
    dialog.show()


def start_plugin():
    global PLUGIN_WIDGETS
    menu = QtWidgets.QMenu("&Test Menu")
    action = unit_menu.addAction("Test")
    action.triggered.connect(display_dialog)
    substance_painter.ui.add_menu(menu)
    PLUGIN_WIDGETS.append(menu)


def close_plugin():
    global PLUGIN_WIDGETS
    for widget in PLUGIN_WIDGETS:
        substance_painter.ui.delete_ui_element(widget)
    PLUGIN_WIDGETS = []

 


I just add a menu "Test" with a "test" action inside and the action open an empty dialog linked to Painter's main window.

When I click on the action to display the dialog, the dialog is correctly shown. But, if I restart Painter just after, I have a crash on startup without message on log files and Painter starts on "Report" window's close.

To fix it, I append my dialog to "PLUGIN_WIDGETS" to delete it on plugin close. It works for this small plugin, but I can't do it for all widgets created in bigger plugins (I will add more complicated Python scripts used also in other software).


Is there another way to prevent this startup crash? Is this bug normal?

Thanks in advance!
Coralie Goldbaum.

4 replies

Participating Frequently
October 10, 2022

I am experiencing this as well. What is the suggested solution? Thanks!

Participating Frequently
October 10, 2022

We've spent more time looking into this and found that adding the following line resolves the issue:
dialog.setAttribute(QtGui.Qt.WA_DeleteOnClose)

 

Participant
March 26, 2025

I implemented your solution in a plugin with the same issue which worked great.
I later discovered that deleting the widget with del(widget) also works. As my plugins is a class I implemented the __del__ method and added a del statement there which cleans up the widget when Painter closes and the plugin is shut down. For plugins that do not use a class I think one can use the atexit module to perform cleanup on shutdown.

 

import atexit

def cleanup():
    print("Cleanup on shutdown or plugin unload!")

atexit.register(cleanup)

 

 

Participant
October 18, 2021

Hello,

We also have this crash when we parent a QWidget to Substance Painter MainWindow:

"""
                    
Parenting a QWidget to the substance main window makes the next Painter instance to raise an error
"""
import substance_painter
from PySide2 import QtWidgets
PLUGIN_WIDGETS = []
def display_dialog():
    main_window = substance_painter.ui.get_main_window()
    dialog = QtWidgets.QDialog(parent=main_window)  # Here
    dialog.exec_()
def start_plugin():
    global PLUGIN_WIDGETS
    menu = QtWidgets.QMenu("&Test Menu")
    action = menu.addAction("Test")
    action.triggered.connect(display_dialog)
    substance_painter.ui.add_menu(menu)
    PLUGIN_WIDGETS.append(menu)
def close_plugin():
    global PLUGIN_WIDGETS
    for widget in PLUGIN_WIDGETS:
        substance_painter.ui.delete_ui_element(widget)
    PLUGIN_WIDGETS = None
if __name__ == "__main__":
    start_plugin()

Participant
September 29, 2022

I am also suffering from the same phenomenon.
Has it been resolved?

Participant
September 21, 2021

More information:

Version 7.1.1 (but also tested on version 7.2) and platform Windows 10 64bits.

Participant
September 21, 2021

Edit: I have the same error without my virtualenv with Qt.py and using directly PySide2.