MaterialXMaterials 0.0.1
Utilities for retrieving materials from remote servers
Loading...
Searching...
No Matches
JsGPUOpenMaterialLoader Class Reference

Class to download MaterialX materials from the GPUOpen material database. More...

Public Member Functions

 constructor ()
 Constructor for the JsGPUOpenMaterialLoader class.
 
 getMaterialList ()
 Return downloaded material list.
 
 getMaterialNames ()
 Return downloaded material names.
 
async getMaterials (batchSize=50)
 Get lists of materials from the GPUOpen material database.
 
async downloadPackage (listNumber, materialNumber, packageId=0)
 Download a material package from the GPUOpen material database.
 
 findMaterialsByName (materialName)
 Find materials by name.
 
async downloadPackageByExpression (searchExpr, packageId=0)
 Download a material package by string expression.
 

Detailed Description

Class to download MaterialX materials from the GPUOpen material database.

The class provides methods to fetch materials and download material packages. The class uses the fetch API to make HTTP requests. The class is intended to be used in a Node.js environment.

Definition at line 8 of file JsGPUOpenLoader.js.

Member Function Documentation

◆ constructor()

JsGPUOpenMaterialLoader::constructor ( )

Constructor for the JsGPUOpenMaterialLoader class.

Definition at line 12 of file JsGPUOpenLoader.js.

12 {
13 if (JsGPUOpenMaterialLoader.instance) {
14 return JsGPUOpenMaterialLoader.instance;
15 }
16
17 this.rootUrl = 'https://api.matlib.gpuopen.com/api';
18 this.url = `${this.rootUrl}/materials`;
19 this.packageUrl = `${this.rootUrl}/packages`;
20 this.materials = null;
21 this.materialNames = null;
22
23 this.logger = console;
24
25 // Cache the instance
26 JsGPUOpenMaterialLoader.instance = this;
27 }
Class to download MaterialX materials from the GPUOpen material database.

◆ downloadPackage()

async JsGPUOpenMaterialLoader::downloadPackage ( listNumber,
materialNumber,
packageId = 0 )

Download a material package from the GPUOpen material database.

Parameters
{number}listNumber - Index of the material list
{number}materialNumber - Index of the material in the list
{number}packageId - Index of the package in the material
Returns
{Array} - A list containing the package data and title

Definition at line 127 of file JsGPUOpenLoader.js.

127 {
128 if (this.materials === null || this.materials.length === 0) {
129 return [null, null];
130 }
131
132 const jsonData = this.materials[listNumber];
133 if (!jsonData) {
134 return [null, null];
135 }
136
137 let jsonResults = null;
138 let jsonResult = null;
139 if ('results' in jsonData) {
140 jsonResults = jsonData['results'];
141 if (jsonResults.length <= materialNumber) {
142 return [null, null];
143 } else {
144 jsonResult = jsonResults[materialNumber];
145 }
146 }
147
148 if (!jsonResult) {
149 return [null, null];
150 }
151
152 let jsonPackages = null;
153 if ('packages' in jsonResult) {
154 jsonPackages = jsonResult['packages'];
155 }
156 if (!jsonPackages) {
157 return [null, null];
158 }
159
160 if (jsonPackages.length <= packageId) {
161 return [null, null];
162 }
163 const packageIdValue = jsonPackages[packageId];
164
165 if (!packageIdValue) {
166 return [null, null];
167 }
168
169 const url = `${this.packageUrl}/${packageIdValue}/download`;
170 const response = await fetch(url);
171 const data = await response.arrayBuffer();
172 const title = jsonResult['title'];
173
174 console.log(`- Loader: Downloaded package: ${title} from ${url}`);
175 return [data, title];
176 }

◆ downloadPackageByExpression()

async JsGPUOpenMaterialLoader::downloadPackageByExpression ( searchExpr,
packageId = 0 )

Download a material package by string expression.

Parameters
{string}searchExpr - Regular expression to match the material name
{number}packageId - Index of the package in the material
Returns
{Array} - A list of material items that match the regular expression of the form: [[data, title], [data, title], ...] where data is the package data (in zip form) and title is the material title

Definition at line 226 of file JsGPUOpenLoader.js.

226 {
227 const downloadList = [];
228
229 const foundList = this.findMaterialsByName(searchExpr);
230 if (foundList.length > 0) {
231 for (const found of foundList) {
232 const listNumber = found['listNumber'];
233 const materialNumber = found['materialNumber'];
234 const matName = found['title'];
235 this.logger.info(`> Download material: ${matName} List: ${listNumber}. Index: ${materialNumber}`);
236 const [data, title] = await this.downloadPackage(listNumber, materialNumber, packageId);
237 downloadList.push([data, title]);
238 }
239 }
240 return downloadList;
241 }
async downloadPackage(listNumber, materialNumber, packageId=0)
Download a material package from the GPUOpen material database.
findMaterialsByName(materialName)
Find materials by name.

◆ findMaterialsByName()

JsGPUOpenMaterialLoader::findMaterialsByName ( materialName)

Find materials by name.

Parameters
{string}materialName - Regular expression to match the material name.
Returns
{Array} - A list of materials that match the regular expression of the form: [{ 'listNumber': listNumber, 'materialNumber': materialNumber, 'title': title }]

Definition at line 184 of file JsGPUOpenLoader.js.

185 {
186 if (!this.materials) {
187 return [];
188 }
189
190 const materialsList = [];
191 let listNumber = 0;
192
193 // Create a RegExp object for case-insensitive matching
194 const regex = new RegExp(materialName, 'i');
195
196 this.materials.forEach((materialList, materialIndex) => {
197 let materialNumber = 0;
198
199 materialList['results'].forEach((material) => {
200
201 //console.log('testing material:', material['title'], ' with regex:', regex)
202 if (regex.test(material['title'])) {
203 materialsList.push({
204 listNumber: listNumber,
205 materialNumber: materialNumber,
206 title: material['title'],
207 });
208 }
209 materialNumber += 1;
210 });
211
212 listNumber += 1;
213 });
214
215 return materialsList;
216 }

◆ getMaterialList()

JsGPUOpenMaterialLoader::getMaterialList ( )

Return downloaded material list.

Returns
{Array} - List of materials

Definition at line 33 of file JsGPUOpenLoader.js.

33 {
34 return this.materials;
35 }

◆ getMaterialNames()

JsGPUOpenMaterialLoader::getMaterialNames ( )

Return downloaded material names.

Returns
{Array} - List of material names

Definition at line 41 of file JsGPUOpenLoader.js.

41 {
42 return this.materialNames;
43 }

◆ getMaterials()

async JsGPUOpenMaterialLoader::getMaterials ( batchSize = 50)

Get lists of materials from the GPUOpen material database.

Parameters
{number}batchSize - Number of materials to fetch per batch
Returns
{Array} - List of material lists

Definition at line 50 of file JsGPUOpenLoader.js.

50 {
51
52 /*
53 * Get the materials returned from the GPUOpen material database.
54 * Will loop based on the linked-list of materials stored in the database.
55 * Currently, the batch size requested is 100 materials per batch.
56 * @param batchSize: Number of materials to fetch per batch
57 * @return: List of material lists
58 */
59
60 this.materials = [];
61 this.materialNames = [];
62
63 let url = this.url;
64
65 // Get batches of materials. Start with the first N.
66 let params = new URLSearchParams({
67 limit: batchSize,
68 offset: 0
69 });
70
71 // Append & parameters to url using params
72 if (params) {
73 url += '?' + params.toString();
74 }
75
76 let haveMoreMaterials = true;
77 while (haveMoreMaterials)
78 {
79 try {
80
81 console.log('Fetch materials from url:', url)
82
83 const response = await fetch(url);
84
85 if (!response.ok) {
86 throw new Error(`HTTP error! status: ${response.status}`);
87 }
88
89 const jsonData = await response.json();
90 this.materials.push(jsonData)
91
92 for (const material of jsonData.results) {
93 this.materialNames.push(material['title']);
94 }
95
96 //console.log("Number of materials fetched:", jsonData.results.length)
97
98 let nextURL = jsonData.next
99 if (nextURL) {
100 //console.log('Next URL: ', jsonData.next)
101 url = nextURL;
102 haveMoreMaterials = true
103 }
104 else
105 {
106 console.log('Finished fetching materials')
107 haveMoreMaterials = false;
108 break;
109 }
110
111 } catch (error) {
112 this.logger.info(`Error: ${error.message}`);
113 haveMoreMaterials = true;
114 }
115 }
116
117 return this.materials;
118 }

The documentation for this class was generated from the following file: