@@ -92,7 +92,9 @@ class JsPolyHavenAPILoader {
9292 throw new Error ( `Failed to fetch MaterialX data for ${ materialId } ` ) ;
9393 }
9494
95- return await response . json ( ) ;
95+ const result = await response . json ( ) ;
96+ console . log ( `Fetched MaterialX files for ${ materialId } :` , result ) ;
97+ return result ;
9698 } catch ( error ) {
9799 console . error ( `Error fetching material files for ${ materialId } :` , error ) ;
98100 throw error ;
@@ -114,7 +116,9 @@ class JsPolyHavenAPILoader {
114116 throw new Error ( 'Failed to download MaterialX file' ) ;
115117 }
116118
117- return await response . text ( ) ;
119+ const result = await response . text ( ) ;
120+ // Find
121+ return result ;
118122 } catch ( error ) {
119123 console . error ( 'Error downloading MaterialX content:' , error ) ;
120124 throw error ;
@@ -135,6 +139,9 @@ class JsPolyHavenAPILoader {
135139 if ( ! response . ok ) {
136140 throw new Error ( `Failed to download texture from ${ url } ` ) ;
137141 }
142+ else {
143+ console . log ( `> Successfully downloaded texture from ${ url } ` ) ;
144+ }
138145
139146 return await response . blob ( ) ;
140147 } catch ( error ) {
@@ -176,6 +183,7 @@ class JsPolyHavenAPILoader {
176183 // Fetch MaterialX files data
177184 const filesData = await this . fetchMaterialFiles ( material . id ) ;
178185 const mtlxData = filesData . mtlx ?. [ resolution ] ?. mtlx ;
186+ console . log ( '> createMaterialXPackage - fetched MaterialX data:' , mtlxData ) ;
179187
180188 if ( ! mtlxData ) {
181189 throw new Error ( `No MaterialX files found for ${ resolution } resolution` ) ;
@@ -185,14 +193,36 @@ class JsPolyHavenAPILoader {
185193 const zip = new JSZip ( ) ;
186194
187195 // 1. Download and add the main MaterialX file
188- const mtlxContent = await this . downloadMaterialXContent ( mtlxData . url ) ;
189- zip . file ( `${ material . id } .mtlx` , mtlxContent ) ;
190- console . log ( `Added MaterialX file to ZIP: ${ material . id } .mtlx, ${ mtlxContent } ` ) ;
196+ let mtlxContent = await this . downloadMaterialXContent ( mtlxData . url ) ;
191197
192198 // 2. Download and add all included texture files
193199 const textureFiles = mtlxData . include || { } ;
200+ let texturePaths = [ ] ;
201+
194202 const texturePromises = Object . entries ( textureFiles ) . map ( async ( [ path , fileData ] ) => {
195203 try {
204+ console . log ( `Processing texture: ${ path } from URL: ${ fileData . url } ` ) ;
205+
206+ let isEXR = path . toLowerCase ( ) . endsWith ( '.exr' )
207+ // Try changing extension to .png.
208+ if ( isEXR ) {
209+ const prevPath = path ;
210+ path = path . replace ( / \. e x r $ / i, '.png' ) ;
211+ // Replace /exr/ with /png/
212+ fileData . url = fileData . url . replace ( / \/ e x r \/ / i, '/png/' ) ;
213+ // Replace .exr with .png extension
214+ fileData . url = fileData . url . replace ( '.exr' , '.png' ) ;
215+ // Replace .exr with .png in mtlxContent if present
216+ const previousMtlxContent = mtlxContent ;
217+ mtlxContent = previousMtlxContent . replace ( prevPath , path ) ;
218+
219+ console . log ( `EXR file detected. Path: ${ prevPath } -> ${ path } , URL: ${ fileData . url } ` ) ;
220+ if ( previousMtlxContent !== mtlxContent ) {
221+ console . log ( `Updated MaterialX content to replace .exr with .png for texture: ${ path } ` ) ;
222+ }
223+
224+ }
225+
196226 const textureBlob = await this . downloadTexture ( fileData . url ) ;
197227
198228 // Maintain the folder structure from the include paths
@@ -211,15 +241,20 @@ class JsPolyHavenAPILoader {
211241 console . warn ( `EXR file present which may not be supported by MaterialX texture loader: ${ path } ` ) ;
212242 }
213243 else {
214- console . log ( `Added texture to ZIP: ${ path } ` ) ;
244+ console . log ( `Added texture ${ path } to ZIP from URL: ${ fileData . url } ` ) ;
245+ zip . file ( path , textureBlob ) ;
215246 }
247+
248+ texturePaths . push ( path ) ;
249+
216250 } catch ( error ) {
217251 console . error ( `Error downloading texture ${ path } :` , error ) ;
218252 // Add placeholder file if download fails
219253 zip . file ( path , `Failed to download: ${ fileData . url } ` ) ;
220254 }
221255 } ) ;
222256
257+
223258 // 3. Download and add thumbnail
224259 if ( material . thumb_url ) {
225260 try {
@@ -236,15 +271,30 @@ class JsPolyHavenAPILoader {
236271 // Wait for all downloads to complete
237272 await Promise . all ( texturePromises ) ;
238273
239- // Add README file
274+ for ( const textureName of texturePaths ) {
275+ // Patch bad MTLX references in original file
276+ const extenson = textureName . split ( '.' ) . pop ( ) ;
277+ const exrName = textureName . replace ( `.${ extenson } ` , `.exr` ) ;
278+ if ( mtlxContent . includes ( exrName ) ) {
279+ console . log ( `Replace ${ exrName } with ${ textureName } in MaterialX content for preview` ) ;
280+ mtlxContent = mtlxContent . replace ( exrName , textureName ) ;
281+ }
282+ }
283+
284+ // Add Materialx document to ZIP.
285+ // This must be done after texture processing which may
286+ // modify the MTLX image references.
287+ zip . file ( `${ material . id } .mtlx` , mtlxContent ) ;
288+ console . log ( `Added MaterialX file to ZIP: ${ material . id } .mtlx` ) ; //, ${mtlxContent}`);
289+
290+ // Add README file, and thumbnail to root of ZIP
240291 zip . file ( "README.txt" ,
241292 `Material: ${ material . name } \n` +
242293 `Resolution: ${ resolution } \n` +
243294 `Source: https://polyhaven.com/a/${ material . id } \n` +
244295 `Downloaded: ${ new Date ( ) . toISOString ( ) } \n\n` +
245296 `Contains the following files:\n` +
246297 `- ${ material . id } .mtlx\n` +
247- Object . keys ( textureFiles ) . map ( path => `- ${ path } ` ) . join ( '\n' ) +
248298 ( material . thumb_url ? `\n- ${ material . id } _thumbnail.png` : '' )
249299 ) ;
250300
0 commit comments