10const fs = require(
'fs');
11const yargs = require(
'yargs/yargs');
12const { hideBin } = require(
'yargs/helpers');
14async
function testFetch()
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');
26 fetchMaterials().catch(error => console.error(
'Error:', error));
30const materialLoader = require(
'../JsGPUOpenLoader');
33async
function getMaterialInformation(batchSize=50, fileName=
"gpuOpenMaterials.json") {
36 const materials = await materialLoader.getMaterials(batchSize);
37 const materialNames = materialLoader.getMaterialNames()
38 console.log(
'Fetched materials:', materialNames.length);
40 if (fileName.length == 0) {
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);
50 if (materialNames.length === 0) {
51 console.error(
'No materials found');
55 console.error(
'Error fetching materials:', error);
60async
function downloadMaterial(listNumber=0, materialNumber=0, packageId = 0) {
62 await getMaterialInformation(100,
"");
63 let [data, title] = await materialLoader.downloadPackage(listNumber, materialNumber, packageId)
65 console.error(
'Error downloading material:', title);
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}`);
74 console.error(
'Error fetching materials:', error);
79async
function downloadMaterialByExpression(expression =
'', packageIndex = 0) {
80 if (expression.length === 0) {
81 console.error(
'No material expression provided');
86 await getMaterialInformation(100,
"");
87 let dataItems = await materialLoader.downloadPackageByExpression(expression, packageIndex)
89 console.error(
'Error downloading material:', expression);
93 for (
const dataItem of dataItems)
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}`);
102 console.error(
'Error fetching materials:', error);
106const argv = yargs(hideBin(process.argv))
107 .option(
'materialName', {
110 description:
'Name of the material to fetch',
113 .option(
'batchSize', {
116 description:
'Batch size for fetching materials',
119 .option(
'materialList', {
122 description:
'Index of the material list',
125 .option(
'materialIndex', {
128 description:
'Index of the material in the list',
131 .option(
'packageIndex', {
134 description:
'Index of the package to download',
140 description:
'Flag to call getMaterialInformation',
143 .option(
'outputFilename', {
146 description:
'Filename to save the fetched materials',
147 default:
'gpuOpenMaterials.json'
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);
164 console.log(
'-- Fetching material information --');
165 getMaterialInformation(argv.batchSize, argv.outputFilename);
169 console.log(
'-- Fetching material --');
170 downloadMaterial(argv.materialList, argv.materialIndex, argv.packageIndex);