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

Public Member Functions

 constructor (socketLibrary, server)
 
 handleImageRendered (data)
 
 setupEventHandlers ()
 
 setupXML ()
 
- 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 MaterialXConversionApp.js.

Member Function Documentation

◆ constructor()

MaterialX_Conversion_Client::constructor ( socketLibrary,
server )

Definition at line 4 of file MaterialXConversionApp.js.

4 {
5 // Call parent to setup socket I/O.
6 super(socketLibrary, server);
7
8 this.editor = null;
9 this.usdEditor = null;
10 this.gltfEditor = null;
11
12 this.setupEventHandlers = this.setupEventHandlers.bind(this);
13 this.setupXML = this.setupXML.bind(this);
14 }

◆ handleImageRendered()

MaterialX_Conversion_Client::handleImageRendered ( data)

Definition at line 16 of file MaterialXConversionApp.js.

17 {
18 console.log('WEB: mtlx rendered event:', data.image ? 'Have image' : 'No image');
19 let base64String = data.image
20 if (base64String) {
21 const base64String = data.image;
22
23 // Decode Base64 to binary and create a Blob
24 const binary = atob(base64String);
25 const array = new Uint8Array(binary.length);
26 for (let i = 0; i < binary.length; i++) {
27 array[i] = binary.charCodeAt(i);
28 }
29 const blob = new Blob([array], { type: 'image/png' });
30
31 // Create a URL for the Blob and display the image
32 const url = URL.createObjectURL(blob);
33 document.getElementById('mtlxImage').src = url;
34 document.getElementById('mtlxImage').style.display = 'block';
35 }
36 else {
37 document.getElementById('mtlxImage').style.display = 'none';
38 }
39 }

◆ setupEventHandlers()

MaterialX_Conversion_Client::setupEventHandlers ( )

Definition at line 41 of file MaterialXConversionApp.js.

42 {
43 let app = this;
44
45 document.getElementById('getMTLXButton').addEventListener('click', function () {
46 console.log("WEB: Emitting load_materialx event");
47
48 // Add UI to select the materialx file from a dialog
49 const input = document.createElement('input');
50 input.type = 'file';
51 input.accept = '.mtlx';
52 let fileName = '';
53 input.onchange = function (event) {
54 const file = event.target.files[0];
55 if (file) {
56 const reader = new FileReader();
57 reader.onload = function (e) {
58 const content = e.target.result;
59 console.log('socket: ', app)
60 app.socket.emit('load_materialx', { materialxFile: fileName, content: content });
61 }
62 reader.readAsText(file);
63 };
64 };
65 input.click();
66 });
67 this.socket.on('materialx_loaded', function (data) {
68 console.log('WEB: materialx loaded event:') //, data.materialxDocument);
69 app.editor.setValue(data.materialxDocument);
70 });
71
72 document.getElementById('renderMTLXButton').addEventListener('click', function () {
73 console.log('Web: Emitting render_mtlx event');
74 app.socket.emit('render_materialx', { materialxDocument: document.getElementById('mtlxOutput').value });
75 });
76 this.socket.on('materialx_rendered', function (data) {
77 app.handleImageRendered(data);
78 });
79
80 // USD Conversion
81 document.getElementById('convertToUsd').addEventListener('click', function () {
82 console.log('Web: Emitting convert_mtlx_to_usd event');
83 app.socket.emit('convert_mtlx_to_usd', { materialxDocument: document.getElementById('mtlxOutput').value });
84 });
85 this.socket.on('usd_converted', function (data) {
86 console.log('WEB: usd converted event:'); //, data.usdDocument);
87 app.usdEditor.setValue(data.usdDocument);
88 });
89
90 // glTF Conversion
91 document.getElementById('convertToglTF').addEventListener('click', function () {
92 console.log('Web: Emitting convert_mtlx_to_gltf event');
93 app.socket.emit('convert_mtlx_to_gltf', { materialxDocument: document.getElementById('mtlxOutput').value });
94 });
95 this.socket.on('gltf_converted', function (data) {
96 console.log('WEB: gltf converted event:'); //, data.usdDocument);
97 app.glTFEditor.setValue(data.document);
98 });
99
100 // Handle Python status messages
101 this.socket.on('materialx_version', function (data) {
102 console.log('WEB: materialx_version event:', data.status);
103 });
104 }

◆ setupXML()

MaterialX_Conversion_Client::setupXML ( )

Definition at line 106 of file MaterialXConversionApp.js.

107 {
108 // Initialize CodeMirror for XML syntax highlighting
109 const materialXTextArea = document.getElementById('mtlxOutput');
110 this.editor = CodeMirror.fromTextArea(materialXTextArea, {
111 mode: 'application/xml',
112 lineNumbers: true,
113 dragDrop: true,
114 theme: 'night'
115 });
116
117 // Optional: Set an initial value for the textarea
118 const initialXML = '<?xml version="1.0" encoding="utf-8"?>\r\n<materialx version="1.38" colorspace="lin_rec709">\r\n</materialx>';
119 materialXTextArea.value = initialXML;
120 this.editor.setValue(initialXML);
121
122 // Update CodeMirror whenever the textarea content changes
123 this.editor.on('change', () => {
124 // Copy the content from CodeMirror back to the textarea
125 materialXTextArea.value = this.editor.getValue();
126 });
127 this.editor.setSize('auto', '300px');
128
130
131 const usdOutput = document.getElementById('usdOutput');
132 this.usdEditor = CodeMirror.fromTextArea(usdOutput, {
133 mode: 'application/javascript',
134 lineNumbers: true,
135 dragDrop: true,
136 theme: 'dracula'
137 });
138
139 // Optional: Set an initial value for the textarea
140 const initialUsd = ''; //'<?xml version="1.0" encoding="utf-8"?>\r\n<materialx version="1.38" colorspace="lin_rec709">\r\n</materialx>';
141 usdOutput.value = initialUsd;
142 this.usdEditor.setValue(initialUsd);
143
144 // Update CodeMirror whenever the textarea content changes
145 this.usdEditor.on('change', () => {
146 // Copy the content from CodeMirror back to the textarea
147 usdOutput.value = this.usdEditor.getValue();
148 });
149 this.usdEditor.setSize('auto', '300px');
150
152 const glTFOutput = document.getElementById('glTFOutput');
153 this.glTFEditor = CodeMirror.fromTextArea(glTFOutput, {
154 mode: 'application/json',
155 lineNumbers: true,
156 dragDrop: true,
157 theme: 'night'
158 });
159
160 // Optional: Set an initial value for the textarea
161 const initialglTF = ''; //'<?xml version="1.0" encoding="utf-8"?>\r\n<materialx version="1.38" colorspace="lin_rec709">\r\n</materialx>';
162 glTFOutput.value = initialglTF;
163 this.glTFEditor.setValue(initialglTF);
164
165 // Update CodeMirror whenever the textarea content changes
166 this.glTFEditor.on('change', () => {
167 // Copy the content from CodeMirror back to the textarea
168 glTFOutput.value = this.glTFEditor.getValue();
169 });
170 this.glTFEditor.setSize('auto', '300px');
171 }

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