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
19import os, sys, argparse, logging
22import physicallyBasedMaterialX
as pbmx
24def physicallBasedMaterialXCmd():
25 logger = logging.getLogger(
'PB_CMD')
26 logging.basicConfig(level=logging.INFO)
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()
42 outputDir =
'PhysicallyBasedMaterialX'
44 if not os.path.exists(opts.outputDir):
45 logger.info(f
'Error: Output directory does not exist: {opts.outputDir}')
48 outputDir = opts.outputDir
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']
59 for shadingModel
in shadingModels:
60 shadingModelPrefixes.append(shadingModePrefixMap[shadingModel])
62 writeJSON = opts.writeJSON
63 separateFiles = opts.separateFiles
67 loader = pbmx.PhysicallyBasedMaterialLoader(mx,
None)
69 if not os.path.exists(opts.loadFromFile):
70 logger.info(f
'> Error: File does not exist: {opts.loadFromFile}')
72 logger.info(f
'> Load materials from file: {opts.loadFromFile}')
73 jsonMat = loader.loadMaterialsFromFile(opts.loadFromFile)
75 jsonMat = loader.getMaterialsFromURL()
80 os.makedirs(outputDir, exist_ok=
True)
83 logger.info(f
'> Write: {outputDir}/PhysicallyBasedMaterial.json')
84 loader.writeJSONToFile(os.path.join(outputDir,
'PhysicallyBasedMaterial.json'))
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)
92 fileName = os.path.join(outputDir, f
'PhysicallyBasedMaterialX_{prefix}.mtlx')
93 loader.writeMaterialXToFile(fileName)
94 logger.info(f
'> Write: {fileName}')
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)
105 fileName = os.path.join(outputDir, f
'PB_{prefix}_{mat}.mtlx')
106 loader.writeMaterialXToFile(fileName)
107 logger.info(f
'> Write: {fileName}')
110 logger.info(
'Could not retrieve PhysicallyBased Materials')
112if __name__ ==
'__main__':
113 physicallBasedMaterialXCmd()