We will deepen your understanding while checking the contents of your own tools one by one, and use them for future tool development. It is a continuation of the following series. The part that overlaps with 1 and 2 is omitted.
-[Maya Python] Crushing the contents of the script 1 ~ Camera Speed Editor -[Maya Python] Crushing the contents of the script 2 ~ list Notes
Deletes the plug-in loading information stored in the scene. If it takes a long time to read the file, please try it.
List the plugin information and delete only the checked information.

# -*- coding: utf-8 -*-
from maya.app.general.mayaMixin import MayaQWidgetBaseMixin
from PySide2 import QtWidgets, QtCore, QtGui
from maya import cmds
	
class Widget(QtWidgets.QWidget):
	def __init__(self):
		super(Widget, self).__init__()
		
		layout = QtWidgets.QGridLayout(self)
		label = QtWidgets.QLabel(
			u'A list of unknown plugins.\n'\
			u'Delete the checked plug-in.')
		layout.addWidget(label, 0, 0)
		
		treeView = TreeView()
		layout.addWidget(treeView, 1, 0, 1, 2)
		
		self.standardItemModel = StandardItemModel()
		treeView.setModel(self.standardItemModel)
		
		button = QtWidgets.QPushButton('Refresh')
		button.clicked.connect(self.refresh)
		layout.addWidget(button, 2, 0)
		
		button = QtWidgets.QPushButton('Remove')
		#Change the background color of the button
		button.setStyleSheet("background-color: red")
		button.clicked.connect(self.remove)
		layout.addWidget(button, 2, 1)
		
		self.refresh()
		
	#Operation of Refresh button
	def refresh(self):
		self.standardItemModel.removeRows(0, self.standardItemModel.rowCount())
		
		#List unknownPlugin
		unknownPluginList = cmds.unknownPlugin(q=True,list=True)
		#Returns False if there is no unknownPlugin
		if not unknownPluginList:
			return False
		
		#Add unknownPlugin to item
		for name in unknownPluginList:
			self.standardItemModel.appendItem(name)
		return True
	#The operation of the Remove button
	def remove(self):
		#Get row count
		count = self.standardItemModel.rowCount()
		#Repeat for row count
		for num in range(count):
			#Get check status and name
			(check,name) = self.standardItemModel.rowData(num)
			#If checked, delete unknownPlugin
			if check:
				cmds.unknownPlugin(name,remove=True)
				print (u'deleted %s\n'%(name)),
		
		#Update list contents
		self.refresh()
		
		
class TreeView(QtWidgets.QTreeView):
	def __init__(self):
		super(TreeView, self).__init__()		
		self.setSelectionMode(QtWidgets.QTreeView.ExtendedSelection)
		self.setAlternatingRowColors(True)
		#Changed to be able to sort
		self.setSortingEnabled(True)
		
class StandardItemModel(QtGui.QStandardItemModel):
	def __init__(self):
		super(StandardItemModel, self).__init__(0, 1)
		self.setHeaderData(0, QtCore.Qt.Horizontal, 'Plugin Name')
	
	def appendItem(self, name):		
		standardItem = QtGui.QStandardItem()
		standardItem.setText(str(name))
		standardItem.setEditable(False)
		#Add check items
		standardItem.setCheckable(True)
		#Check
		standardItem.setCheckState(QtCore.Qt.Checked)
		
		self.appendRow([standardItem])
	
	def rowData(self, index):
		check = self.item(index, 0).checkState()
		name  = self.item(index, 0).text()
				
		return (check,name)	
class MainWindow(MayaQWidgetBaseMixin, QtWidgets.QMainWindow):
	def __init__(self):
		super(MainWindow, self).__init__()
		self.setWindowTitle('List Unknown Plugins')
		self.resize(430, 260)
		widget = Widget()
		self.setCentralWidget(widget)
def main():
	window = MainWindow()
	window.show()
	
if __name__ == "__main__":
	main()
		label = QtWidgets.QLabel(
			u'A list of unknown plugins.\n'\
			u'Delete the checked plug-in.')
To start a new line in a long program, close it with ' and insert \ at the end. Then add ʻu` to the beginning of the line.
		#Change the background color of the button
		button.setStyleSheet("background-color: red")
Change the button color to red. It seems that you can also change the frame and do various things. This time, it is set to red with the meaning of calling attention to the action of erasing.
	#The operation of the Remove button
	def remove(self):
		#Get row count
		count = self.standardItemModel.rowCount()
		#Repeat for row count
		for num in range(count):
			#Get check status and name
			(check,name) = self.standardItemModel.rowData(num)
			#If checked, delete unknownPlugin
			if check:
				cmds.unknownPlugin(name,remove=True)
				print (u'deleted %s\n'%(name)),
		
		#Update list contents
		self.refresh()
Get the number of rows and use range to count up.
If count is 5, range (count) becomes 0,1,2,3,4, and check, name is obtained from each row number.
If check is True, the plug-in information will be deleted and printed.
After processing all the steps, re-acquire the unknownPlugin with self.refresh and update the list contents.
		#Add check items
		standardItem.setCheckable(True)
		#Check
		standardItem.setCheckState(QtCore.Qt.Checked)
Add a check item to the item so that you can check it.
QStandardItemModel is convenient because you can add a check box for each item.
This time, I created it by diverting the script created below and partially modifying it. If you create a UI with PySide in this way, you will be able to create various scripts with only partial changes to the contents. When creating a Python script in Maya, please try creating a UI with PySide.
-[Maya Python] Crushing the contents of the script 1 ~ Camera Speed Editor -[Maya Python] Crushing the contents of the script 2 ~ list Notes
Recommended Posts