diff options
author | Julien Duroure <julien.duroure@gmail.com> | 2018-12-11 23:51:40 +0300 |
---|---|---|
committer | Julien Duroure <julien.duroure@gmail.com> | 2018-12-11 23:51:40 +0300 |
commit | 5aa12449c934b4ae2d429586be118bc79a17752a (patch) | |
tree | d5df27b57de29d3ccf524d2526fa0eb26e1b81c9 /io_scene_gltf2/blender | |
parent | ed2c64455a78bcc43a0fefbb6cd3a3e0220c5168 (diff) |
glTF Exporter: optimizations & fixes
* image optimizations
* options refactoring
* Fix T59047
Diffstat (limited to 'io_scene_gltf2/blender')
4 files changed, 38 insertions, 15 deletions
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_export.py b/io_scene_gltf2/blender/exp/gltf2_blender_export.py index f2cc9fa5..1ddeb6a5 100755 --- a/io_scene_gltf2/blender/exp/gltf2_blender_export.py +++ b/io_scene_gltf2/blender/exp/gltf2_blender_export.py @@ -33,6 +33,7 @@ def save(context, export_settings): def __export(export_settings): + export_settings['gltf_channelcache'] = dict() exporter = GlTF2Exporter(__get_copyright(export_settings)) __add_root_objects(exporter, export_settings) buffer = __create_buffer(exporter, export_settings) diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py b/io_scene_gltf2/blender/exp/gltf2_blender_extract.py index c26429d3..8762a90f 100755 --- a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py +++ b/io_scene_gltf2/blender/exp/gltf2_blender_extract.py @@ -975,10 +975,10 @@ def extract_primitives(glTF, blender_mesh, blender_vertex_groups, modifiers, exp if max_index >= range_indices: # - # Spliting result_primitives. + # Splitting result_primitives. # - # At start, all indicees are pending. + # At start, all indices are pending. pending_attributes = { POSITION_ATTRIBUTE: [], NORMAL_ATTRIBUTE: [] @@ -1038,6 +1038,7 @@ def extract_primitives(glTF, blender_mesh, blender_vertex_groups, modifiers, exp while len(pending_indices) > 0: process_indices = pending_primitive[INDICES_ID] + max_index = max(process_indices) pending_indices = [] @@ -1046,7 +1047,7 @@ def extract_primitives(glTF, blender_mesh, blender_vertex_groups, modifiers, exp all_local_indices = [] - for i in range(0, (max(process_indices) // range_indices) + 1): + for i in range(0, (max_index // range_indices) + 1): all_local_indices.append([]) # @@ -1063,7 +1064,7 @@ def extract_primitives(glTF, blender_mesh, blender_vertex_groups, modifiers, exp process_indices[face_index + 2]) # ... check if it can be but in a range of maximum indices. - for i in range(0, (max(process_indices) // range_indices) + 1): + for i in range(0, (max_index // range_indices) + 1): offset = i * range_indices # Yes, so store the primitive with its indices. @@ -1075,7 +1076,7 @@ def extract_primitives(glTF, blender_mesh, blender_vertex_groups, modifiers, exp written = True break - # If not written, the triangel face has indices from different ranges. + # If not written, the triangle face has indices from different ranges. if not written: pending_indices.extend([process_indices[face_index + 0], process_indices[face_index + 1], process_indices[face_index + 2]]) diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py index d9583d08..b6131a59 100755 --- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py +++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_image.py @@ -49,7 +49,7 @@ def __filter_image(sockets_or_slots, export_settings): def __gather_buffer_view(sockets_or_slots, export_settings): if export_settings[gltf2_blender_export_keys.FORMAT] != 'GLTF_SEPARATE': - image = __get_image_data(sockets_or_slots) + image = __get_image_data(sockets_or_slots, export_settings) return gltf2_io_binary_data.BinaryData( data=image.to_image_data(__gather_mime_type(sockets_or_slots, export_settings))) return None @@ -81,7 +81,7 @@ def __gather_name(sockets_or_slots, export_settings): def __gather_uri(sockets_or_slots, export_settings): if export_settings[gltf2_blender_export_keys.FORMAT] == 'GLTF_SEPARATE': # as usual we just store the data in place instead of already resolving the references - return __get_image_data(sockets_or_slots) + return __get_image_data(sockets_or_slots, export_settings) return None @@ -93,14 +93,21 @@ def __is_slot(sockets_or_slots): return isinstance(sockets_or_slots[0], bpy.types.MaterialTextureSlot) -def __get_image_data(sockets_or_slots): +def __get_image_data(sockets_or_slots, export_settings): # For shared ressources, such as images, we just store the portion of data that is needed in the glTF property # in a helper class. During generation of the glTF in the exporter these will then be combined to actual binary # ressources. - def split_pixels_by_channels(image: bpy.types.Image) -> typing.List[typing.List[float]]: + def split_pixels_by_channels(image: bpy.types.Image, export_settings) -> typing.List[typing.List[float]]: + channelcache = export_settings['gltf_channelcache'] + if image.name in channelcache: + return channelcache[image.name] + pixels = np.array(image.pixels) pixels = pixels.reshape((pixels.shape[0] // image.channels, image.channels)) channels = np.split(pixels, pixels.shape[1], axis=1) + + channelcache[image.name] = channels + return channels if __is_socket(sockets_or_slots): @@ -118,15 +125,16 @@ def __get_image_data(sockets_or_slots): }[elem.from_socket.name] if channel is not None: - pixels = [split_pixels_by_channels(result.shader_node.image)[channel]] + pixels = [split_pixels_by_channels(result.shader_node.image, export_settings)[channel]] else: - pixels = split_pixels_by_channels(result.shader_node.image) + pixels = split_pixels_by_channels(result.shader_node.image, export_settings) channel = 0 file_name = os.path.splitext(result.shader_node.image.name)[0] image_data = gltf2_io_image_data.ImageData( file_name, + result.shader_node.image.filepath, result.shader_node.image.size[0], result.shader_node.image.size[1], channel, @@ -140,10 +148,11 @@ def __get_image_data(sockets_or_slots): return image elif __is_slot(sockets_or_slots): texture = __get_tex_from_slot(sockets_or_slots[0]) - pixels = split_pixels_by_channels(texture.image) + pixels = split_pixels_by_channels(texture.image, export_settings) image_data = gltf2_io_image_data.ImageData( texture.name, + texture.image.filepath, texture.image.size[0], texture.image.size[1], 0, diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py b/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py index b1408b62..6561567e 100755 --- a/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py +++ b/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py @@ -20,6 +20,9 @@ from io_scene_gltf2.io.exp import gltf2_io_binary_data from io_scene_gltf2.io.exp import gltf2_io_image_data from io_scene_gltf2.io.exp import gltf2_io_buffer +import bpy +import os +from shutil import copyfile class GlTF2Exporter: """ @@ -141,9 +144,18 @@ class GlTF2Exporter: :return: """ for image in self.__images: - uri = output_path + image.name + ".png" - with open(uri, 'wb') as f: - f.write(image.to_png_data()) + dst_path = output_path + image.name + ".png" + + src_path = bpy.path.abspath(image.filepath) + if os.path.isfile(src_path): + # Source file exists. + if os.path.abspath(dst_path) != os.path.abspath(src_path): + # Only copy, if source and destination are not the same. + copyfile(src_path, dst_path) + else: + # Source file does not exist e.g. it is packed or has been generated. + with open(dst_path, 'wb') as f: + f.write(image.to_png_data()) def add_scene(self, scene: gltf2_io.Scene, active: bool = True): """ |