3@brief A Flask application that connects with the GPUOpen MaterialX server to allow downloading and extracting of materials by regular expression.
7from flask
import Flask, render_template
8from flask_socketio
import SocketIO, emit
9from materialxMaterials
import GPUOpenLoader
as gpuo
12 import MaterialX
as mx
14except ImportError
as e:
15 print(
"MaterialX module not found.")
21 def __init__(self, home):
25 self.
app = Flask(__name__)
42 status_message = f
'Startup: Using MaterialX version: {mx.getVersionString()}'
45 return render_template(self.
home)
48 """Pure virtual method: Must be implemented by subclasses."""
49 raise NotImplementedError(
"Subclasses must implement _setup_event_handler_map")
53 Register SocketIO events.
56 for event_name, handler
in self.event_handlers.items():
57 self.
socketio.on_event(event_name, handler)
59 def run(self, host, port, debug=True):
61 Run the Flask server with SocketIO.
68 A Flask application that connects with the GPUOpen MaterialX server to allow downloading
69 and extracting of materials by regular expression.
73 Initialize the Flask application and the MaterialX loader.
85 Emit a status message to the client.
87 emit(
'materialx_status', {
'message': message }, broadcast=
True)
88 print(
'Python:', message)
92 Handle the 'download_materialx' event, initialize the loader, and send materials data to the client.
94 status_message = f
'Downloaded materials...'
98 self.
loader = gpuo.GPUOpenMaterialLoader()
103 materials_list = self.
loader.getMaterialsAsJsonString()
107 status_message = f
'Downloaded {self.material_count} materials.'
111 emit(
'materialx_downloaded', {
114 'materialsList': materials_list
119 Handle the 'extract_material' event, extract material data, and send it back to the client.
126 expression = data.get(
'expression',
'Default Expression')
127 update_mtlx = data.get(
'update_materialx',
False)
130 data_items = self.
loader.downloadPackageByExpression(expression)
133 for data_item
in data_items:
134 status_message = f
'Extracting material: {data_item[1]}'
136 package = data_item[0]
138 extracted_data = self.
loader.extractPackageData(package,
None)
142 for item
in extracted_data:
143 file_name = item[
'file_name']
144 if item[
'type'] ==
'mtlx':
146 mx_string = item[
'data']
149 doc = mx.createDocument()
150 readOptions = mx.XmlReadOptions()
151 readOptions.readComments =
True
152 readOptions.readNewlines =
True
153 readOptions.upgradeVersion =
True
154 mx.readFromXmlString(doc, mx_string, mx.FileSearchPath(), readOptions)
155 mx_string = mx.writeToXmlString(doc)
157 return_data[file_name] = mx_string
158 elif item[
"type"] ==
'image':
161 image_base64 = self.
loader.convertPilImageToBase64(image)
162 return_data[file_name] = image_base64
164 if len(return_data) > 0:
165 return_list.append({
'title': title,
'data': return_data})
167 if len(return_list) == 0:
170 status_message = f
'Extracted {len(return_list)} materials'
172 emit(
'materialx_extracted', {
'extractedData': return_list}, broadcast=
True)
176 Set up dictionary of mapping event names to their handlers
185 parser = argparse.ArgumentParser(description=
"GPUOpen MaterialX Application")
186 parser.add_argument(
'--host', type=str, default=
'127.0.0.1', help=
"Host address to run the server on (default: 127.0.0.1)")
187 parser.add_argument(
'--port', type=int, default=8080, help=
"Port to run the server on (default: 8080)")
188 parser.add_argument(
'--home', type=str, default=
'MaterialXGPUOpenApp.html', help=
"Home page.")
190 args = parser.parse_args()
195 app.run(host=app_host, port=app_port)
197if __name__ ==
'__main__':
run(self, host, port, debug=True)
Run the Flask server with SocketIO.
_register_socket_events(self)
Register SocketIO events.
_register_routes(self)
Register HTTP routes.
_setup_event_handler_map(self)
Pure virtual method: Must be implemented by subclasses.
A Flask application that connects with the GPUOpen MaterialX server to allow downloading and extracti...
__init__(self, homePage)
Initialize the Flask application and the MaterialX loader.
handle_extract_material(self, data)
Handle the 'extract_material' event, extract material data, and send it back to the client.
_emit_status_message(self, message)
Emit a status message to the client.
handle_download_materialx(self, data)
Handle the 'download_materialx' event, initialize the loader, and send materials data to the client.
_setup_event_handler_map(self)
Set up dictionary of mapping event names to their handlers.