Example UI for listing Substance materials¶
This example collects all the substance materials of the current scene and displays some of their basic properties.
vrSubstanceExample.py¶
1# © 2024 Autodesk, Inc. All rights reserved.
2
3# vrSubstanceExample shows a small UI for querying the substance materials
4# in the scene and registers it as an script plugin into VRED
5from PySide6 import QtCore, QtGui, QtWidgets
6import uiTools
7from vrKernelServices import vrdSubstanceMaterial
8
9# Create a form using the UI file created by QT Designer
10vrSubstanceExample_form, vrSubstanceExample_base = uiTools.loadUiType('vrSubstanceExample.ui')
11
12# We will use this class to generate python enumerations.
13# For each query type we define a seperate value
14class vrQueryType:
15 Archive = 0
16 Graph = 1
17 Preset = 2
18
19# This class implement all functionality from substance material query dialog
20class vrSubstanceExample(vrSubstanceExample_form, vrSubstanceExample_base):
21 # Constructor set connections between button and class methods
22 # And add a resize grid an vottom rigth corner
23 def __init__(self, parent=None):
24 super(vrSubstanceExample, self).__init__(parent)
25 parent.layout().addWidget(self)
26 self.parent = parent
27 self.setupUi(self)
28 self.type = vrQueryType.Archive
29
30 # add resize grip in bottom right corner.
31 self.sizeGrip = QtWidgets.QSizeGrip(parent)
32 self.sizeGrip.setFixedSize(16, 16)
33 self.sizeGrip.move(parent.rect().bottomRight() - self.sizeGrip.rect().bottomRight())
34 self.sizeGrip.raise_()
35 self.sizeGrip.show()
36
37 # connect signals with methods
38 self.pbQuery.clicked.connect(self.makeQuery)
39 self.rbArchive.clicked.connect(self.activateArchive)
40 self.rbGraph.clicked.connect(self.activateGraph)
41 self.rbPreset.clicked.connect(self.activatePreset)
42
43 # Move resize grip to bottom right corner
44 def resizeEvent(self, event):
45 self.sizeGrip.move(self.parent.rect().bottomRight() - self.sizeGrip.rect().bottomRight())
46 self.sizeGrip.raise_()
47
48 # This method is called, when Query button was pressed
49 def makeQuery(self):
50 # Clean result table first
51 self.tableWidget.clearContents()
52
53 # Get all material and remove indvalid and non substance materials
54 materials = vrMaterialService.getAllMaterials()
55 valid = [item for item in materials if item.isValid()]
56 substance = [item for item in valid if item.isType(vrdSubstanceMaterial)]
57
58 # Look for the substance material name filter
59 # If filter is set, look for all matching substance materials
60 filter = self.leFilter.text()
61 if not filter:
62 filtered = substance
63 else:
64 filtered = [item for item in substance if filter in item.getName()]
65
66 # Depending from type property we will call a method for a query
67 # All results from will be shown in result table
68 if self.type == vrQueryType.Archive:
69 self.showArchive(filtered)
70
71 if self.type == vrQueryType.Graph:
72 self.showGraph(filtered)
73
74 if self.type == vrQueryType.Preset:
75 self.showPreset(filtered)
76
77 # Shows for each item in a list of substance material the material name and all defined presets
78 # Iterate over all material and all its presets
79 # A substance material may contain one ore more presets
80 # Substance materials with no presets will be suppressed
81 def showPreset(self, materials):
82 # Rename header column
83 resultItem = QtWidgets.QTableWidgetItem("Presets")
84 self.tableWidget.setHorizontalHeaderItem(1, resultItem)
85
86 row = 0
87 for material in materials:
88 # Substance material name and a list of all its preset names
89 name = material.getName()
90 presets = material.getPresets()
91
92 # Extend result table size
93 count = len(presets)
94 old = self.tableWidget.rowCount()
95 self.tableWidget.setRowCount(old + count)
96
97 # Iterate over all presets and show name in the result table
98 pos = 0
99 for preset in presets:
100 # Substance material name together the preset ID and preset name
101 key = "{}({})".format(name, pos)
102 itemName = QtWidgets.QTableWidgetItem(key)
103
104 # Create the result table entries
105 itemPreset = QtWidgets.QTableWidgetItem(preset.getName())
106 self.tableWidget.setItem(row, 0, itemName)
107 self.tableWidget.setItem(row, 1, itemPreset)
108 row = row + 1
109 pos = pos + 1
110
111 # Shows for each item in a list of substance material the material name and the active graph
112 # Iterate over all materials and query the active graph
113 def showGraph(self, materials):
114 # Rename header column
115 resultItem = QtWidgets.QTableWidgetItem("Active Graph")
116 self.tableWidget.setHorizontalHeaderItem(1, resultItem)
117
118 # Set result table size
119 count = len(materials)
120 self.tableWidget.setRowCount(count)
121
122 row = 0
123 for material in materials:
124 # Substance material namen and its active graph
125 name = material.getName()
126 graph = material.getActiveGraphName()
127
128 # Create the result table entries
129 itemName = QtWidgets.QTableWidgetItem(name)
130 itemGraph = QtWidgets.QTableWidgetItem(graph)
131 self.tableWidget.setItem(row, 0, itemName)
132 self.tableWidget.setItem(row, 1, itemGraph)
133 row = row + 1
134
135 # Shows for each item in a list of substance material the material name and the full path of the substance archive
136 # Iterate over all materials and query the archive path
137 def showArchive(self, materials):
138 # Rename header column
139 resultItem = QtWidgets.QTableWidgetItem("Archive Path")
140 self.tableWidget.setHorizontalHeaderItem(1, resultItem)
141
142 # Set result table size
143 count = len(materials)
144 self.tableWidget.setRowCount(count)
145
146 row = 0
147 for material in materials:
148 # Substance material name and the archive path
149 name = material.getName()
150 archive = material.getArchivePath()
151
152 # Create the result table entries
153 itemName = QtWidgets.QTableWidgetItem(name)
154 itemArchive = QtWidgets.QTableWidgetItem(archive)
155 self.tableWidget.setItem(row, 0, itemName)
156 self.tableWidget.setItem(row, 1, itemArchive)
157 row = row + 1
158
159 # Handler called from 'Path' radio button set the type property
160 def activateArchive(self):
161 self.type = vrQueryType.Archive
162
163 # Handler called from 'Graph' radio button set the type property
164 def activateGraph(self):
165 self.type = vrQueryType.Graph
166
167 # Handler called from 'Preset' radio button set the type property
168 def activatePreset(self):
169 self.type = vrQueryType.Preset
170
171# Create one instance from substance material query result form
172substanceExample = vrSubstanceExample(VREDPluginWidget)