I decided to make a simple UI using PySide. And also few days back I watched a tutorial of QtDesigner by Carlo Sansonetti on CGCircuit. I thought why not convert his PyQt4 implementation to PySide, since now I have PySide working on Maya 2012 x64 on Ubuntu , I kicked off and all I did is change PyQt to PySide. But the story doesn't end here, in PySide there is no loadUiType function like in PyQt4 has uic.loadUiType. So I remembered Nathan implemented a loadUiType function for pysideuic(which is uic in PyQt4), secondly I knew their is a problem that shiboken.wrapInstance returns the type of object passed whereas sip.wrapinstance returns the best possible match. So againNathan's implementation for wrapinstance function came handy. Except you only pass the ptr to wrapinstance() function as an argument. So now we get the correct return object form getMayaWindow() I am ready to go!
1: import maya.OpenMayaUI as mui
2: from PySide_loadUiType import loadUiType
3: import sip
4: from PySide import QtGui,QtCore
5: import maya.cmds as cmds
6: import maya.mel as mel
7: import shiboken
8:
9:
10: def wrapinstance(ptr, base=None):
11: """
12: Utility to convert a pointer to a Qt class instance (PySide/PyQt compatible)
13: :type base: QtGui.QWidget
14: :return: QWidget or subclass instance
15: :rtype: QtGui.QWidget
16: """
17: if ptr is None:
18: return None
19: ptr = long(ptr) #Ensure type
20: if globals().has_key('shiboken'):
21: if base is None:
22: qObj = shiboken.wrapInstance(long(ptr), QtCore.QObject)
23: metaObj = qObj.metaObject()
24: cls = metaObj.className()
25: superCls = metaObj.superClass().className()
26: if hasattr(QtGui, cls):
27: base = getattr(QtGui, cls)
28: elif hasattr(QtGui, superCls):
29: base = getattr(QtGui, superCls)
30: else:
31: base = QtGui.QWidget
32: return shiboken.wrapInstance(long(ptr), base)
33: elif globals().has_key('sip'):
34: base = QtCore.QObject
35: return sip.wrapinstance(long(ptr), base)
36: else:
37: return None
38:
39: def getMayaWindow():
40: #'get the maya main window as QMainWindow instance'
41: ptr =mui.MQtUtil.mainWindow()
42: return wrapinstance(long(ptr))
43:
44:
45: uifile=("/media/Drive_P/san_documents/Dropbox/Research_Study/renameSelNode.ui")
46: form_class, base_class= loadUiType(uifile)
47:
48: class RenameSelNodeUI(form_class,base_class):
49:
50: def __init__(self,parent=getMayaWindow()):
51:
52: super(RenameSelNodeUI,self).__init__(parent)
53: self.setupUi(self)
54: #method
55: self.connectSignals()
56:
57: def connectSignals(self):
58: """Connect all the UI signals"""
59: self.connect(self.clear_btn,QtCore.SIGNAL('released()'),self.clearTextField)
60: self.connect(self.rename_btn,QtCore.SIGNAL('released()'),self.renameNode)
61:
62: def clearTextField(self):
63: """Clears the text field"""
64: self.nodeName_txt.clear()
65:
66: def renameNode(self):
67: """Renames the selected node using the content of the text field
68: if multiple nodes are selected, only the first node will be renamed"""
69:
70: #--- get the text from the UI
71: currentText = str(self.nodeName_txt.text())
72:
73: #---grab the selection in maya
74: selection= cmds.ls(sl=1)
75:
76: #--- rename the maya node
77: if selection is not None:
78: if len(selection) > 0:
79: if currentText != "":
80: cmds.rename(selection[0],currentText)
81:
82:
83: def runUI():
84: global app
85: global win
86: app=QtGui.qApp
87: win = RenameSelNodeUI()
88: win.show()