43 const categories =
new Set();
45 for (
const [
id, materialData] of Object.entries(rawData)) {
47 if (!materialData.name || !materialData.categories)
continue;
50 let authorsList =
'Author(s): ';
51 const authors = materialData.authors || {};
52 const authorNames = Object.keys(authors);
53 authorsList += authorNames.join(
', ');
55 let max_resolution = materialData.max_resolution
56 let maxresolutionString =
'';
58 maxresolutionString = `${max_resolution[0]} x ${max_resolution[1]}`
63 name: materialData.name,
64 description: authorsList,
65 categories: materialData.categories,
66 tags: materialData.tags || [],
68 maps: materialData.maxresolutionString || {}
71 materials.push(material);
74 material.categories.forEach(cat => categories.add(cat));
77 return { materials, categories };
178 const mtlxData = filesData.mtlx?.[resolution]?.mtlx;
181 throw new Error(`No MaterialX files found
for ${resolution} resolution`);
185 const zip =
new JSZip();
189 zip.file(`${material.id}.mtlx`, mtlxContent);
190 console.log(`Added MaterialX file to ZIP: ${material.id}.mtlx, ${mtlxContent}`);
193 const textureFiles = mtlxData.include || {};
194 const texturePromises = Object.entries(textureFiles).map(async ([path, fileData]) => {
199 const pathParts = path.split(
'/');
200 let currentFolder = zip;
203 for (let i = 0; i < pathParts.length - 1; i++) {
204 const folderName = pathParts[i];
205 currentFolder = currentFolder.folder(folderName);
208 currentFolder.file(pathParts[pathParts.length - 1], textureBlob);
210 if (path.toLowerCase().endsWith(
'.exr')) {
211 console.warn(`EXR file present which may not be supported by MaterialX texture loader: ${path}`);
214 console.log(`Added texture to ZIP: ${path}`);
217 console.error(`Error downloading texture ${path}:`, error);
219 zip.file(path, `Failed to download: ${fileData.url}`);
224 if (material.thumb_url) {
227 const thumbUrl =
new URL(material.thumb_url);
228 const thumbPath = thumbUrl.pathname.split(
'/').pop();
229 const thumbExt = thumbPath.split(
'.').pop();
230 zip.file(`${material.id}_thumbnail.${thumbExt}`, thumbBlob);
232 console.error(
'Error downloading thumbnail:', error);
237 await Promise.all(texturePromises);
240 zip.file(
"README.txt",
241 `Material: ${material.name}\n` +
242 `Resolution: ${resolution}\n` +
244 `Downloaded: ${
new Date().toISOString()}\n\n` +
245 `Contains the following files:\n` +
246 `- ${material.id}.mtlx\n` +
247 Object.keys(textureFiles).map(path => `- ${path}`).join(
'\n') +
248 (material.thumb_url ? `\n- ${material.id}_thumbnail.png` :
'')
252 return await zip.generateAsync({ type:
'blob' });
255 console.error(
'Error creating MaterialX package:', error);
269 const mtlxData = filesData.mtlx?.[resolution]?.mtlx;
272 throw new Error(`No MaterialX files found
for ${resolution} resolution`);
280 textureFiles: mtlxData.include || {}
283 console.error(
'Error getting material content:', error);