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_gather_texture_info.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_gather_texture_info.py')
-rwxr-xr-x | io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py new file mode 100755 index 00000000..149a2a84 --- /dev/null +++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py @@ -0,0 +1,107 @@ +# 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 typing +from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached +from io_scene_gltf2.io.com import gltf2_io +from io_scene_gltf2.blender.exp import gltf2_blender_gather_texture +from io_scene_gltf2.blender.exp import gltf2_blender_search_node_tree + + +@cached +def gather_texture_info(blender_shader_sockets_or_texture_slots: typing.Union[ + typing.Tuple[bpy.types.NodeSocket], typing.Tuple[bpy.types.Texture]], + export_settings): + if not __filter_texture_info(blender_shader_sockets_or_texture_slots, export_settings): + return None + + texture_info = gltf2_io.TextureInfo( + extensions=__gather_extensions(blender_shader_sockets_or_texture_slots, export_settings), + extras=__gather_extras(blender_shader_sockets_or_texture_slots, export_settings), + index=__gather_index(blender_shader_sockets_or_texture_slots, export_settings), + tex_coord=__gather_tex_coord(blender_shader_sockets_or_texture_slots, export_settings) + ) + + return texture_info + + +def __filter_texture_info(blender_shader_sockets_or_texture_slots, export_settings): + if not blender_shader_sockets_or_texture_slots: + return False + if not all([elem is not None for elem in blender_shader_sockets_or_texture_slots]): + return False + if isinstance(blender_shader_sockets_or_texture_slots[0], bpy.types.NodeSocket): + if any([__get_tex_from_socket(socket) is None for socket in blender_shader_sockets_or_texture_slots]): + # sockets do not lead to a texture --> discard + return False + return True + + +def __gather_extensions(blender_shader_sockets_or_texture_slots, export_settings): + return None + + +def __gather_extras(blender_shader_sockets_or_texture_slots, export_settings): + return None + + +def __gather_index(blender_shader_sockets_or_texture_slots, export_settings): + # We just put the actual shader into the 'index' member + return gltf2_blender_gather_texture.gather_texture(blender_shader_sockets_or_texture_slots, export_settings) + + +def __gather_tex_coord(blender_shader_sockets_or_texture_slots, export_settings): + if isinstance(blender_shader_sockets_or_texture_slots[0], bpy.types.NodeSocket): + blender_shader_node = __get_tex_from_socket(blender_shader_sockets_or_texture_slots[0]).shader_node + if len(blender_shader_node.inputs['Vector'].links) == 0: + return 0 + + input_node = blender_shader_node.inputs['Vector'].links[0].from_node + + if isinstance(input_node, bpy.types.ShaderNodeMapping): + + if len(input_node.inputs['Vector'].links) == 0: + return 0 + + input_node = input_node.inputs['Vector'].links[0].from_node + + if not isinstance(input_node, bpy.types.ShaderNodeUVMap): + return 0 + + if input_node.uv_map == '': + return 0 + + # Try to gather map index. + for blender_mesh in bpy.data.meshes: + texCoordIndex = blender_mesh.uv_textures.find(input_node.uv_map) + if texCoordIndex >= 0: + return texCoordIndex + + return 0 + elif isinstance(blender_shader_sockets_or_texture_slots[0], bpy.types.MaterialTextureSlot): + # TODO: implement for texture slots + return 0 + else: + raise NotImplementedError() + + +def __get_tex_from_socket(socket): + result = gltf2_blender_search_node_tree.from_socket( + socket, + gltf2_blender_search_node_tree.FilterByType(bpy.types.ShaderNodeTexImage)) + if not result: + return None + return result[0] + |