Monday, September 05, 2011

first pyqt window in maya 2011 ( static ui built with qt designer )

为毛编个script都这么麻烦!!!!!!!烦死了烦死了 我TM就是不要用英文!!!!!!

combined from 4 tutorials somewhere on google ...
你玛其实就是抄来的
PyQt4 and Maya Help - Technical Artist Forum
 还拼凑了maya 2008 devkit 目录下的例子 我准备以后就用这个当模板了
 .ui 放在 ../maya/scripts/ui/下
EDIT: 其实老纸不知道main()里只开一个窗口(如果已经有了就关掉先)那部分对不对,求高人指导

first pyqt based window = =


#!/usr/bin/env python2

import os
import sip

import maya.cmds as cmds
import maya.OpenMayaUI as mui
import maya.mel as mm

from PyQt4 import QtGui, QtCore, uic
from functools import partial

#Get the absolute path to my ui file
uiFile = os.path.join(cmds.internalVar(userAppDir=True), 'scripts','ui', 'hjExportTemporaryGeoCache.ui')
print 'Loading ui file:', os.path.normpath(uiFile)

#Load the ui file, and create my class
form_class, base_class = uic.loadUiType(uiFile)

class Window(base_class, form_class):
    # def __init__(self, parent=getMayaWindow()):
    def __init__(self, parent=None):
        '''A custom window with a demo set of ui widgets'''
        #init our ui using the MayaWindow as parent
        super(base_class, self).__init__(parent)
        #uic adds a function to our class called setupUi, calling this creates all the widgets from the .ui file
        self.setupUi(self)
        self.setObjectName('myWindow')
        self.setWindowTitle("temporary geo cache export tool v 0.1 - by someone")
        


    def initialize(self):
        self.getFrameRange()
        self.getCacheDirectory()
        self.allFrameRangePushButton.clicked.connect(  partial(self.getFrameRange,1 )  )

    def getCacheDirectory(self):
        cmds.ls(selected=True)

    def exportCache(self):
        pass

    def getFrameRange(self,all=0):
       if not all:
            start=cmds.playbackOptions(q=True,minTime=True)
            end=cmds.playbackOptions(q=True,maxTime=True)
        else:
            start=cmds.playbackOptions(q=True,animationStartTime=True)
            end=cmds.playbackOptions(q=True,animationEndTime=True)            
            
        self.startFrameSpinBox.setValue(start)
        self.endFrameSpinBox.setValue(end)

def main():
    global myWindow
    mayaVer = mm.eval("getApplicationVersionAsFloat()")

    if 2008<mayaVer>=2011:

        def getMayaWindow():
            'Get the maya main window as a QMainWindow instance'
            ptr = mui.MQtUtil.mainWindow()
            return sip.wrapinstance(long(ptr), QtCore.QObject)

        try:
            if myWindow is not None:
                myWindow.close()
        except NameError:
            pass

        myWindow = Window(getMayaWindow())

        # if (cmds.dockControl('myDock', q=1, ex=1)):
        #     cmds.deleteUI('myDock')
        # allowedAreas = ['right', 'left']
        # myDock = cmds.dockControl('myDock',aa=allowedAreas, a='right', content='myWindow', label='My Qt Demo Window', w=350)

    elif 8.5<mayaVer<=2008:
        import pumpThread as pt
        pt.initializePumpThread()
        myWindow = Window()
    else:
        print 'use 2008 or above ...'
        return

    global app    
    app=QtGui.qApp
    app.connect(app, QtCore.SIGNAL("lastWindowClosed()"),
                        app, QtCore.SLOT("quit()"))

    myWindow.show()  


hjExportTemporaryGeoCache.py
hjExportTemporaryGeoCache.ui


Edit : Version 2 后来发现这样也可以,为何Window不需要parent也可以 ?


#!/usr/bin/env python2
import os
import maya.cmds as cmds
import maya.mel as mm

from PyQt4 import QtGui, QtCore, uic
from functools import partial

#Get the absolute path to my ui file
uiFile = os.path.join(cmds.internalVar(userAppDir=True), 'scripts','ui', 'lasCwCloth_ExportTemporaryGeoCache.ui')
print 'Loading ui file:', os.path.normpath(uiFile)

#Load the ui file, and create my class
form_class, base_class = uic.loadUiType(uiFile)

class Window(base_class, form_class):
    def __init__(self, parent=None):
        '''A custom window with a demo set of ui widgets'''
        #init our ui using the MayaWindow as parent
        super(base_class, self).__init__(parent)
        #uic adds a function to our class called setupUi, calling this creates all the widgets from the .ui file
        self.setupUi(self)
        self.setObjectName('myWindow')
        self.setWindowTitle("temporary geo cache export tool v 0.1 - by someone")
        
def main():
    global myWindow
    mayaVer = mm.eval("getApplicationVersionAsFloat()")

    if 2008<mayaVer>=2011:
        pass

    elif 8.5<mayaVer<=2008:
        import pumpThread as pt
        pt.initializePumpThread()
    else:
        print 'use 2008 or above ...'
        return

    myWindow = Window()
    global app    
    app=QtGui.qApp
    app.connect(app, QtCore.SIGNAL("lastWindowClosed()"),
                        app, QtCore.SLOT("quit()"))

    myWindow.show()


Edit : Version 3 最后改成这样子了 ...


#!/usr/bin/env python2

import os

import maya.cmds as cmds
import maya.OpenMayaUI as mui
import maya.mel as mel

from PyQt4 import QtGui, QtCore, uic
from functools import partial

#Get the absolute path to my ui file
uiFile = os.path.join(cmds.internalVar(userAppDir=True), 'scripts','ui', 'hjExportTemporaryGeoCache.ui')
print 'Loading ui file:', os.path.normpath(uiFile)

#Load the ui file, and create my class
form_class, base_class = uic.loadUiType(uiFile)

class Window(base_class, form_class):
    # def __init__(self, parent=getMayaWindow()):
    def __init__(self, parent=None):
        '''A custom window with a demo set of ui widgets'''
        #init our ui using the MayaWindow as parent
        super(base_class, self).__init__(parent)
        #uic adds a function to our class called setupUi, calling this creates all the widgets from the .ui file
        self.setupUi(self)
        self.setObjectName('myWindow')
        self.setWindowTitle("temporary geo cache export tool v 0.1 - by someone")
        


    def initialize(self):
        self.getFrameRange()
        self.getCacheDirectory()
        self.allFrameRangePushButton.clicked.connect(  partial(self.getFrameRange,1 )  )

    def getCacheDirectory(self):
        cmds.ls(selected=True)

    def exportCache(self):
        pass

    def getFrameRange(self,all=0):
        
        if not all:
            start=cmds.playbackOptions(q=True,minTime=True)
            end=cmds.playbackOptions(q=True,maxTime=True)
        else:
            start=cmds.playbackOptions(q=True,animationStartTime=True)
            end=cmds.playbackOptions(q=True,animationEndTime=True)            
            
        self.startFrameSpinBox.setValue(start)
        self.endFrameSpinBox.setValue(end)

def main():
    mayaVer = mel.eval("getApplicationVersionAsFloat()")

    if mayaVer<2011:
        import  pumpThread as pt
        pt.initializePumpThread()

    try:
        myWindow.close()
    except:
        pass

    global myWindow
    myWindow = Window()
    myWindow.show()