diff options
author | Julien Duroure <julien.duroure@gmail.com> | 2019-04-09 19:48:14 +0300 |
---|---|---|
committer | Julien Duroure <julien.duroure@gmail.com> | 2019-04-09 19:48:14 +0300 |
commit | 8e72572153ed7166c284598c53af1e0ab4937263 (patch) | |
tree | a244557434ca3e854ec45ac0eec63ff5d6f52af4 /io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py | |
parent | 4f34e011d491ed29a707608b72e92a9ba1172378 (diff) |
glTF exporter: fix / enhancement of texture images export
Diffstat (limited to 'io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py')
-rwxr-xr-x | io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py | 46 |
1 files changed, 24 insertions, 22 deletions
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 7d4d3a80..27bf869e 100755 --- a/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py +++ b/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py @@ -11,18 +11,15 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -from typing import Optional, List, Dict +import re +from typing import List from io_scene_gltf2.io.com import gltf2_io from io_scene_gltf2.io.com import gltf2_io_extensions 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 +from io_scene_gltf2.io.exp import gltf2_io_image_data -import bpy -import os -from shutil import copyfile class GlTF2Exporter: """ @@ -65,7 +62,7 @@ class GlTF2Exporter: ) self.__buffer = gltf2_io_buffer.Buffer() - self.__images = [] + self.__images = {} # mapping of all glTFChildOfRootProperty types to their corresponding root level arrays self.__childOfRootPropertyTypeLookup = { @@ -152,18 +149,10 @@ class GlTF2Exporter: :param output_path: :return: """ - for image in self.__images: - dst_path = output_path + image.name + image.get_extension() - 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()) + for name, image in self.__images.items(): + dst_path = output_path + "/" + name + image.file_extension + with open(dst_path, 'wb') as f: + f.write(image.data) def add_scene(self, scene: gltf2_io.Scene, active: bool = True): """ @@ -220,11 +209,23 @@ class GlTF2Exporter: return index def __add_image(self, image: gltf2_io_image_data.ImageData): - self.__images.append(image) + name = image.adjusted_name() + count = 1 + regex = re.compile(r"\d+$") + regex_found = re.findall(regex, name) + while name in self.__images.keys(): + if regex_found: + name = re.sub(regex, str(count), name) + else: + name += " " + str(count) + + count += 1 # TODO: we need to know the image url at this point already --> maybe add all options to the constructor of the # exporter # TODO: allow embedding of images (base64) - return image.name + image.get_extension() + + self.__images[name] = image + return name + image.file_extension @classmethod def __get_key_path(cls, d: dict, keypath: List[str], default=[]): @@ -288,7 +289,8 @@ class GlTF2Exporter: # image data needs to be saved to file if isinstance(node, gltf2_io_image_data.ImageData): - return self.__add_image(node) + image = self.__add_image(node) + return image # extensions if isinstance(node, gltf2_io_extensions.Extension): |