Help: Making Custom WorkspaceControl Window, RuntimeError: Internal C++ object already deleted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello I'm am a newbie when it comes to Python and coding for Maya. I'm using PyQt pyside2 for the first time to create a custom dockable window that will help with lighting. Such as bringing the roughness control slider to the window so I can adjust it without having to dive all the way into the shader.
I'm able to create a slider that affects the roughness but I want the number of the roughness to be displayed live so you see what it is currently at. On line 99 I added a line of code that I thought would update the number on my window every time you moved the slider but I'm getting an error that says: # RuntimeError: Internal C++ object (PySide2.QtWidgets.QLabel) already deleted.
I'm not sure if this has something to do with Python's garbage clean up or with the way I'm deleting and recreating the UI? Or is there a different way that I need to parent things? I'm not sure what I can do.
My code is below, any help is much appreciated, thank you!
########################################################
## LightingManager_v02.py ##
## testing to make a window for lighting ##
########################################################
import maya.cmds as cmds
import maya.mel as mel
from maya import OpenMayaUI as omui
from shiboken2 import wrapInstance
from PySide2 import QtWidgets, QtCore, QtGui, QtUiTools
import pymel.core as pm
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
import sys
def mayaMainWindow():
mainWindowPointer = omui.MQtUtil.mainWindow()
return wrapInstance(int(mainWindowPointer), QtWidgets.QWidget)
def deleteControl(control):
if cmds.workspaceControl(control, query=True, exists=True):
cmds.workspaceControl(control, edit=True, close=True)
cmds.deleteUI(control, control=True)
else:
pass
#################################################################################
## Getting the location of the roughness value to adjust
objTransform = "Head_hi"
objMesh = cmds.listRelatives(objTransform, shapes=True)[0]
objSE = cmds.listConnections(objMesh, type="shadingEngine")[0]
objMat = cmds.listConnections(objSE + ".aiSurfaceShader")[0]
objBeauty = cmds.listConnections(objMat + ".beauty")[0]
objTest = cmds.listConnections(objBeauty + ".input1")[0]
objFoo = cmds.listConnections(objTest + ".specularRoughness")[0]
objBar = cmds.listConnections(objFoo + ".input1")[0]
#This gets the current attribute value of the roughness of head
objGet = cmds.getAttr(objBar + ".value[0].value_FloatValue")
#################################################################################
class LightingManager(MayaQWidgetDockableMixin, QtWidgets.QMainWindow):
def __init__(self, parent = mayaMainWindow()):
# Creates the window
super(LightingManager, self).__init__(parent)
self.setObjectName('LightingManagerWindow')
self.setWindowTitle("Lighting Manager Window")
self.setWindowFlags(QtCore.Qt.Window)
self.widgetPath = ('C:\\Users\\... path to my scripts folder... \\lightingManagerWidget.ui')
self.widget = QtUiTools.QUiLoader().load(self.widgetPath)
# this adds the UI to the window
self.setCentralWidget(self.widget)
self.widget.setParent(self)
## Close Window Button
self.btn_close = self.widget.findChild(QtWidgets.QPushButton, 'btn_close')
self.btn_close.clicked.connect(self.close)
## Say "Hello" Slider
self.say_hello = self.widget.findChild(QtWidgets.QSlider, 'say_hello')
self.say_hello.valueChanged.connect(self.sayHello)
## Prints the current roughness adjustments of the head in script editor when button pushed
self.btn_currentRoughness = self.widget.findChild(QtWidgets.QPushButton, 'btn_currentRoughness')
self.btn_currentRoughness.setEnabled(True)
self.btn_currentRoughness.clicked.connect(self.currentRoughness)
self.lineEdit_roughness = self.widget.findChild(QtWidgets.QLineEdit, 'lineEdit_roughness')
self.label_roughnessUpdate = self.widget.findChild(QtWidgets.QLabel, 'label_roughnessUpdate')
self.slider_roughness = self.widget.findChild(QtWidgets.QSlider, 'slider_roughness')
self.slider_roughness.setRange(-20.0, 20.0)
self.roughnessSlider()
def sayHello(self):
print("Hello V02")
def currentRoughness(self):
objGet = cmds.getAttr(objBar + ".value[0].value_FloatValue")
print(objGet)
def getRoughness(self, value):
cmds.setAttr(objBar + ".value[0].value_FloatValue", value/100)
self.label_roughnessUpdate.setText(str(value/100))
def roughnessSlider(self):
self.slider_roughness.setEnabled(True)
objGet = cmds.getAttr(objBar + ".value[0].value_FloatValue")
self.slider_roughness.setValue(objGet*100)
value = self.slider_roughness.value()
self.slider_roughness.valueChanged.connect(self.getRoughness)
self.lineEdit_roughness.setText(str(value/100))
self.label_roughnessUpdate.setText(str(value/100))
def openWindow():
deleteControl('LightingManagerWindowWorkspaceControl')
Core = LightingManager()
Core.show(dockable=True)
cmds.workspaceControl('LightingManagerWindowWorkspaceControl', e=True, widthProperty='preferred')
Core.raise_()
if __name__ == '__main__':
openWindow()
# Paste this into Maya script editor to run the window!
'''
import sys
p=r"C:\\Users\\... path to my scripts folder... \\"
if p not in sys.path:
sys.path.insert(0, p)
from importlib import reload
import LightingManager_v01
reload(LightingManager_v01)
LightingManager_v01.openWindow()
'''RuntimeError