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