Skip to content

Commit

Permalink
[#D3D-6202] Updates GlTF import code from Khronos repository
Browse files Browse the repository at this point in the history
This commit updates the GlTF import code to match the version for
Blender 2.93 available on Khronos repository.

Matches version 1.6.16.
Commit 709630548cdc184af6ea50b2ff3ddc5450bc0af3
KhronosGroup/glTF-Blender-IO@7096305
  • Loading branch information
loicSketchfab authored and cedricpinson committed Jun 30, 2021
1 parent 4604f9c commit b0d3798
Show file tree
Hide file tree
Showing 34 changed files with 4,069 additions and 2,674 deletions.
5 changes: 5 additions & 0 deletions addons/io_sketchfab_plugin/blender/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Directories *com* and *imp* in this directory are copied - with very minor changes - from their respective counterparts on [Khronos repository](https://github.com/KhronosGroup/glTF-Blender-IO/tree/master/addons/io_scene_gltf2/blender), set at [commit 709630548cdc184af6ea50b2ff3ddc5450bc0af3](https://github.com/KhronosGroup/glTF-Blender-IO/commit/709630548cdc184af6ea50b2ff3ddc5450bc0af3) (Blender 2.93).

Licensed under [Apache Version 2](https://www.apache.org/licenses/LICENSE-2.0), see [LICENSE.txt](https://github.com/sketchfab/blender-plugin/blob/master/LICENSE.txt).


111 changes: 61 additions & 50 deletions addons/io_sketchfab_plugin/blender/com/gltf2_blender_conversion.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
"""
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Julien Duroure.
*
* ***** END GPL LICENSE BLOCK *****
"""

from mathutils import Matrix, Vector, Quaternion

class Conversion():

@staticmethod
def matrix_gltf_to_blender(mat_input):
mat = Matrix([mat_input[0:4], mat_input[4:8], mat_input[8:12], mat_input[12:16]])
mat.transpose()
return mat

@staticmethod
def loc_gltf_to_blender(loc):
return loc

@staticmethod
def scale_gltf_to_blender(scale):
return scale

@staticmethod
def quaternion_gltf_to_blender(q):
return Quaternion([q[3], q[0], q[1], q[2]])

def scale_to_matrix(scale):
mat = Matrix()
for i in range(3):
mat[i][i] = scale[i]

return mat
# Copyright 2018-2021 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.

from math import sin, cos

def texture_transform_blender_to_gltf(mapping_transform):
"""
Converts the offset/rotation/scale from a Mapping node applied in Blender's
UV space to the equivalent KHR_texture_transform.
"""
offset = mapping_transform.get('offset', [0, 0])
rotation = mapping_transform.get('rotation', 0)
scale = mapping_transform.get('scale', [1, 1])
return {
'offset': [
offset[0] - scale[1] * sin(rotation),
1 - offset[1] - scale[1] * cos(rotation),
],
'rotation': rotation,
'scale': [scale[0], scale[1]],
}

def texture_transform_gltf_to_blender(texture_transform):
"""
Converts a KHR_texture_transform into the equivalent offset/rotation/scale
for a Mapping node applied in Blender's UV space.
"""
offset = texture_transform.get('offset', [0, 0])
rotation = texture_transform.get('rotation', 0)
scale = texture_transform.get('scale', [1, 1])
return {
'offset': [
offset[0] + scale[1] * sin(rotation),
1 - offset[1] - scale[1] * cos(rotation),
],
'rotation': rotation,
'scale': [scale[0], scale[1]],
}

def get_target(property):
return {
"delta_location": "translation",
"delta_rotation_euler": "rotation",
"location": "translation",
"rotation_axis_angle": "rotation",
"rotation_euler": "rotation",
"rotation_quaternion": "rotation",
"scale": "scale",
"value": "weights"
}.get(property)
42 changes: 42 additions & 0 deletions addons/io_sketchfab_plugin/blender/com/gltf2_blender_data_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2018-2021 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.


def get_target_property_name(data_path: str) -> str:
"""Retrieve target property."""
return data_path.rsplit('.', 1)[-1]


def get_target_object_path(data_path: str) -> str:
"""Retrieve target object data path without property"""
path_split = data_path.rsplit('.', 1)
self_targeting = len(path_split) < 2
if self_targeting:
return ""
return path_split[0]

def get_rotation_modes(target_property: str) -> str:
"""Retrieve rotation modes based on target_property"""
if target_property == "rotation_euler":
return True, False, ["XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"]
elif target_property == "delta_rotation_euler":
return True, True, ["XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"]
elif target_property == "rotation_quaternion":
return True, False, ["QUATERNION"]
elif target_property == "delta_rotation_quaternion":
return True, True, ["QUATERNION"]
elif target_property in ["rotation_axis_angle"]:
return True, False, ["AXIS_ANGLE"]
else:
return False, False, []
97 changes: 97 additions & 0 deletions addons/io_sketchfab_plugin/blender/com/gltf2_blender_extras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright 2018-2021 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
from .gltf2_blender_json import is_json_convertible


# Custom properties, which are in most cases present and should not be imported/exported.
BLACK_LIST = ['cycles', 'cycles_visibility', 'cycles_curves', '_RNA_UI']


def generate_extras(blender_element):
"""Filter and create a custom property, which is stored in the glTF extra field."""
if not blender_element:
return None

extras = {}

for custom_property in blender_element.keys():
if custom_property in BLACK_LIST:
continue

value = __to_json_compatible(blender_element[custom_property])

if value is not None:
extras[custom_property] = value

if not extras:
return None

return extras


def __to_json_compatible(value):
"""Make a value (usually a custom property) compatible with json"""

if isinstance(value, bpy.types.ID):
return value

elif isinstance(value, str):
return value

elif isinstance(value, (int, float)):
return value

# for list classes
elif isinstance(value, list):
value = list(value)
# make sure contents are json-compatible too
for index in range(len(value)):
value[index] = __to_json_compatible(value[index])
return value

# for IDPropertyArray classes
elif hasattr(value, "to_list"):
value = value.to_list()
return value

elif hasattr(value, "to_dict"):
value = value.to_dict()
if is_json_convertible(value):
return value

return None


def set_extras(blender_element, extras, exclude=[]):
"""Copy extras onto a Blender object."""
if not extras or not isinstance(extras, dict):
return

for custom_property, value in extras.items():
if custom_property in BLACK_LIST:
continue
if custom_property in exclude:
continue

try:
blender_element[custom_property] = value
except Exception:
# Try to convert to string
try:
blender_element[custom_property] = str(value)
except Exception:
print('Error setting property %s to value of type %s' % (custom_property, type(value)))
30 changes: 0 additions & 30 deletions addons/io_sketchfab_plugin/blender/com/gltf2_blender_image.py

This file was deleted.

Loading

0 comments on commit b0d3798

Please sign in to comment.