Repository: khronosgroup/gltf-blender-io
Issue: KHR_texture_transform support?

It seems that this code has disappeared: https://github.com/KhronosGroup/glTF-Blender-IO/search?q=KHR_texture_transform&unscoped_q=KHR_texture_transform

https://github.com/KhronosGroup/glTF-Blender-IO/commit/2786bbd8de72f48bf66189bca09956b24494f818

Changed files:
addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py
addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_material_normal_texture_info_class.py
addons/io_scene_gltf2/blender/exp/gltf2_blender_get.py

diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_material_normal_texture_info_class.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_material_normal_texture_info_class.py
index fa05653d..de3ebd89 100644
--- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_material_normal_texture_info_class.py
+++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_material_normal_texture_info_class.py
@@ -18,6 +18,9 @@ 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
+from io_scene_gltf2.blender.exp import gltf2_blender_export_keys
+from io_scene_gltf2.blender.exp import gltf2_blender_get
+from io_scene_gltf2.io.com.gltf2_io_extensions import Extension
 
 
 @cached
@@ -51,7 +54,22 @@ def __filter_texture_info(blender_shader_sockets_or_texture_slots, export_settin
 
 
 def __gather_extensions(blender_shader_sockets_or_texture_slots, export_settings):
-    return None
+    if not export_settings[gltf2_blender_export_keys.TEXTURE_TRANSFORM]:
+        return None
+
+    normal_map_node = blender_shader_sockets_or_texture_slots[0].links[0].from_node
+    if not isinstance(normal_map_node, bpy.types.ShaderNodeNormalMap):
+        return None
+
+    texture_socket = normal_map_node.inputs["Color"]
+    if len(texture_socket.links) == 0:
+        return None
+
+    texture_node = texture_socket.links[0].from_node
+    texture_transform = gltf2_blender_get.get_texture_transform_from_texture_node(texture_node)
+
+    extension = Extension("KHR_texture_transform", texture_transform)
+    return {"KHR_texture_transform": extension}
 
 
 def __gather_extras(blender_shader_sockets_or_texture_slots, export_settings):
diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py
index ce86d902..9e723727 100644
--- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py
+++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_texture_info.py
@@ -18,6 +18,9 @@ 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
+from io_scene_gltf2.blender.exp import gltf2_blender_export_keys
+from io_scene_gltf2.blender.exp import gltf2_blender_get
+from io_scene_gltf2.io.com.gltf2_io_extensions import Extension
 
 
 @cached
@@ -50,7 +53,16 @@ def __filter_texture_info(blender_shader_sockets_or_texture_slots, export_settin
 
 
 def __gather_extensions(blender_shader_sockets_or_texture_slots, export_settings):
-    return None
+    if not export_settings[gltf2_blender_export_keys.TEXTURE_TRANSFORM]:
+        return None
+
+    texture_node = blender_shader_sockets_or_texture_slots[0].links[0].from_node
+    texture_transform = gltf2_blender_get.get_texture_transform_from_texture_node(texture_node)
+    if texture_transform is None:
+        return None
+
+    extension = Extension("KHR_texture_transform", texture_transform)
+    return {"KHR_texture_transform": extension}
 
 
 def __gather_extras(blender_shader_sockets_or_texture_slots, export_settings):
diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_get.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_get.py
index ff23e916..0b96483b 100644
--- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_get.py
+++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_get.py
@@ -341,6 +341,38 @@ def get_texcoord_index_from_shader_node(glTF, name, shader_node):
     return 0
 
 
+def get_texture_transform_from_texture_node(texture_node):
+    if not isinstance(texture_node, bpy.types.ShaderNodeTexImage):
+        return None
+
+    mapping_socket = texture_node.inputs["Vector"]
+    if len(mapping_socket.links) == 0:
+        return None
+
+    mapping_node = mapping_socket.links[0].from_node
+    if not isinstance(mapping_node, bpy.types.ShaderNodeMapping):
+        return None
+
+    texture_transform = {}
+    if mapping_node.vector_type == 'TEXTURE':
+        texture_transform["offset"] = [-mapping_node.translation[0], -mapping_node.translation[1]]
+        texture_transform["rotation"] = -mapping_node.rotation[2]
+        texture_transform["scale"] = [1.0 / mapping_node.scale[0], 1.0 / mapping_node.scale[1]]
+    elif mapping_node.vector_type == 'POINT':
+        texture_transform["offset"] = [mapping_node.translation[0], mapping_node.translation[1]]
+        texture_transform["rotation"] = mapping_node.rotation[2]
+        texture_transform["scale"] = [mapping_node.scale[0], mapping_node.scale[1]]
+
+    if all([component == 0 for component in texture_transform["offset"]]):
+        del(texture_transform["offset"])
+    if all([component == 1 for component in texture_transform["scale"]]):
+        del(texture_transform["scale"])
+    if texture_transform["rotation"] == 0:
+        del(texture_transform["rotation"])
+
+    return texture_transform
+
+
 def get_image_uri(export_settings, blender_image):
     """Return the final URI depending on a file path."""
     file_format = get_image_format(export_settings, blender_image)