MaterialXMaterials 1.39.5
Utilities for retrieving materials from remote servers
Loading...
Searching...
No Matches
gpuOpenFetch.js
1/*
2 * Sample script to fetch materials from the GPUOpen Material Library
3 * and download the first package.
4 * Usage:
5 * npm start -- <arguments>
6 * or
7 * node gpuOpenFetch.js <arguments>
8 */
9
10const fs = require('fs');
11const yargs = require('yargs/yargs');
12const { hideBin } = require('yargs/helpers');
13
14async function testFetch()
15/*
16 * Sample code
17 */
18{
19 const fetch = require('node-fetch');
20 async function fetchMaterials() {
21 const response = await fetch('https://api.matlib.gpuopen.com/api/materials/');
22 const json = await response.json();
23 console.log('Fetched:', json.results.length, 'materials');
24 }
25
26 fetchMaterials().catch(error => console.error('Error:', error));
27}
28
29// Create an loader instance
30const materialLoader = require('../JsGPUOpenLoader');
31
32// Get materials
33async function getMaterialInformation(batchSize=50, fileName="gpuOpenMaterials.json") {
34 try {
35 // Get all materials
36 const materials = await materialLoader.getMaterials(batchSize);
37 const materialNames = materialLoader.getMaterialNames()
38 console.log('Fetched materials:', materialNames.length);
39
40 if (fileName.length == 0) {
41 return;
42 }
43 // Save each JSON object in materials to disk
44 fs.writeFileSync(fileName, JSON.stringify(materials, null, 2));
45 console.log('Wrote material information to:', fileName);
46 let materialsNamesFile = fileName.replace('.json', '_names.json');
47 fs.writeFileSync(materialsNamesFile, JSON.stringify(materialNames, null, 2));
48 console.log('Wrote material names to:', materialsNamesFile);
49
50 if (materialNames.length === 0) {
51 console.error('No materials found');
52 return;
53 }
54 } catch (error) {
55 console.error('Error fetching materials:', error);
56 }
57}
58
59// Download a material from list
60async function downloadMaterial(listNumber=0, materialNumber=0, packageId = 0) {
61 try {
62 await getMaterialInformation(100, "");
63 let [data, title] = await materialLoader.downloadPackage(listNumber, materialNumber, packageId)
64 if (!data) {
65 console.error('Error downloading material:', title);
66 return;
67 }
68
69 let filename = title.replace(/[^a-z0-9]/gi, '_') + '.zip';
70 fs.writeFileSync(filename, Buffer.from(data));
71 console.log(`Wrote material ${title} package (${data.byteLength} bytes) to: ${filename}`);
72 return ;
73 } catch (error) {
74 console.error('Error fetching materials:', error);
75 }
76}
77
78// Download a material by expression
79async function downloadMaterialByExpression(expression = '', packageIndex = 0) {
80 if (expression.length === 0) {
81 console.error('No material expression provided');
82 return;
83 }
84
85 try {
86 await getMaterialInformation(100, "");
87 let dataItems = await materialLoader.downloadPackageByExpression(expression, packageIndex)
88 if (!dataItems) {
89 console.error('Error downloading material:', expression);
90 return;
91 }
92
93 for (const dataItem of dataItems)
94 {
95 const [data, title] = dataItem;
96 let filename = title.replace(/[^a-z0-9]/gi, '_') + '.zip';
97 fs.writeFileSync(filename, Buffer.from(data));
98 console.log(`Wrote material ${title} package (${data.byteLength} bytes) to: ${filename}`);
99 }
100 return ;
101 } catch (error) {
102 console.error('Error fetching materials:', error);
103 }
104}
105
106const argv = yargs(hideBin(process.argv))
107 .option('materialName', {
108 alias: 'n',
109 type: 'string',
110 description: 'Name of the material to fetch',
111 default: ''
112 })
113 .option('batchSize', {
114 alias: 'b',
115 type: 'number',
116 description: 'Batch size for fetching materials',
117 default: 50
118 })
119 .option('materialList', {
120 alias: 'l',
121 type: 'number',
122 description: 'Index of the material list',
123 default: 0
124 })
125 .option('materialIndex', {
126 alias: 'i',
127 type: 'number',
128 description: 'Index of the material in the list',
129 default: 0
130 })
131 .option('packageIndex', {
132 alias: 'p',
133 type: 'number',
134 description: 'Index of the package to download',
135 default: 0
136 })
137 .option('getInfo', {
138 alias: 'g',
139 type: 'boolean',
140 description: 'Flag to call getMaterialInformation',
141 default: true
142 })
143 .option('outputFilename', {
144 alias: 'o',
145 type: 'string',
146 description: 'Filename to save the fetched materials',
147 default: 'gpuOpenMaterials.json'
148 })
149 .help()
150 .argv;
151
152//console.log(argv)
153
154// Check if we are fetching material information or downloading a package
155let materialName = argv.materialName
156console.log('Material name:', argv.materialName)
157if (argv.materialName.length > 0) {
158 console.log('------------- Look for material:', argv.materialName);
159 downloadMaterialByExpression(argv.materialName, argv.packageIndex);
160}
161else {
162 if (argv.getInfo)
163 {
164 console.log('-- Fetching material information --');
165 getMaterialInformation(argv.batchSize, argv.outputFilename);
166 }
167 else
168 {
169 console.log('-- Fetching material --');
170 downloadMaterial(argv.materialList, argv.materialIndex, argv.packageIndex);
171 }
172}