MaterialXWeb 0.0.2
Utilities for using MaterialX Packages with Web clients
Loading...
Searching...
No Matches
MaterialXConversionApp.MaterialXConversionApp Class Reference
Inheritance diagram for MaterialXConversionApp.MaterialXConversionApp:
MaterialXConversionApp.MaterialXFlaskApp

Public Member Functions

 __init__ (self, homePage)
 Constructor.
 
 handle_page_loaded (self, data)
 Handle page load / startup feedback.
 
 handle_load_materialx (self, data)
 Handle loading in of MaterialX document.
 
 handle_render_materialx (self, data)
 Handle request to render MaterialX document.
 
 handle_convert_to_usd (self, data)
 Handle request to convert MaterialX to USD.
 
 handle_convert_to_glTF (self, data)
 Handle request to convert MaterialX to glTF Texture Procedural graph.
 
 handle_have_gltf_converter (self)
 Handle query to see if glTF converter is available.
 
- Public Member Functions inherited from MaterialXConversionApp.MaterialXFlaskApp
 run (self, host, port, deployment_platform, debug=True)
 Run the Flask server with SocketIO.
 

Static Public Member Functions

 convert_png_to_base64 (file_path)
 Utility to load in png image from disk and return Base64 representation.
 

Public Attributes

dict event_handlers
 
- Public Attributes inherited from MaterialXConversionApp.MaterialXFlaskApp
 home = home
 
 app = Flask(__name__)
 
 socketio = SocketIO(self.app)
 
str deployment_platform = 'Local'
 
 os_details = get_os_details()
 

Protected Member Functions

 _setup_event_handler_map (self)
 Set up dictionary of mapping event names to their handlers.
 
- Protected Member Functions inherited from MaterialXConversionApp.MaterialXFlaskApp
 _register_routes (self)
 Register HTTP routes.
 
 _register_socket_events (self)
 Register SocketIO events.
 

Detailed Description

Definition at line 89 of file MaterialXConversionApp.py.

Constructor & Destructor Documentation

◆ __init__()

MaterialXConversionApp.MaterialXConversionApp.__init__ ( self,
homePage )

Constructor.

Reimplemented from MaterialXConversionApp.MaterialXFlaskApp.

Definition at line 92 of file MaterialXConversionApp.py.

92 def __init__(self, homePage):
93 '''
94 Constructor
95 '''
96 super().__init__(homePage)
97

Member Function Documentation

◆ _setup_event_handler_map()

MaterialXConversionApp.MaterialXConversionApp._setup_event_handler_map ( self)
protected

Set up dictionary of mapping event names to their handlers.

Reimplemented from MaterialXConversionApp.MaterialXFlaskApp.

Definition at line 98 of file MaterialXConversionApp.py.

98 def _setup_event_handler_map(self):
99 '''
100 Set up dictionary of mapping event names to their handlers
101 '''
102 event_map = {}
103 event_map['page_loaded'] = self.handle_page_loaded
104
105 self.event_handlers = {
106 'page_loaded': self.handle_page_loaded,
107 'load_materialx': self.handle_load_materialx,
108 'render_materialx': self.handle_render_materialx,
109 'convert_mtlx_to_usd': self.handle_convert_to_usd,
110 'convert_mtlx_to_gltf': self.handle_convert_to_glTF,
111 'query_have_gltf_converter': self.handle_have_gltf_converter,
112 }
113

◆ convert_png_to_base64()

MaterialXConversionApp.MaterialXConversionApp.convert_png_to_base64 ( file_path)
static

Utility to load in png image from disk and return Base64 representation.

Definition at line 234 of file MaterialXConversionApp.py.

234 def convert_png_to_base64(file_path):
235 '''
236 Utility to load in png image from disk and return Base64
237 representation
238 '''
239 with open(file_path, "rb") as image_file:
240 binary_data = image_file.read()
241 return base64.b64encode(binary_data).decode('utf-8')
242

◆ handle_convert_to_glTF()

MaterialXConversionApp.MaterialXConversionApp.handle_convert_to_glTF ( self,
data )

Handle request to convert MaterialX to glTF Texture Procedural graph.

Definition at line 202 of file MaterialXConversionApp.py.

202 def handle_convert_to_glTF(self, data):
203 '''
204 Handle request to convert MaterialX to glTF Texture Procedural
205 graph
206 '''
207 if not have_gltf_converter:
208 emit('gltf_converted', {'document': '{}'}, broadcast=True)
209 return
210
211 materialx_string = data.get('materialxDocument', '')
212 print('> Server: convert_to_glTF event received')
213 stdlib, _ = MxGLTFPTUtil.load_standard_libraries()
214 doc = MxGLTFPTUtil.create_working_document([stdlib])
215 if len(materialx_string) == 0:
216 return
217 mx.readFromXmlString(doc, materialx_string)
218 print('>> MaterialX document loaded')
219 converter = MxGLTFPT.glTFMaterialXConverter()
220 json_string, status = converter.materialX_to_glTF(doc)
221 if not json_string:
222 print('>> Error converting to glTF:', status)
223 json_string = '{}'
224 print('>> glTF JSON created:')
225 emit('gltf_converted', {'document': json_string}, broadcast=True)
226

◆ handle_convert_to_usd()

MaterialXConversionApp.MaterialXConversionApp.handle_convert_to_usd ( self,
data )

Handle request to convert MaterialX to USD.

Definition at line 179 of file MaterialXConversionApp.py.

179 def handle_convert_to_usd(self, data):
180 '''
181 Handle request to convert MaterialX to USD
182 '''
183 if not have_usd_converter:
184 emit('usd_converted', {'usdDocument': ''}, broadcast=True)
185 return
186
187 materialx_string = data.get('materialxDocument', '')
188 print('> Server: convert-to-usd event received')
189 doc = mx.createDocument()
190 if len(materialx_string) == 0:
191 return
192 try:
193 mx.readFromXmlString(doc, materialx_string)
194 print('>> MaterialX document loaded')
195 stage_string = convertMtlxToUsd(doc, True)
196 print('>> USD stage created.')
197 emit('usd_converted', {'usdDocument': stage_string}, broadcast=True)
198 except Exception as e:
199 print(f'>> Error during USD conversion: {e}')
200 emit('usd_converted', {'usdDocument': ''}, broadcast=True)
201

◆ handle_have_gltf_converter()

MaterialXConversionApp.MaterialXConversionApp.handle_have_gltf_converter ( self)

Handle query to see if glTF converter is available.

Definition at line 227 of file MaterialXConversionApp.py.

227 def handle_have_gltf_converter(self):
228 '''
229 Handle query to see if glTF converter is available
230 '''
231 emit('have_gltf_converter', {'have_gltf_converter': have_gltf_converter}, broadcast=True)
232

◆ handle_load_materialx()

MaterialXConversionApp.MaterialXConversionApp.handle_load_materialx ( self,
data )

Handle loading in of MaterialX document.

Definition at line 121 of file MaterialXConversionApp.py.

121 def handle_load_materialx(self, data):
122 '''
123 Handle loading in of MaterialX document
124 '''
125 materialx_file = data.get('materialxFile', 'Default MaterialX File')
126 print('> Server: load materialx event received. File:', materialx_file)
127 materialx_content = data.get('content', 'MaterialX content')
128 doc = mx.createDocument()
129 if len(materialx_content) == 0:
130 return
131 mx.readFromXmlString(doc, materialx_content)
132 print('>> MaterialX Document loaded')
133 doc_string = mx.writeToXmlString(doc)
134 emit('materialx_loaded', {'materialxDocument': doc_string}, broadcast=True)
135

◆ handle_page_loaded()

MaterialXConversionApp.MaterialXConversionApp.handle_page_loaded ( self,
data )

Handle page load / startup feedback.

Definition at line 114 of file MaterialXConversionApp.py.

114 def handle_page_loaded(self, data):
115 '''
116 Handle page load / startup feedback
117 '''
118 status = '> Using MaterialX version: ' + mx.getVersionString()
119 emit('materialx_version', {'status': status}, broadcast=True)
120

◆ handle_render_materialx()

MaterialXConversionApp.MaterialXConversionApp.handle_render_materialx ( self,
data )

Handle request to render MaterialX document.

Definition at line 136 of file MaterialXConversionApp.py.

136 def handle_render_materialx(self, data):
137 '''
138 Handle request to render MaterialX document
139 '''
140 materialx_string = data.get('materialxDocument', 'MaterialX content')
141 print('> Server: render_materialx event received')
142
143 # Get environment variable: MATERIALX_DEFAULT_VIEWER
144 ilm_viewer = os.getenv('MATERIALX_DEFAULT_VIEWER', '')
145 if len(ilm_viewer) == 0:
146 print('>> MATERIALX_DEFAULT_VIEWER environment variable not set')
147 return
148
149 doc = mx.createDocument()
150 if len(materialx_string) == 0:
151 return
152
153 stdlib = mx.createDocument()
154 mx.loadLibraries(mx.getDefaultDataLibraryFolders(), mx.getDefaultDataSearchPath(), stdlib)
155 doc.importLibrary(stdlib)
156 mx.readFromXmlString(doc, materialx_string)
157
158 # Get platform temporary folder location
159 temp_location = os.getenv('TEMP', '/tmp')
160 if not os.path.exists(temp_location):
161 temp_location = '/tmp'
162 # Generate a temp file name based on the current date and time
163 temp_name = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '.mtlx'
164 temp_file = f'{temp_location}/{temp_name}'
165 mx.writeToXmlFile(doc, temp_file)
166 capture_filename = temp_file.replace(".mtlx", ".png")
167 cmd = f'{ilm_viewer} --screenWidth 512 --screenHeight 512 '
168 cmd += f' --captureFilename {capture_filename} --material {temp_file}'
169 print('>> Rendering:', cmd)
170 os.system(cmd)
171 # Delete the temp files
172 os.remove(temp_file)
173
174 image_base64 = self.convert_png_to_base64(capture_filename)
175 os.remove(capture_filename)
176 print('>> Emit materialx_rendered event')
177 emit('materialx_rendered', {'image': image_base64}, broadcast=True)
178

Member Data Documentation

◆ event_handlers

dict MaterialXConversionApp.MaterialXConversionApp.event_handlers
Initial value:
= {
'page_loaded': self.handle_page_loaded,
'load_materialx': self.handle_load_materialx,
'render_materialx': self.handle_render_materialx,
'convert_mtlx_to_usd': self.handle_convert_to_usd,
'convert_mtlx_to_gltf': self.handle_convert_to_glTF,
'query_have_gltf_converter': self.handle_have_gltf_converter,
}

Definition at line 105 of file MaterialXConversionApp.py.


The documentation for this class was generated from the following file: