使用QtGui 添加icon
- .setIcon(QtGui.QIcon(':fileOpen.png'))
- 使用 .setIcon
- (':')这里的 :(冒号) 是Qt语法 表示是一个资源
def create_widgets(self):
self.select_file_path_btn = QtWidgets.QPushButton()
self.select_file_path_btn.setIcon(QtGui.QIcon(':fileOpen.png'))
使用 .setToolTip() 快速添加文字说明
- .setTipTool('Select File')
self.select_file_path_btn.setToolTip('Select File')
如图++++++++++++++++++使用 cmds.resourceManager(nameFilter = '*png') 遍历 所有png的图标
import maya.cmds as cmds
for item in cmds.resourceManager(nameFilter = '*png'):
print item
如图+++++++++++++++check_box 可见性与 radio_btn 关联
force_check_box 可见性被 open_radio 关联时可见, 被其他 reference_radio, import_radio 点击时不可见
- update_force_visibility()槽函数传入 checked
- 当open_radio.toggled 到 update_force_visibility 为真时
- force_check_box.setVisible(checked= True)
def create_connections(self):
self.open_radio.toggled.connect(self.update_force_visibility)
def update_force_visibility(self, checked):
self.force_check_box.setVisible(checked)
文件图标添加Maya文件过滤器
file_path, selected_filter = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File')
- file_path 文件路径
-
selected_filter 过滤的的文件格式
QtWidgets.QFileDialog.getOpenFileName(self, 'Select File')
- 得到 完整文件路径名
-
设置给 line_edit 路径文本信息
def show_file_select_dialog(self):
'''
:return:
'''
file_path, selected_filter = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File')
if file_path:
self.file_path_line_edit.setText(file_path)
用类变量设置过滤文件格式
- 过滤格式放在字符串里
- 每两个之间用;;(两个分号)隔开
-
过滤器设置的格式下面
class TestUIWindow(QtWidgets.QDialog):
FILE_FILTERS = 'Maya(*.ma *mb);;Maya ASCII(*.ma);;Maya Binary(*.mb);;All Files(*.*)'
selected_filter = 'Maya(*.ma *.mb)'
- 填入 过滤 类变量
def show_file_select_dialog(self):
'''
:return:
'''
file_path, selected_filter = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File',
'',
self.FILE_FILTERS,
self.selected_filter)
if file_path:
self.file_path_line_edit.setText(file_path)
Apply_btn 关联 load_file 槽函数
- load_file 槽函数 根据按钮去 调用
- open_file
- import_file
- reference_file
# load_file
- file_path 得到line_edit 的文件路径文本
- 判断文本有没有
- 判断是不是文件信息
- 判断radio_btn 被选择中的那一个去执行对应的条件下的函数
def load_file(self):
'''
:return:
'''
file_path = self.file_path_line_edit.text()
if not file_path:
return
file_info = QtCore.QFileInfo(file_path)
if not file_info.exists():
om.MGlobal.displayError('File does not exsit: {0}'.format(file_path))
return
if self.open_radio.isChecked():
self.open_file(file_path)
elif self.import_radio.isChecked():
self.import_file(file_path)
else:
self.reference_file(file_path)
Open_file 函数
- force 复选框没有选择并且文件被修改了,弹出信息。
- 如果信息是Yes,设置 打开文件 force = True
def open_file(self, file_path):
force = self.force_check_box.isChecked()
if not force and cmds.file(q = True, modified = True):
result = QtWidgets.QMessageBox.question(self, "Modified", "Current scene has unsaved changes. Continue?")
if result == QtWidgets.QMessageBox.StandardButton.Yes:
force = True
else:
return
cmds.file(file_path, open = True, iv = True, force = force)
使用类方法调用窗口
- 申明一个 dialog_instance = None
- 在类方法中判读这个类变量
- 实例化 TestUIWindow
- 判读这个隐藏就show
- 如果在前面就 激活 窗口
class TestUIWindow(QtWidgets.QDialog):
FILE_FILTERS = 'Maya(*.ma *mb);;Maya ASCII(*.ma);;Maya Binary(*.mb);;All Files(*.*)'
selected_filter = 'Maya(*.ma *.mb)'
dialog_instance = None
@classmethod
def show_dialog(cls):
'''
:return:
'''
if not cls.dialog_instance:
cls.dialog_instance = TestUIWindow()
if cls.dialog_instance.isHidden():
cls.dialog_instance.show()
else:
cls.dialog_instance.raise_()
cls.dialog_instance.activateWindow()
完整代码如下
from PySide2 import QtCore
from PySide2 import QtWidgets
from PySide2 import QtGui
from shiboken2 import wrapInstance
import maya.OpenMayaUI as omui
import maya.OpenMaya as om
import maya.cmds as cmds
def get_maya_main_window():
maya_main_wnd = omui.MQtUtil.mainWindow()
return wrapInstance(long(maya_main_wnd), QtWidgets.QWidget)
class TestUIWindow(QtWidgets.QDialog):
FILE_FILTERS = 'Maya(*.ma *mb);;Maya ASCII(*.ma);;Maya Binary(*.mb);;All Files(*.*)'
selected_filter = 'Maya(*.ma *.mb)'
dialog_instance = None
@classmethod
def show_dialog(cls):
'''
:return:
'''
if not cls.dialog_instance:
cls.dialog_instance = TestUIWindow()
if cls.dialog_instance.isHidden():
cls.dialog_instance.show()
else:
cls.dialog_instance.raise_()
cls.dialog_instance.activateWindow()
def __init__(self, parent = get_maya_main_window()):
super(TestUIWindow, self).__init__(parent)
self.setWindowTitle('Open/Import/Reference')
self.setMaximumSize(700, 90)
self.create_widgets()
self.create_layouts()
self.create_connections()
def create_widgets(self):
'''
:return:
'''
self.file_path_line_edit = QtWidgets.QLineEdit()
self.select_file_path_btn = QtWidgets.QPushButton()
self.select_file_path_btn.setIcon(QtGui.QIcon(':fileOpen.png'))
self.select_file_path_btn.setToolTip('Select File')
self.open_radio = QtWidgets.QRadioButton('Open')
self.open_radio.setChecked(True)
self.import_radio = QtWidgets.QRadioButton('Import')
self.reference_radio = QtWidgets.QRadioButton('Reference')
self.force_check_box = QtWidgets.QCheckBox('Force')
self.apply_btn = QtWidgets.QPushButton('Apply')
self.close_btn = QtWidgets.QPushButton('Close')
def create_layouts(self):
'''
:return:
'''
file_path_layout = QtWidgets.QHBoxLayout()
file_path_layout.addWidget(self.file_path_line_edit)
file_path_layout.addWidget(self.select_file_path_btn)
radio_btn_layout = QtWidgets.QHBoxLayout()
radio_btn_layout.addWidget(self.open_radio)
radio_btn_layout.addWidget(self.import_radio)
radio_btn_layout.addWidget(self.reference_radio)
apply_close_btn_layout = QtWidgets.QHBoxLayout()
apply_close_btn_layout.addStretch()
apply_close_btn_layout.addWidget(self.apply_btn)
apply_close_btn_layout.addWidget(self.close_btn)
form_layout = QtWidgets.QFormLayout()
form_layout.addRow('File:', file_path_layout)
form_layout.addRow('', radio_btn_layout)
form_layout.addRow('', self.force_check_box)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.addLayout(form_layout)
main_layout.addLayout(apply_close_btn_layout)
#___Connect___
def create_connections(self):
'''
:return:
'''
self.select_file_path_btn.clicked.connect(self.show_file_select_dialog)
self.open_radio.toggled.connect(self.update_force_visibility)
self.apply_btn.clicked.connect(self.load_file)
self.close_btn.clicked.connect(self.close)
#___Slots___
def show_file_select_dialog(self):
'''
:return:
'''
file_path, selected_filter = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File',
'',
self.FILE_FILTERS,
self.selected_filter)
if file_path:
self.file_path_line_edit.setText(file_path)
def update_force_visibility(self, checked):
'''
:return:
'''
self.force_check_box.setVisible(checked)
def load_file(self):
'''
:return:
'''
file_path = self.file_path_line_edit.text()
if not file_path:
return
file_info = QtCore.QFileInfo(file_path)
if not file_info.exists():
om.MGlobal.displayError('File does not exsit: {0}'.format(file_path))
return
if self.open_radio.isChecked():
self.open_file(file_path)
elif self.import_radio.isChecked():
self.import_file(file_path)
else:
self.reference_file(file_path)
def open_file(self, file_path):
force = self.force_check_box.isChecked()
if not force and cmds.file(q = True, modified = True):
result = QtWidgets.QMessageBox.question(self, "Modified", "Current scene has unsaved changes. Continue?")
if result == QtWidgets.QMessageBox.StandardButton.Yes:
force = True
else:
return
cmds.file(file_path, open = True, iv = True, force = force)
def import_file(self, file_path):
cmds.file(file_path, i = True, ignoreVersion = True)
def reference_file(self, file_path):
cmds.file(file_path, reference = True, iv = True)
if __name__ == '__main__':
try:
myTest_ui.close() # pylint:disable = E0601
myTest_ui.deleteLater() # pylint:disable = E0601
except:
pass
myTest_ui = TestUIWindow()
myTest_ui.show()