atom_material.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # coding:utf-8
  2. #!/usr/bin/python
  3. #
  4. # Copyright (c) Contributors to the Open 3D Engine Project.
  5. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  6. #
  7. # SPDX-License-Identifier: Apache-2.0 OR MIT
  8. #
  9. # -- This line is 75 characters -------------------------------------------
  10. """Empty Doc String""" # To Do: add documentation
  11. # -------------------------------------------------------------------------
  12. # built-ins
  13. import os
  14. import sys
  15. #import simplejson as json
  16. import json
  17. # Lumberyard extensions
  18. from azpy.env_bool import env_bool
  19. from azpy.constants import ENVAR_DCCSI_GDEBUG
  20. from azpy.constants import ENVAR_DCCSI_DEV_MODE
  21. from azpy.constants import *
  22. # 3rdparty (we provide)
  23. from box import Box
  24. from pathlib import Path
  25. # -------------------------------------------------------------------------
  26. # -------------------------------------------------------------------------
  27. # set up global space, logging etc.
  28. _DCCSI_GDEBUG = env_bool(ENVAR_DCCSI_GDEBUG, False)
  29. _DCCSI_DEV_MODE = env_bool(ENVAR_DCCSI_DEV_MODE, False)
  30. _PACKAGENAME = __name__
  31. if _PACKAGENAME == '__main__':
  32. _PACKAGENAME = 'DCCsi.SDK.substance.builder.atom_material'
  33. import azpy
  34. _LOGGER = azpy.initialize_logger(_PACKAGENAME)
  35. _LOGGER.debug('Starting up: {0}.'.format({_PACKAGENAME}))
  36. # -------------------------------------------------------------------------
  37. # -------------------------------------------------------------------------
  38. # early attach WingIDE debugger (can refactor to include other IDEs later)
  39. if _DCCSI_DEV_MODE:
  40. from azpy.test.entry_test import connect_wing
  41. foo = connect_wing()
  42. # -------------------------------------------------------------------------
  43. # -------------------------------------------------------------------------
  44. # previous
  45. class AtomPBR:
  46. def __init__(self, material_file):
  47. # loading .material File
  48. self.material_file = material_file
  49. self.input_data = open(self.material_file, "r")
  50. self.material = json.load(self.input_data)
  51. self.mat_box = Box(self.material)
  52. self.input_data.close()
  53. # List of texture slots
  54. self.tex = ['baseColor', 'metallic', 'roughness', 'normalMap', 'opacity']
  55. # Construct texture maps
  56. self.basecolor_tex = ""
  57. self.metallic_tex = ""
  58. self.roughness_tex = ""
  59. self.normalmap_tex = ""
  60. self.opacity_tex = ""
  61. def load(self, material_file):
  62. input_data = open(material_file, "r")
  63. self.material = json.load(input_data)
  64. self.mat_box = Box(self.material)
  65. input_data.close()
  66. def get_map(self, tex_slot):
  67. return self.mat_box.properties[tex_slot].parameters.textureMap
  68. def set_map(self, tex_slot, tex_map):
  69. self.mat_box.properties[tex_slot].parameters.textureMap = tex_map
  70. def write(self, material_out):
  71. output_data = open(material_out, "w+")
  72. output_data.write(json.dumps(self.mat_box, indent=4))
  73. output_data.close()
  74. # -------------------------------------------------------------------------
  75. # -------------------------------------------------------------------------
  76. # new?
  77. class AtomMaterial:
  78. def __init__(self, material_file):
  79. # loading .material File
  80. self.material_file = material_file
  81. self.input_data = open(self.material_file, "r")
  82. self.material = json.load(self.input_data)
  83. self.mat_box = Box(self.material)
  84. self.input_data.close()
  85. # List of texture slots
  86. # old tex maps
  87. # self.tex = ['DiffuseMap', 'NormalMap', 'SpecularMap', 'EnvironmentMap']
  88. self.tex = ['baseColor', 'metallic', 'roughness', 'specularF0', 'normal', 'opacity']
  89. self.texture_map = {'baseColor': 'baseColor',
  90. 'metallic': 'metallic',
  91. 'roughness': 'roughness',
  92. 'specularF0': 'specular',
  93. 'normal': 'normal',
  94. 'opacity': 'opacity'
  95. }
  96. def load(self, material_file):
  97. input_data = open(material_file, "r")
  98. self.material = json.load(input_data)
  99. self.mat_box = Box(self.material)
  100. input_data.close()
  101. def get_material_type(self):
  102. return self.mat_box.materialType
  103. # old getMap function
  104. # def getMap(self, tex_slot):
  105. # return self.mat_box.properties.general[tex_slot]
  106. def get_map(self, tex_slot):
  107. return self.mat_box.properties[tex_slot].textureMap
  108. def set_map(self, tex_slot, tex_map):
  109. self.mat_box.properties[tex_slot].textureMap = tex_map
  110. self.mat_box.properties[tex_slot].useTexture = True
  111. self.mat_box.properties[tex_slot].factor = 1.0
  112. def write(self, material_out):
  113. if not material_out.parent.exists():
  114. try:
  115. material_out.parent.mkdir(mode=0o777, parents=True, exist_ok=True)
  116. _LOGGER.info('mkdir: {}'.format(material_out.parent))
  117. except Exception as e:
  118. _LOGGER.error(e)
  119. raise(e)
  120. else:
  121. _LOGGER.info('exists: {}'.format(material_out.parent))
  122. material_out.touch()
  123. output_data = open(str(material_out), "w+")
  124. output_data.write(json.dumps(self.mat_box, indent=4))
  125. output_data.close()
  126. return material_out
  127. # -------------------------------------------------------------------------
  128. ###########################################################################
  129. # Main Code Block, runs this script as main (testing)
  130. # -------------------------------------------------------------------------
  131. if __name__ == "__main__":
  132. """Run this file as main"""
  133. _LOGGER.info("Test Run:: {0}.".format({_PACKAGENAME}))
  134. _LOGGER.info("{0} :: if __name__ == '__main__':".format(_PACKAGENAME))
  135. material_path = Path(Path(__file__).parent.parent, 'resources', 'atom')
  136. # material_01 = AtomPBR("atom_pbr.material", "awesome.material")
  137. # material_01 = AtomPBR("atom_pbr.material")
  138. material_01 = AtomMaterial(Path(material_path, "StandardPBR_AllProperties.json"))
  139. # material_01.load("atom_pbr.material")
  140. # material_01.map(material_01.tex[2]).textureMap = "materials/substance/amazing_xzzzx.tif"
  141. # # print(material_01.metallic)
  142. # material_01.write("awesome.material")
  143. # print(material_01.tex[2])
  144. # print(material_01.getMap(material_01.tex[3]))
  145. # material_01.baesColor_tex = "materials/substance/amazing_bc.tif"
  146. # material_01.setMap(material_01.tex[0], material_01.baesColor_tex)
  147. # material_01.write("awesome.material")
  148. # Test material parser for the new format.
  149. material_01.baseColor_tex = "Textures/Streaming/streaming99.dds"
  150. material_01.metallic_tex = "Textures/Streaming/streaming99.dds"
  151. material_01.set_map(material_01.tex[0], material_01.baseColor_tex)
  152. material_01.set_map(material_01.tex[1], material_01.metallic_tex)
  153. material_out = material_01.write(Path(material_path, "atom_variant00.material"))
  154. _LOGGER.info('materialType is:: {}'.format(material_01.get_material_type()))
  155. if material_out.exists():
  156. _LOGGER.info('Wrote material file: {}'.format(material_out))
  157. # remove the logger
  158. del _LOGGER
  159. # ---- END ---------------------------------------------------------------