3import os, argparse, sys, logging
4import GPUOpenLoader
as gpuo
8 Utility to download and write GPUOpen materials
10 logger = logging.getLogger(
'GPUO_CMD')
11 logging.basicConfig(level=logging.INFO)
13 parser = argparse.ArgumentParser(description=
'Get materials information and material package from AMD GPUOpen.'
14 ' By default the materials will be downloaded and a named and index material'
15 ' package will be extracted.')
16 parser.add_argument(
'--materialNames', type=bool, default=
None,
17 help=
'Return material names. Default is False')
18 parser.add_argument(
'--loadMaterials', type=str, default=
'',
19 help=
'Folder to load materials from. All JSON files with post-fix _#.json are loaded. Default is false'
20 ' meaning to download materials')
21 parser.add_argument(
'--saveMaterials', type=bool, default=
None,
22 help=
'Save material lists. Default is None.'
23 ' Has no effect if --loadMaterials is set')
24 parser.add_argument(
'--extractExpression', type=str, default=
'Oliana Blue Painted Wood',
25 help=
'Extract out a package for a materials which match a given expression. Default is a sample material'
27 parser.add_argument(
'--extractIndices', type=str, default=
'',
28 help=
'Extract out a package for a materials which match a given material list, material index, and package index.'
29 ' Default is empty. Format is: <materialList>,<materialIndex>,<materialPackage>')
30 parser.add_argument(
'--output', type=str, default=
'',
31 help=
'Output folder for data files. Default location is GPUOpenMaterialX.')
32 parser.add_argument(
'--unzip', type=bool, default=
None,
33 help=
'Unzip the downloaded package. Default is None.')
34 opts = parser.parse_args()
36 loader = gpuo.GPUOpenMaterialLoader()
39 if opts.loadMaterials:
40 filePaths = loader.getMaterialFileNames(opts.loadMaterials)
41 if len(filePaths) == 0:
42 logger.error(f
'Error: No files found in folder: {opts.loadMaterials}')
45 logger.info(f
'> Load materials from files: {filePaths}')
46 materials = loader.readMaterialFiles(filePaths)
49 logger.info(f
'> Download materials from GPUOpen')
50 materials = loader.getMaterials()
52 outputFolder =
'GPUOpenMaterialX'
54 if not os.path.exists(opts.output):
55 logger.error(f
'Error: Output directory does not exist: {opts.output}')
58 outputFolder = opts.output
60 materialNames = loader.getMaterialNames()
61 materialCount = len(materialNames)
62 logger.info(f
'Available number of materials: {materialCount}')
63 if opts.saveMaterials:
64 loader.writeMaterialFiles(outputFolder,
'GPUOpenMaterialX')
68 if opts.extractExpression:
69 searchExpr = opts.extractExpression
70 if len(searchExpr) == 0:
71 logger.info(f
'> No search expression given.')
73 dataItems = loader.downloadPackageByExpression(searchExpr, 0)
74 toMB = 1.0 / (1024.0 * 1024.0)
75 unzipFile = opts.unzip
if opts.unzip
else False
76 for dataItem
in dataItems:
79 logger.info(f
'Write package data to file: {title}, Data size: {len(data)*toMB:.2f} MB')
80 loader.writePackageDataToFile(data, outputFolder, title, unzipFile=unzipFile)
82 extractIndices = opts.extractIndices
83 if len(extractIndices) > 0:
84 indices = extractIndices.split(
',')
86 logger.error(f
'Error: Invalid indices given: {extractIndices}')
89 materialList = int(indices[0])
90 materialIndex = int(indices[1])
91 materialPackage = int(indices[2])
92 [data, title] = loader.downloadPackage(materialList, materialIndex, materialPackage)
93 logger.info(f
'> Download material: {title} List: {materialList}. Index: {materialIndex}. Package: {materialPackage}')
95 loader.writePackageDataToFile(data, outputFolder, title)
97 if opts.materialNames:
98 materialNamesFile = os.path.join(outputFolder,
'GPUOpenMaterialX_Names.json')
99 logger.info(f
'> Save materials names to file: {materialNamesFile}')
101 loader.writeMaterialNamesToFile(materialNamesFile, sorted)
103if __name__ ==
'__main__':