MaterialXMaterials 0.0.1
Utilities for retrieving materials from remote servers
Loading...
Searching...
No Matches
physicallyBasedMaterialXCmd.py
Go to the documentation of this file.
1#/usr/bin/env python3
2'''
3@file physicallyBasedMaterialXCmd.py
4@brief Convert Physically Based Materials to MaterialX Command Line Utility
5@details This script converts Physically Based Materials to MaterialX using the MaterialX Python API.
6@details The script can be run from the command line with the following options:
7@details --shadingModel: Shading models to use for conversion. If not specified then all will be used.
8@details Options: standard_surface, gltf_pbr, open_pbr_surface
9@details --outputDir: Output directory for MaterialX files
10@details --writeJSON: Write materials JSON file. Default is True
11@details --separateFiles: Convert individual MaterialX files per material. Default is false
12@details Example usage:
13@details - python physicallyBasedMaterialXCmd.py
14@details - python physicallyBasedMaterialXCmd.py --outputDir=myfolder
15@details - python physicallyBasedMaterialXCmd.py --writeJSON=False
16@details - python physicallyBasedMaterialXCmd.py --shadingModel=gltf_pbr,open_pbr_surface
17@details - python physicallyBasedMaterialXCmd.py --shadingModel=open_pbr_surface --separateFiles=True
18'''
19import os, sys, argparse, logging
20
21import MaterialX as mx # type: ignore
22import physicallyBasedMaterialX as pbmx
23
24def physicallBasedMaterialXCmd():
25 logger = logging.getLogger('PB_CMD')
26 logging.basicConfig(level=logging.INFO)
27
28 # TODO: Add arguments for shading model, and output directory using argparse
29 parser = argparse.ArgumentParser(description='Convert Physically Based Materials to MaterialX')
30 parser.add_argument('--shadingModel', type=str, default='', help='Shading models to use for conversion. '
31 ' If not specified then all will be used. '
32 ' Options: standard_surface, gltf_pbr, open_pbr_surface')
33 parser.add_argument('--outputDir', type=str, default='',
34 help='Output directory for MaterialX files. Default location is PhysicallyBasedMaterialX')
35 parser.add_argument('--writeJSON', type=bool, default=True,
36 help='Write materials JSON file. Default is True')
37 parser.add_argument('--separateFiles', type=bool, default=False,
38 help='Convert individual MaterialX files per material. Default is false')
39 parser.add_argument('--loadFromFile', type=str, default='', help='Load materials a specified file')
40 opts = parser.parse_args()
41
42 outputDir = 'PhysicallyBasedMaterialX'
43 if opts.outputDir:
44 if not os.path.exists(opts.outputDir):
45 logger.info(f'Error: Output directory does not exist: {opts.outputDir}')
46 sys.exit(1)
47 else:
48 outputDir = opts.outputDir
49
50 shadingModels = []
51 if opts.shadingModel:
52 shadingModels = opts.shadingModel.split(',')
53 shadingModePrefixMap = { 'standard_surface': 'SS', 'gltf_pbr': 'GLTF', 'open_pbr_surface': 'OPBR' }
54 shadingModelPrefixes = []
55 if len(shadingModels) == 0:
56 shadingModels = ['standard_surface', 'gltf_pbr', 'open_pbr_surface']
57 shadingModelPrefixes = ['SS', 'GLTF', 'OPBR']
58 else:
59 for shadingModel in shadingModels:
60 shadingModelPrefixes.append(shadingModePrefixMap[shadingModel])
61
62 writeJSON = opts.writeJSON
63 separateFiles = opts.separateFiles
64
65 # Create loader and get PhysicallyBasedMaterials
66 jsonMat = None
67 loader = pbmx.PhysicallyBasedMaterialLoader(mx, None)
68 if opts.loadFromFile:
69 if not os.path.exists(opts.loadFromFile):
70 logger.info(f'> Error: File does not exist: {opts.loadFromFile}')
71 sys.exit(1)
72 logger.info(f'> Load materials from file: {opts.loadFromFile}')
73 jsonMat = loader.loadMaterialsFromFile(opts.loadFromFile)
74 else:
75 jsonMat = loader.getMaterialsFromURL()
76
77 if jsonMat:
78
79 # Create folder for MaterialX call PhysicallyBasedMaterialX
80 os.makedirs(outputDir, exist_ok=True)
81
82 if writeJSON:
83 logger.info(f'> Write: {outputDir}/PhysicallyBasedMaterial.json')
84 loader.writeJSONToFile(os.path.join(outputDir, 'PhysicallyBasedMaterial.json'))
85
86 if not separateFiles:
87 for shadingModel, prefix in zip(shadingModels, shadingModelPrefixes):
88 logger.info(f'> Generate MaterialX for shading model: {shadingModel}')
89 matdoc = loader.convertToMaterialX([], shadingModel, {}, prefix)
90 valid, errors = loader.validateMaterialXDocument(matdoc)
91 if valid:
92 fileName = os.path.join(outputDir, f'PhysicallyBasedMaterialX_{prefix}.mtlx')
93 loader.writeMaterialXToFile(fileName)
94 logger.info(f'> Write: {fileName}')
95
96 else:
97 for shadingModel, prefix in zip(shadingModels, shadingModelPrefixes):
98 logger.info(f'> Generate MaterialX for shading model: {shadingModel}')
99 for mat in loader.getJSONMaterialNames():
100 materialFilter = [mat]
101 matdoc = loader.convertToMaterialX(materialFilter, shadingModel, {}, prefix)
102 if matdoc is not None:
103 valid, errors = loader.validateMaterialXDocument(matdoc)
104 if valid:
105 fileName = os.path.join(outputDir, f'PB_{prefix}_{mat}.mtlx')
106 loader.writeMaterialXToFile(fileName)
107 logger.info(f'> Write: {fileName}')
108
109 else:
110 logger.info('Could not retrieve PhysicallyBased Materials')
111
112if __name__ == '__main__':
113 physicallBasedMaterialXCmd()