scene.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import os
  2. from .. import constants, logger
  3. from . import (
  4. base_classes,
  5. texture,
  6. material,
  7. geometry,
  8. object,
  9. io,
  10. api
  11. )
  12. class Scene(base_classes.BaseScene):
  13. _defaults = {
  14. constants.METADATA: constants.DEFAULT_METADATA.copy(),
  15. constants.GEOMETRIES: [],
  16. constants.MATERIALS: [],
  17. constants.IMAGES: [],
  18. constants.TEXTURES: []
  19. }
  20. def __init__(self, filepath, options=None):
  21. logger.debug('Scene().__init__(%s, %s)', filepath, options)
  22. base_classes.BaseScene.__init__(self, filepath, options or {})
  23. source_file = api.scene_name()
  24. if source_file:
  25. self[constants.METADATA][constants.SOURCE_FILE] = source_file
  26. @property
  27. def valid_types(self):
  28. valid_types = [api.constants.MESH]
  29. if self.options.get(constants.CAMERAS):
  30. logger.info('Adding cameras to valid object types')
  31. valid_types.append(api.constants.CAMERA)
  32. if self.options.get(constants.LIGHTS):
  33. logger.info('Adding lights to valid object types')
  34. valid_types.append(api.constants.LAMP)
  35. return valid_types
  36. def geometry(self, arg):
  37. logger.debug('Scene().geometry(%s)', arg)
  38. return self._find_node(arg, self[constants.GEOMETRIES])
  39. def image(self, arg):
  40. logger.debug('Scene().image%s)', arg)
  41. return self._find_node(arg, self[constants.IMAGES])
  42. def material(self, arg):
  43. logger.debug('Scene().material(%s)', arg)
  44. return self._find_node(arg, self[constants.MATERIALS])
  45. def parse(self):
  46. logger.debug('Scene().parse()')
  47. if self.options.get(constants.MAPS):
  48. self.__parse_textures()
  49. if self.options.get(constants.MATERIALS):
  50. self.__parse_materials()
  51. self.__parse_geometries()
  52. self.__parse_objects()
  53. def texture(self, arg):
  54. logger.debug('Scene().texture(%s)', arg)
  55. return self._find_node(arg, self[constants.TEXTURES])
  56. def write(self):
  57. logger.debug('Scene().write()')
  58. data = {}
  59. embed_anim = self.options.get(constants.EMBED_ANIMATION, True)
  60. embed = self.options[constants.EMBED_GEOMETRY]
  61. compression = self.options.get(constants.COMPRESSION)
  62. extension = constants.EXTENSIONS.get(compression,
  63. constants.EXTENSIONS[constants.JSON])
  64. export_dir = os.path.dirname(self.filepath)
  65. for key, value in self.items():
  66. if key == constants.GEOMETRIES:
  67. geometries = []
  68. for geometry in value:
  69. if not embed_anim:
  70. geometry.write_animation(export_dir)
  71. if embed:
  72. for each in value:
  73. geometries.append(each.copy())
  74. continue
  75. geom_data = geometry.copy()
  76. geo_type = geom_data[constants.TYPE].lower()
  77. if geo_type == constants.GEOMETRY.lower():
  78. geom_data.pop(constants.DATA)
  79. elif geo_type == constants.BUFFER_GEOMETRY.lower():
  80. geom_data.pop(constants.ATTRIBUTES)
  81. geom_data.pop(constants.METADATA)
  82. url = 'geometry.%s%s' % (geometry.node, extension)
  83. geometry_file = os.path.join(export_dir, url)
  84. geometry.write(filepath=geometry_file)
  85. geom_data[constants.URL] = os.path.basename(url)
  86. geometries.append(geom_data)
  87. data[key] = geometries
  88. elif isinstance(value, list):
  89. data[key] = []
  90. for each in value:
  91. data[key].append(each.copy())
  92. elif isinstance(value, dict):
  93. data[key] = value.copy()
  94. io.dump(self.filepath, data, options=self.options)
  95. if self.options.get(constants.COPY_TEXTURES):
  96. for geo in self[constants.GEOMETRIES]:
  97. logger.info('Copying textures from %s', geo.node)
  98. geo.copy_textures()
  99. def _find_node(self, arg, manifest):
  100. for index in manifest:
  101. uuid = index.get(constants.UUID) == arg
  102. name = index.node == arg
  103. if uuid or name:
  104. return index
  105. else:
  106. logger.debug('No matching node for %s', arg)
  107. def __parse_geometries(self):
  108. logger.debug('Scene().__parse_geometries()')
  109. # this is an important step. please refer to the doc string
  110. # on the function for more information
  111. api.object.prep_meshes(self.options)
  112. geometries = []
  113. # now iterate over all the extracted mesh nodes and parse each one
  114. for mesh in api.object.extracted_meshes():
  115. logger.info('Parsing geometry %s', mesh)
  116. geo = geometry.Geometry(mesh, self)
  117. geo.parse()
  118. geometries.append(geo)
  119. logger.info('Added %d geometry nodes', len(geometries))
  120. self[constants.GEOMETRIES] = geometries
  121. def __parse_materials(self):
  122. logger.debug('Scene().__parse_materials()')
  123. materials = []
  124. for material_name in api.material.used_materials():
  125. logger.info('Parsing material %s', material_name)
  126. materials.append(material.Material(material_name, parent=self))
  127. logger.info('Added %d material nodes', len(materials))
  128. self[constants.MATERIALS] = materials
  129. def __parse_objects(self):
  130. logger.debug('Scene().__parse_objects()')
  131. self[constants.OBJECT] = object.Object(None, parent=self)
  132. self[constants.OBJECT][constants.TYPE] = constants.SCENE.title()
  133. objects = []
  134. for node in api.object.nodes(self.valid_types, self.options):
  135. logger.info('Parsing object %s', node)
  136. obj = object.Object(node, parent=self[constants.OBJECT])
  137. objects.append(obj)
  138. logger.info('Added %d object nodes', len(objects))
  139. self[constants.OBJECT][constants.CHILDREN] = objects
  140. def __parse_textures(self):
  141. logger.debug('Scene().__parse_textures()')
  142. textures = []
  143. for texture_name in api.texture.textures():
  144. logger.info('Parsing texture %s', texture_name)
  145. tex_inst = texture.Texture(texture_name, self)
  146. textures.append(tex_inst)
  147. logger.info('Added %d texture nodes', len(textures))
  148. self[constants.TEXTURES] = textures