在[url=http://groups.google.com/group/python_inside_maya]python_inside_maya[/url]中看到的一个简单的PyQt例子,我已经加入注释希望对大家有帮助。
如下图,点击Refresh按钮会将maya中所选的物体添加到列表中,如没选择任何物体则清空列表。
[img]http://dl.iteye.com/upload/attachment/167242/4eba66ce-bbcb-3987-962e-63a85ce543f6.jpg[/img]
使用方法:
将qtExampleUI.py和pumpThread.py文件放到以下路径中
[quote]C:/Documents and Settings/你的用户名/My Documents/maya/2008/prefs/scripts
C:/Documents and Settings/你的用户名/My Documents/maya/scripts [/quote]
在maya的脚本编辑器中的python面板中执行
qtExampleUI.py
pumpThread.py
如下图,点击Refresh按钮会将maya中所选的物体添加到列表中,如没选择任何物体则清空列表。
[img]http://dl.iteye.com/upload/attachment/167242/4eba66ce-bbcb-3987-962e-63a85ce543f6.jpg[/img]
使用方法:
将qtExampleUI.py和pumpThread.py文件放到以下路径中
[quote]C:/Documents and Settings/你的用户名/My Documents/maya/2008/prefs/scripts
C:/Documents and Settings/你的用户名/My Documents/maya/scripts [/quote]
在maya的脚本编辑器中的python面板中执行
import qtExampleUI
qtExampleUI.ExampleUI.Display()
# import qtExampleUI as qeui
# qeui.ExampleUI.Display()
qtExampleUI.py
# -*- coding: UTF-8 -*-
from PyQt4 import QtCore, QtGui
import maya.cmds as cmds
import pumpThread
# UI class
class ExampleUI( QtGui.QDialog ):
def __init__( self, parent=None ):
super( ExampleUI, self ).__init__( parent )
# Set some basic properties on the UI
# 设置UI的基本属性
self.setWindowTitle( 'ExampleUI' )
self.setObjectName( "ExampleUI" )
self.setAttribute( QtCore.Qt.WA_DeleteOnClose )
# Add a Layout and set it to the UI
# 添加一个Layout(布局)并设置到UI中
self.mainLayout = QtGui.QVBoxLayout( self )
self.setLayout( self.mainLayout )
# Add a Button and set some properties.
# Also add it to a layout.
# 添加一个按钮并设置一些属性
# 同时将它添加到layout中
self.RefreshButton = QtGui.QPushButton()
self.RefreshButton.setText( "Refresh" )
self.mainLayout.addWidget( self.RefreshButton )
# Add a list and set it to the layout
# 添加一个列表并将它添加到layout中
self.SelectionList = QtGui.QListWidget()
self.SelectionList.setMinimumSize( 250, 250 )
self.mainLayout.addWidget( self.SelectionList )
# Connect the Refresh Button to a function to populate the list
# using SIGNAL's
# 给Refresh按钮连接信号
# 当按钮被点击,会调用_RefreshButtonFunc方法
self.connect( self.RefreshButton, QtCore.SIGNAL( "clicked()" ),
self._RefreshButtonFunc )
# 将maya中选择的物体添加到列表中的方法
def _RefreshButtonFunc( self ):
'''
Fill the list based on a maya selection
'''
oSel = cmds.ls( sl=1 )
if oSel:
self.SelectionList.clear()
for i in oSel: self.SelectionList.addItem( i )
#[self.SelectionList.addItem(s) for s in oSel]
else:
self.SelectionList.clear()
@staticmethod
def Display():
'''
calls the window. Typical PumpThread call
Use's a modified pumpThread that properly set's up the thread.
'''
# We need a global to stop python gc the UI
global mainWindow
# We need pumpThread to make the UI responsive
# 我们需要 pumpThread来让UI有回应
pumpThread.initializePumpThread()
app = pumpThread.get_app()
if app:
# We can set the app to use a nice style
# 我们可以设置一个好看的样式
app.setStyle( 'Plastique' )
mainWindow = ExampleUI()
mainWindow.show()
#ExampleUI.Display()
pumpThread.py
###########################
from PyQt4 import QtCore, QtGui
import maya.utils as utils
import sys
import time
import threading
import maya.cmds as cmds
pumpedThread = None
app = None
gPump = True
def get_app():
global app
return app
def set_app( i_app ):
global app
testAppInstance = QtCore.QCoreApplication.instance()
if testAppInstance:
app = testAppInstance
else:
app = i_app
def get_pt():
global pumpedThread
return pumpedThread
def set_pt( i_pt ):
global pumpedThread
pumpedThread = i_pt
def pumpQt():
global app
global gPump
processorEv = threading.Event()
def processor():
app.processEvents()
processorEv.set()
while gPump:
utils.executeDeferred( processor )
processorEv.wait()
processorEv.clear()
time.sleep( 0.01 )
def killProcess():
global gPump
gPump = False
def killPumpThread():
if get_app():
get_app().closeAllWindows()
if get_pt():
while get_pt().isAlive():
killProcess()
set_pt( None )
set_app( None )
def initializePumpThread():
global gPump
gPump = True
if get_pt() == None:
set_app( QtGui.QApplication( sys.argv ) )
set_pt( threading.Thread( target=pumpQt, args=() ) )
get_pt().start()