MaterialXMaterials 0.0.1
Utilities for retrieving materials from remote servers
Loading...
Searching...
No Matches
ambientCGLoaderCmd.py
1#/usr/bin/env python3
2
3import os, argparse, sys, logging
4import ambientCGLoader as acg
5import MaterialX as mx
6
7def ambientCgLoaderCmd():
8 '''
9 Utility to download and write ambientCG materials
10 '''
11 logger = logging.getLogger('ACG_CMD')
12 logging.basicConfig(level=logging.INFO)
13
14 parser = argparse.ArgumentParser(description='Get materials information and material package from ambientCG.'
15 ' By default the materials will be downloaded and a named and index material'
16 ' package will be extracted.')
17
18 parser.add_argument('--loadMaterials', type=str, default='',
19 help='Load JSON file containing list of materials that can be downloaded.')
20 parser.add_argument('--downloadMaterials', type=bool, default=None,
21 help='Download JSON file containing list of materials that can be downloaded.'
22 ' Has no effect if --loadMaterials is set')
23
24 # Material names query
25 parser.add_argument('--materialNames', type=bool, default=None,
26 help='Return material names. Default is False')
27
28 # Save download information for all materials or a specific one
29 # based on asset identifier
30 parser.add_argument('--saveMaterials', type=bool, default=None,
31 help='Save material lists. Default is None.'
32 ' Has no effect if --loadMaterials is set')
33 parser.add_argument('--saveMaterial', type=str, default='',
34 help='Save material download information in JSON format')
35
36 # Asset download options
37 parser.add_argument('--downloadMaterial', type=str, default='',
38 help='Download zip package for a materials which match a given string. Default is a sample material')
39 parser.add_argument('--downloadmageFormat', type=str, default='PNG',
40 help='Download image format. Valid values include PNG and JPEG. Default is PNG')
41 parser.add_argument('--downloadResolution', type=str, default='1',
42 help='Download image resulution. Valid values include 1,2,4,8 to indicate 1K to 8K.')
43
44 # Download full database iformation for material assets
45 parser.add_argument('--downloadDatabase', type=bool, default=None,
46 help='Download information database')
47 parser.add_argument('--saveDatabase', type=str, default='ambientCG_database.json',
48 help='Save information database')
49
50 # Output options
51 parser.add_argument('--output', type=str, default='',
52 help='Output folder for data files. Default location is the current execution folder.')
53 opts = parser.parse_args()
54
55 loader = acg.AmbientCGLoader(mx, None)
56
57 # Set output folder. Default is current folder
58 outputFolder = '.'
59 if opts.output:
60 outputFolder = opts.output
61 if not os.path.exists(outputFolder):
62 logger.error(f'Output directory does not exist: {outputFolder}')
63 sys.exit(1)
64
65 # Get materials list which contains download information
66 loadMaterials = opts.loadMaterials
67 downloadMaterials = opts.downloadMaterials or opts.saveMaterials
68 if loadMaterials or downloadMaterials:
69 materialsList = None
70 if len(loadMaterials) > 0:
71 materialsList = loader.loadMaterialsList(loadMaterials)
72 elif downloadMaterials:
73 materialsList = loader.downloadMaterialsList()
74 # Save materials list if specified. Only do so for download case
75 if opts.saveMaterials:
76 loader.writeMaterialList(materialsList, os.path.join(outputFolder,'ambientCG_materialsList.json'))
77
78 # Check if the list of materials is asked to be returned
79 if opts.materialNames:
80 materialNames = loader.getMaterialNames()
81 print(f'{materialNames}')
82
83 # Check if a material asset is specified to downloaded
84 materialName = opts.downloadMaterial
85 if len(materialName) > 0:
86 result = loader.findMaterial(materialName)
87 if result:
88 fileName = loader.downloadMaterialAsset(materialName) #, opts.downloadmageFormat, opts.downloadResolution)
89 if len(fileName) > 0:
90 loader.writeDownloadedMaterialToFile(outputFolder)
91 else:
92 print(f'Material not found: {materialName}')
93
94 # Check if material database is specified for download
95 databaseFileName = opts.saveDatabase
96 haveDatabaseFileName = len(databaseFileName) > 0
97 downloadDatabase = opts.downloadDatabase and haveDatabaseFileName
98 if downloadDatabase:
99 print('download database')
100 loader.downloadAssetDatabase()
101 if haveDatabaseFileName:
102 path = os.path.join(outputFolder, databaseFileName)
103 loader.writeDatabaseToFile(path)
104
105if __name__ == '__main__':
106 ambientCgLoaderCmd()