13const fs = require(
'fs');
14const yargs = require(
'yargs/yargs');
15const { hideBin } = require(
'yargs/helpers');
17async
function testFetch()
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');
29 fetchMaterials().catch(error => console.error(
'Error:', error));
33const materialLoader = require(
'../JsGPUOpenLoader');
36async
function getMaterialInformation(batchSize=50, fileName=
"gpuOpenMaterials.json") {
39 const materials = await materialLoader.getMaterials(batchSize);
40 const materialNames = materialLoader.getMaterialNames()
41 console.log(
'Fetched materials:', materialNames.length);
43 if (fileName.length == 0) {
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);
53 if (materialNames.length === 0) {
54 console.error(
'No materials found');
58 console.error(
'Error fetching materials:', error);
63async
function downloadMaterial(listNumber=0, materialNumber=0, packageId = 0) {
65 await getMaterialInformation(100,
"");
66 let [data, title] = await materialLoader.downloadPackage(listNumber, materialNumber, packageId)
68 console.error(
'Error. Could not find material for list,material,package: ', listNumber, materialNumber, packageId);
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}`);
77 console.error(
'Error fetching materials:', error);
82async
function downloadMaterialByExpression(expression =
'', packageIndex = 0) {
83 if (expression.length === 0) {
84 console.error(
'No material expression provided');
89 await getMaterialInformation(100,
"");
90 let dataItems = await materialLoader.downloadPackageByExpression(expression, packageIndex)
92 console.error(
'Error downloading material:', expression);
96 if (dataItems.length === 0) {
97 console.error(
'No materials found matching expression:', expression);
101 for (
const dataItem of dataItems)
103 const [data, title] = dataItem;
105 console.error(
'Error downloading material:', title);
109 console.error(
'Error downloading material: No title provided for expression:', expression);
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}`);
119 console.error(
'Error fetching materials:', error);
123const argv = yargs(hideBin(process.argv))
124 .option(
'materialName', {
127 description:
'Name of the material to fetch',
130 .option(
'batchSize', {
133 description:
'Batch size for fetching materials',
136 .option(
'materialList', {
139 description:
'Index of the material list',
142 .option(
'materialIndex', {
145 description:
'Index of the material in the list',
148 .option(
'packageIndex', {
151 description:
'Index of the package to download',
157 description:
'Flag to call getMaterialInformation',
160 .option(
'outputFilename', {
163 description:
'Filename to save the fetched materials',
164 default:
'gpuOpenMaterials.json'
169packageIndex = argv.packageIndex >= 0 ? argv.packageIndex : 0;
174 console.log(
'> Fetching material information --');
175 getMaterialInformation(argv.batchSize, argv.outputFilename);
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);
182else if (argv.materialIndex >= 0)
184 console.log(
'> Fetching material matching list index:', argv.materialList,
' material index:', argv.materialIndex,
' package index:', packageIndex);
185 downloadMaterial(argv.materialList, argv.materialIndex, packageIndex);
188 console.log(
'> No action specifieid to perform. Use --help for usage information.');