Issue
I am learning Python/PySide for Maya from time to time and today I tried to make a class with PySide.
I am trying to make a window with an empty list inside. I have done the same code structure with usual Maya commands for UI and it worked.
But when I tried to do same with PySide, I got AttributeError: 'VL_Test' object has no attribute 'ui_fk'
error.
What am I missing?
Code:
import maya.cmds as cmds
import maya.OpenMayaUI as omui
try:
from PySide import QtCore, QtWidgets, QtGui
from shiboken import wrapInstance
except:
from PySide2 import QtCore, QtWidgets, QtGui
from shiboken2 import wrapInstance
def getMayaWindow():
pointer = omui.MQtUtil.mainWindow()
if pointer is not None:
return wrapInstance(long(pointer), QtWidgets.QWidget)
class VL_Test(object):
def showUI(cls):
win = cls()
win.create()
return win
def __init__(self):
self.window = 'vl_test'
self.title = 'Test Remastered'
self.size = (1000,650)
self.supportsToolAction = False
self.actionName = 'Create and Close'
self.applyName = 'Create'
self.closeName = 'Close'
def create(self):
if cmds.window(self.window, exists= True):
cmds.deleteUI(self.window ,window= True)
self.parentWindow = getMayaWindow()
self.mainWindow = QtWidgets.QMainWindow(self.parentWindow)
self.mainWindow.setObjectName(self.window)
self.mainWindow.setWindowTitle(self.title)
self.mainWidget = QtWidgets.QWidget()
self.mainWindow.setCentralWidget(self.mainWidget)
QtWidgets.QStyleFactory.create('Windows')
self.mainLayout = QtWidgets.QHBoxLayout(self.mainWidget)
self.ui_fk()
self.mainWindow.show()
def ui_fk(self):
self.fkStrandGroupBox = QtWidgets.QGroupBox('FK Joints:')
self.fkStrandLayout = QtWidgets.QHBoxLayout()
self.mainLayout.addWidget(fkStrandGroupBox)
self.fkStrandGroupBox.setLayout(fkStrandLayout)
self.fkListWidget = QtWidgets.QListWidget()
self.fkStrandLayout.addWidget(fkListWidget)
testWindow = VL_Test()
testWindow.create()
Solution
I think you just lose some self
before these variables fkStrandGroupBox
fkStrandLayout
fkListWidget
I run it in Maya 2017, it seems ok.
import maya.cmds as cmds
import maya.OpenMayaUI as omui
try:
from PySide import QtCore, QtWidgets, QtGui
from shiboken import wrapInstance
except:
from PySide2 import QtCore, QtWidgets, QtGui
from shiboken2 import wrapInstance
def getMayaWindow():
pointer = omui.MQtUtil.mainWindow()
if pointer is not None:
return wrapInstance(long(pointer), QtWidgets.QWidget)
class VL_Test(object):
def showUI(cls):
win = cls()
win.create()
return win
def __init__(self):
self.window = 'vl_test'
self.title = 'Test Remastered'
self.size = (1000,650)
self.supportsToolAction = False
self.actionName = 'Create and Close'
self.applyName = 'Create'
self.closeName = 'Close'
def create(self):
if cmds.window(self.window, exists= True):
cmds.deleteUI(self.window ,window= True)
self.parentWindow = getMayaWindow()
self.mainWindow = QtWidgets.QMainWindow(self.parentWindow)
self.mainWindow.setObjectName(self.window)
self.mainWindow.setWindowTitle(self.title)
self.mainWidget = QtWidgets.QWidget()
self.mainWindow.setCentralWidget(self.mainWidget)
QtWidgets.QStyleFactory.create('Windows')
self.mainLayout = QtWidgets.QHBoxLayout(self.mainWidget)
self.ui_fk()
self.mainWindow.show()
def ui_fk(self):
self.fkStrandGroupBox = QtWidgets.QGroupBox('FK Joints:')
self.fkStrandLayout = QtWidgets.QHBoxLayout()
self.mainLayout.addWidget(self.fkStrandGroupBox)
self.fkStrandGroupBox.setLayout(self.fkStrandLayout)
self.fkListWidget = QtWidgets.QListWidget()
self.fkStrandLayout.addWidget(self.fkListWidget)
# test
self.fkListWidget.addItems(['a', 'b', 'c'])
testWindow = VL_Test()
testWindow.create()
And, if you use PySide
, don't add QtWidgets
, because PySide have no QtWidgets
I prefer to use import *
, because I am lazy.
try:
from PySide.QtGui import *
from PySide.QtCore import *
from shiboken import wrapInstance
except:
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtUiTools import *
from shiboken2 import wrapInstance
If you consider the compatibility, you can try this. https://github.com/mottosso/Qt.py
Answered By - Bon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.