diff options
author | Julien Duroure <julien.duroure@gmail.com> | 2018-11-24 18:28:33 +0300 |
---|---|---|
committer | Julien Duroure <julien.duroure@gmail.com> | 2018-11-24 18:28:33 +0300 |
commit | b1f2133fa2849da272e9d8404f371220226ddaf1 (patch) | |
tree | 25db56e0f2211bd1059fe0e04e78430a6156e021 /io_scene_gltf2/blender/exp/gltf2_blender_export.py | |
parent | 8959f1798cfc86924493347304118c61bd5c7f7a (diff) |
Initial commit of glTF 2.0 importer/exporter
Official Khronos Group Blender glTF 2.0 importer and exporter.
glTF specification: https://github.com/KhronosGroup/glTF
The upstream repository can be found here:
https://github.com/KhronosGroup/glTF-Blender-IO
Reviewed By: Bastien, Campbell
Differential Revision: https://developer.blender.org/D3929
Diffstat (limited to 'io_scene_gltf2/blender/exp/gltf2_blender_export.py')
-rwxr-xr-x | io_scene_gltf2/blender/exp/gltf2_blender_export.py | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_export.py b/io_scene_gltf2/blender/exp/gltf2_blender_export.py new file mode 100755 index 00000000..ae2db26b --- /dev/null +++ b/io_scene_gltf2/blender/exp/gltf2_blender_export.py @@ -0,0 +1,100 @@ +# Copyright 2018 The glTF-Blender-IO authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import bpy +import sys +import traceback + +from . import gltf2_blender_export_keys +from . import gltf2_blender_gather +from .gltf2_blender_gltf2_exporter import GlTF2Exporter +from ..com import gltf2_blender_json +from ...io.exp import gltf2_io_export +from ...io.com.gltf2_io_debug import print_console, print_newline + + +def save(operator, + context, + export_settings): + """Start the glTF 2.0 export and saves to content either to a .gltf or .glb file.""" + print_console('INFO', 'Starting glTF 2.0 export') + context.window_manager.progress_begin(0, 100) + context.window_manager.progress_update(0) + + if not export_settings[gltf2_blender_export_keys.COPYRIGHT]: + export_settings[gltf2_blender_export_keys.COPYRIGHT] = None + + scenes, animations = gltf2_blender_gather.gather_gltf2(export_settings) + exporter = GlTF2Exporter(copyright=export_settings[gltf2_blender_export_keys.COPYRIGHT]) + for scene in scenes: + exporter.add_scene(scene) + for animation in animations: + exporter.add_animation(animation) + + buffer = bytes() + if export_settings[gltf2_blender_export_keys.FORMAT] == 'ASCII': + # .gltf + if export_settings[gltf2_blender_export_keys.EMBED_BUFFERS]: + exporter.finalize_buffer(export_settings[gltf2_blender_export_keys.FILE_DIRECTORY]) + else: + exporter.finalize_buffer(export_settings[gltf2_blender_export_keys.FILE_DIRECTORY], + export_settings[gltf2_blender_export_keys.BINARY_FILENAME]) + else: + # .glb + buffer = exporter.finalize_buffer(export_settings[gltf2_blender_export_keys.FILE_DIRECTORY], is_glb=True) + exporter.finalize_images(export_settings[gltf2_blender_export_keys.FILE_DIRECTORY]) + glTF = exporter.glTF + + # + + # TODO: move to custom JSON encoder + def dict_strip(obj): + o = obj + if isinstance(obj, dict): + o = {} + for k, v in obj.items(): + if v is None: + continue + elif isinstance(v, list) and len(v) == 0: + continue + o[k] = dict_strip(v) + elif isinstance(obj, list): + o = [] + for v in obj: + o.append(dict_strip(v)) + elif isinstance(obj, float): + # force floats to int, if they are integers (prevent INTEGER_WRITTEN_AS_FLOAT validator warnings) + if int(obj) == obj: + return int(obj) + return o + + try: + gltf2_io_export.save_gltf(dict_strip(glTF.to_dict()), export_settings, gltf2_blender_json.BlenderJSONEncoder, + buffer) + except AssertionError as e: + _, _, tb = sys.exc_info() + traceback.print_tb(tb) # Fixed format + tb_info = traceback.extract_tb(tb) + for tbi in tb_info: + filename, line, func, text = tbi + print_console('ERROR', 'An error occurred on line {} in statement {}'.format(line, text)) + print_console('ERROR', str(e)) + raise e + + print_console('INFO', 'Finished glTF 2.0 export') + context.window_manager.progress_end() + print_newline() + + return {'FINISHED'} + |