3@brief A Flask application that connects with the GPUOpen MaterialX server to allow downloading and extracting of materials by regular expression.
6from flask
import Flask, render_template
7from flask_socketio
import SocketIO, emit
8from materialxMaterials
import GPUOpenLoader
as gpuo
14 def __init__(self, home):
18 self.
app = Flask(__name__)
37 return render_template(self.
home)
40 """Pure virtual method: Must be implemented by subclasses."""
41 raise NotImplementedError(
"Subclasses must implement _setup_event_handler_map")
45 Register SocketIO events.
48 for event_name, handler
in self.event_handlers.items():
49 self.
socketio.on_event(event_name, handler)
51 def run(self, host, port, debug=True):
53 Run the Flask server with SocketIO.
60 A Flask application that connects with the GPUOpen MaterialX server to allow downloading
61 and extracting of materials by regular expression.
65 Initialize the Flask application and the MaterialX loader.
77 Emit a status message to the client.
79 emit(
'materialx_status', {
'message': message }, broadcast=
True)
80 print(
'Python:', message)
84 Handle the 'download_materialx' event, initialize the loader, and send materials data to the client.
86 status_message = f
'Downloaded materials...'
90 self.
loader = gpuo.GPUOpenMaterialLoader()
95 materials_list = self.
loader.getMaterialsAsJsonString()
99 status_message = f
'Downloaded {self.material_count} materials.'
103 emit(
'materialx_downloaded', {
106 'materialsList': materials_list
111 Handle the 'extract_material' event, extract material data, and send it back to the client.
117 expression = data.get(
'expression',
'Default Expression')
118 data_items = self.
loader.downloadPackageByExpression(expression)
121 for data_item
in data_items:
122 status_message = f
'Extracting material: {data_item[1]}'
124 package = data_item[0]
126 extracted_data = self.
loader.extractPackageData(package,
None)
130 for item
in extracted_data:
131 file_name = item[
'file_name']
132 if item[
'type'] ==
'mtlx':
134 return_data[file_name] = item[
'data']
135 elif item[
"type"] ==
'image':
138 image_base64 = self.
loader.convertPilImageToBase64(image)
139 return_data[file_name] = image_base64
141 if len(return_data) > 0:
142 return_list.append({
'title': title,
'data': return_data})
144 if len(return_list) == 0:
147 status_message = f
'Extracted {len(return_list)} materials'
149 emit(
'materialx_extracted', {
'extractedData': return_list}, broadcast=
True)
153 Set up dictionary of mapping event names to their handlers
162 parser = argparse.ArgumentParser(description=
"GPUOpen MaterialX Application")
163 parser.add_argument(
'--host', type=str, default=
'127.0.0.1', help=
"Host address to run the server on (default: 127.0.0.1)")
164 parser.add_argument(
'--port', type=int, default=8080, help=
"Port to run the server on (default: 8080)")
165 parser.add_argument(
'--home', type=str, default=
'MaterialXGPUOpenApp.html', help=
"Home page.")
167 args = parser.parse_args()
172 app.run(host=app_host, port=app_port)
174if __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.