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

Public Member Functions

 constructor (socketLibrary, server)
 
 findMaterialByName (name)
 
 updateStatusInput (message, force=false)
 
 handleMaterialXDownLoad (data)
 
 handleMaterialXExtract (data)
 
 extractMaterials ()
 
 downloadMaterials ()
 
 setupEventHandlers ()
 
 setupXML ()
 
 populateForm (data)
 
- Public Member Functions inherited from WebSocketClient
 constructor (socketLibrary, server)
 
async initialize (socketLibrary, server)
 
 setupEventHandlers ()
 
 emit (message, data)
 
 constructor (socketLibrary, server)
 
async initialize (socketLibrary, server)
 
 setupEventHandlers ()
 
 emit (message, data)
 
 constructor (socketLibrary, server)
 
async initialize (socketLibrary, server)
 
 setupEventHandlers ()
 
 emit (message, data)
 

Detailed Description

Definition at line 3 of file MaterialXGPUOpenClient.js.

Member Function Documentation

◆ constructor()

MaterialX_GPUOpen_Client::constructor ( socketLibrary,
server )

Definition at line 5 of file MaterialXGPUOpenClient.js.

5 {
6 // Call parent to setup socket I/O.
7 super(socketLibrary, server);
8
9 this.editor = null;
10 this.extractedEditor = null;
11 this.materialsList = [];
12 this.materialNames = [];
13 this.materialCount = 0;
14
15 // Bind class methods to `this`
16 this.findMaterialByName = this.findMaterialByName.bind(this);
17 this.populateForm = this.populateForm.bind(this);
18 this.setupEventHandlers = this.setupEventHandlers.bind(this);
19 this.setupXML = this.setupXML.bind(this);
20
21 // Setup XML editors
22 this.setupXML();
23 }

◆ downloadMaterials()

MaterialX_GPUOpen_Client::downloadMaterials ( )

Definition at line 184 of file MaterialXGPUOpenClient.js.

184 {
185 console.log("WEB: Emitting download_materialx event");
186 this.emit('download_materialx', {});
187 }

◆ extractMaterials()

MaterialX_GPUOpen_Client::extractMaterials ( )

Definition at line 176 of file MaterialXGPUOpenClient.js.

176 {
177 const materialSelect = document.getElementById('materialSelect');
178 const update_mtlx = document.getElementById('update_mtlx').checked;
179 let selectedItem = materialSelect.options[materialSelect.selectedIndex].text;
180 console.log("WEB: Emitting extract_material event");
181 this.emit('extract_material', { expression: selectedItem, update_materialx: update_mtlx });
182 }

◆ findMaterialByName()

MaterialX_GPUOpen_Client::findMaterialByName ( name)

Definition at line 26 of file MaterialXGPUOpenClient.js.

26 {
27 let foundMaterial = null;
28
29 for (const element of this.materialsList) {
30 let resultsArray = JSON.parse(element).results;
31 if (resultsArray) {
32 for (const result of resultsArray) {
33 if (result.title === name) {
34 foundMaterial = result;
35 //console.log('>>>>>>>>>> Found Material:', result.title);
36 this.populateForm(result);
37 return foundMaterial;
38 }
39 }
40 }
41 }
42 return null; // Return null if no match is found
43 }

◆ handleMaterialXDownLoad()

MaterialX_GPUOpen_Client::handleMaterialXDownLoad ( data)

Definition at line 56 of file MaterialXGPUOpenClient.js.

57 {
58 console.log('WEB: materialx downloaded event:', data);
59 this.materialCount = data.materialCount;
60 this.materialsList = data.materialsList;
61 this.materialNames = data.materialNames;
62
63 if (this.materialCount > 0) {
64 let listString = '';
65 for (const element of this.materialsList) {
66 listString += element + '\n';
67 }
68 this.editor.setValue(listString);
69
70 // Populate the material select dropdown
71 const materialSelect = document.getElementById('materialSelect');
72 materialSelect.innerHTML = ''; // Clear existing options
73 this.materialNames.forEach((name, index) => {
74 const option = document.createElement('option');
75 option.value = index + 1;
76 option.text = name;
77 materialSelect.appendChild(option);
78 });
79
80 // Populate the form with the first material
81 const firstMaterial = JSON.parse(this.materialsList[0]).results[0];
82 if (firstMaterial) {
83 this.populateForm(firstMaterial);
84 }
85 }
86 }

◆ handleMaterialXExtract()

MaterialX_GPUOpen_Client::handleMaterialXExtract ( data)

Definition at line 88 of file MaterialXGPUOpenClient.js.

89 {
90 console.log('WEB: materialx extracted event:', data.extractedData);
91 const extractedData = data.extractedData[0];
92 const title = extractedData.title;
93 console.log('Title:', title);
94
95 const dataObj = extractedData.data;
96 const imageDOM = document.getElementById('extracted_images');
97 imageDOM.innerHTML = ''; // Clear existing images
98
99 const mtlxDOM = document.getElementById('extracted_mtlx');
100 this.extractedEditor.setValue('');
101
102 // Extract MTLX and images out.
103 // Optionally save zip of data to file.
104 let save_extracted = document.getElementById('save_extracted').checked;
105 let zip = save_extracted ? new JSZip() : null;
106 for (const key in dataObj) {
107 if (key.endsWith('.mtlx')) {
108 this.extractedEditor.setValue(dataObj[key]);
109 // Add the extracted MaterialX file to the zip
110 if (zip)
111 zip.file(key, dataObj[key]);
112 } else {
113 const base64String = dataObj[key];
114 const binary = atob(base64String);
115 const array = new Uint8Array(binary.length);
116 for (let i = 0; i < binary.length; i++) {
117 array[i] = binary.charCodeAt(i);
118 }
119 const extension = key.split('.').pop();
120 const blob = new Blob([array], { type: `image/${extension}` });
121 const url = URL.createObjectURL(blob);
122
123 // Create a container for the image and label
124 const imageContainer = document.createElement('div');
125 imageContainer.style.display = 'inline-block';
126 imageContainer.style.margin = '10px';
127 imageContainer.style.textAlign = 'center'; // Center the label under the image
128
129 // Create the image element
130 const img = document.createElement('img');
131 img.src = url;
132 img.width = 200;
133 img.alt = key;
134
135 // Add the key as a tooltip
136 img.title = key;
137
138 // Create a label for the image
139 const label = document.createElement('div');
140 label.innerText = key;
141 label.style.fontSize = '0.8rem';
142 //label.style.color = '#FFF'; //
143
144 // Append the image and label to the container
145 imageContainer.appendChild(img);
146 imageContainer.appendChild(label);
147
148 // Append the container to the image DOM
149 imageDOM.appendChild(imageContainer);
150
151 // Convert the blob to a file and add it to the zip
152 if (zip)
153 {
154 const imgFile = new File([blob], key, { type: 'image/${extension}' });
155 zip.file(key, imgFile);
156 }
157 }
158 }
159
160 // Create the zip file asynchronously
161 if (zip)
162 {
163 zip.generateAsync({ type: 'blob' }).then(function(content) {
164 // Create a download link for the zip file
165 const link = document.createElement('a');
166 link.href = URL.createObjectURL(content);
167 link.download = title + '.zip'; // Set the name of the zip file
168 link.click(); // Trigger the download
169 })
170 .catch(function(error) {
171 console.error('Error creating zip file:', error);
172 });
173 }
174 }

◆ populateForm()

MaterialX_GPUOpen_Client::populateForm ( data)

Definition at line 240 of file MaterialXGPUOpenClient.js.

240 {
241 document.getElementById('m_title').value = data.title || '';
242 document.getElementById('author').value = data.author || '';
243 const pdate = data.published_date || '';
244 if (pdate) {
245 const date = new Date(pdate);
246 const offset = date.getTimezoneOffset();
247 const localDate = new Date(date.getTime() - offset * 60 * 1000);
248 const formattedDate = localDate.toISOString().slice(0, 16);
249 document.getElementById('published_date').value = formattedDate;
250 }
251 document.getElementById('mtlx_filename').value = data.mtlx_filename || '';
252 document.getElementById('category').value = data.category || '';
253 document.getElementById('status').value = data.status || 'Unpublished';
254 document.getElementById('tags').value = data.tags ? data.tags.join(', ') : '';
255 document.getElementById('packages').value = data.packages ? data.packages.join(', ') : '';
256 document.getElementById('renders').value = data.renders ? data.renders.join(', ') : '';
257 document.getElementById('description').value = data.description || '';
258 }

◆ setupEventHandlers()

MaterialX_GPUOpen_Client::setupEventHandlers ( )

Definition at line 189 of file MaterialXGPUOpenClient.js.

189 {
190 // Setup clear status button
191 document.getElementById('clear_status').addEventListener('click', () => {
192 this.updateStatusInput('Status', true);
193 });
194
195 // Bind "Extract Material" button click
196 document.getElementById('extract_material').addEventListener('click', () => {
197 this.extractMaterials();
198 });
199
200 // Bind "Download MaterialX" button click
201 document.getElementById('getMTLXButton').addEventListener('click', () => {
202 this.downloadMaterials();
203 });
204
205 // Set up socket message event handlers
206 this.webSocketWrapper = new WebSocketEventHandlers(this.socket, {
207 materialx_status: (data) => { console.log('WEB: materialx status event:', data.message); this.updateStatusInput(data.message) },
208 materialx_downloaded: (data) => { this.handleMaterialXDownLoad(data) },
209 materialx_extracted: (data) => { this.handleMaterialXExtract(data) }
210 });
211
212 // Update material selection
213 document.getElementById('materialSelect').addEventListener('change', () => {
214 const materialSelect = document.getElementById('materialSelect');
215 const selectedItem = materialSelect.options[materialSelect.selectedIndex].text;
216 this.findMaterialByName(selectedItem);
217 });
218 }

◆ setupXML()

MaterialX_GPUOpen_Client::setupXML ( )

Definition at line 220 of file MaterialXGPUOpenClient.js.

220 {
221 // Initialize CodeMirror for MaterialX content
222 const materialXTextArea = document.getElementById('mtlxOutput');
223 this.editor = CodeMirror.fromTextArea(materialXTextArea, {
224 mode: 'application/json',
225 lineNumbers: true,
226 theme: 'dracula',
227 });
228 this.editor.setSize('auto', '300px');
229
230 // Initialize CodeMirror for extracted MaterialX content
231 const materialXTextArea2 = document.getElementById('extracted_mtlx');
232 this.extractedEditor = CodeMirror.fromTextArea(materialXTextArea2, {
233 mode: 'application/xml',
234 lineNumbers: true,
235 theme: 'dracula',
236 });
237 this.extractedEditor.setSize('auto', '300px');
238 }

◆ updateStatusInput()

MaterialX_GPUOpen_Client::updateStatusInput ( message,
force = false )

Definition at line 45 of file MaterialXGPUOpenClient.js.

46 {
47 const inputDOM = document.getElementById('status_message');
48 if (inputDOM.value == 'Status' || force)
49 inputDOM.value = message
50 else
51 inputDOM.value += '\n' + message
52 // Scroll to the bottom of the textarea
53 inputDOM.scrollTop = inputDOM.scrollHeight;
54 }

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