MaterialXLab API  0.0.1
APIs For MaterialXLab Libraries
Loading...
Searching...
No Matches
MxZipUtils Class Reference

Utility class for unzipping ZIP which contain MaterialX files and dependent images . More...

Public Member Functions

 constructor ()
 Constructor.
 

Static Public Member Functions

static async unzipMaterialXData (zipData)
 Unzips the given ZIP data and returns the MaterialX documents and textures.
 
static async unzipMaterialXUrl (zipUrl)
 Fetch the ZIP file on initial load.
 

Detailed Description

Utility class for unzipping ZIP which contain MaterialX files and dependent images .

Assumes fflate has been imported. Simple way in an htm files is to include: <script src="https://cdn.jsdelivr.net/npm/fflate/umd/index.js"></script>

Definition at line 9 of file JsMaterialXZipUtils.js.

Member Function Documentation

◆ constructor()

MxZipUtils::constructor ( )

Constructor.

Will check if fflate is available. If not available, an error will be logged.

Definition at line 15 of file JsMaterialXZipUtils.js.

15 {
16 // Check if fflate is available
17 if (typeof fflate === 'undefined') {
18 console.error("fflate is not available. Please include fflate in your HTML file.");
19 }
20 }

◆ unzipMaterialXData()

static async MxZipUtils::unzipMaterialXData ( zipData)
static

Unzips the given ZIP data and returns the MaterialX documents and textures.

Parameters
zipDataThe ZIP data to unzip.
Returns
The unzipped documents and textures in an array.

Definition at line 27 of file JsMaterialXZipUtils.js.

27 {
28 let documents = [];
29 let textures = [];
30
31 // Unzip the file using fflate
32 fflate.unzip(zipData, (err, files) => {
33 if (err) {
34 console.error("Error unzipping file:", err);
35 return;
36 }
37
38 // Iterate through files in the ZIP
39 for (let fileName in files) {
40 const fileType = fileName.split('.').pop().toLowerCase();
41
42 if (['mtlx'].includes(fileType)) {
43
44 // Handle text files using TextDecoder
45 const fileContent = new TextDecoder("utf-8").decode(files[fileName]);
46 //console.log(`File: ${fileName} (${fileContent.length} )`);
47 documents.push({ name: fileName, content: fileContent });
48
49 } else {
50 // Get mime type based on extension
51 const standardMimeTypes = {
52 'png': 'image/png',
53 'jpg': 'image/jpeg',
54 'jpeg': 'image/jpeg',
55 'gif': 'image/gif',
56 'bmp': 'image/bmp',
57 'svg': 'image/svg+xml',
58 'webp': 'image/webp',
59 'exr': 'image/exr',
60 'hdr': 'image/vnd.radiance'
61 };
62
63 let mimeType = 'image/' + fileType;
64 if (fileType in standardMimeTypes) {
65 mimeType = standardMimeTypes[fileType];
66
67 const blob = new Blob([files[fileName]], { type: mimeType });
68 const url = URL.createObjectURL(blob);
69
70 textures.push({ name: fileName, url: url });
71 //console.log(`Texture: ${fileName} (${url})`);
72
73 }
74 else {
75 // Check if it's a folder
76 if (files[fileName].length === 0) {
77 console.log(`Folder: ${fileName}`);
78 } else {
79 console.log(`Unsupported file type: ${fileName}`);
80 }
81 }
82 }
83 }
84 });
85
86 return [documents, textures];
87 }

◆ unzipMaterialXUrl()

static async MxZipUtils::unzipMaterialXUrl ( zipUrl)
static

Fetch the ZIP file on initial load.

Parameters
zipUrlThe URL of the ZIP file to fetch.
Returns
The unzipped documents and textures in an array

Definition at line 95 of file JsMaterialXZipUtils.js.

95 {
96 const response = await fetch(zipUrl);
97 const data = await response.arrayBuffer();
98 const zipData = new Uint8Array(data); // Convert to Uint8Array
99 const result = await MxZipUtils.unzipMaterialXData(zipData);
100 return result;
101 }
Utility class for unzipping ZIP which contain MaterialX files and dependent images .
static async unzipMaterialXData(zipData)
Unzips the given ZIP data and returns the MaterialX documents and textures.

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