From 0d0220fb776d137761636d6272923595a1ff5ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Norgeot?= Date: Mon, 28 Jun 2021 17:29:22 +0200 Subject: [PATCH] [#D3D-6202] Updates Sketchfab plugin code to match updated import This commit updates the Sketchfab-side code and patch to take into account changes to the GlTF import functions. It also switches the submodule repository to Khronos' one instead of a previous fork of their repo which lived in the Sketchfab Github organization. Updates to commit 709630548cdc184af6ea50b2ff3ddc5450bc0af3 An uniformization of licenses (GPL to Apache) is also done. Note: no change in behaviour should happen. --- .gitmodules | 2 +- addons/io_sketchfab_plugin/__init__.py | 19 ++--- .../blender/imp/gltf2_blender_material.py | 1 + addons/io_sketchfab_plugin/pack_for_export.py | 2 +- .../io_sketchfab_plugin/sketchfab/__init__.py | 36 ++++----- build.sh | 2 +- glTF-Blender-IO | 2 +- khronos-gltf.patch | 79 +++++++------------ 8 files changed, 59 insertions(+), 84 deletions(-) diff --git a/.gitmodules b/.gitmodules index 181db46..c98c9df 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "glTF-Blender-IO"] path = glTF-Blender-IO - url = https://github.com/sketchfab/glTF-Blender-IO.git + url = https://github.com/KhronosGroup/glTF-Blender-IO.git branch = master diff --git a/addons/io_sketchfab_plugin/__init__.py b/addons/io_sketchfab_plugin/__init__.py index 7a98d3e..fe9e282 100644 --- a/addons/io_sketchfab_plugin/__init__.py +++ b/addons/io_sketchfab_plugin/__init__.py @@ -1,5 +1,5 @@ """ -Copyright 2019 Sketchfab +Copyright 2021 Sketchfab Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ from .io import * from .sketchfab import Config, Utils, Cache +from .io.imp.gltf2_io_gltf import * from .blender.imp.gltf2_blender_gltf import * from .blender.blender_version import Version @@ -74,9 +75,9 @@ 'name': 'Sketchfab Plugin', 'description': 'Browse and download free Sketchfab downloadable models', 'author': 'Sketchfab', - 'license': 'GPL', + 'license': 'APACHE2', 'deps': '', - 'version': (1, 4, 0), + 'version': (1, 4, 1), "blender": (2, 80, 0), 'location': 'View3D > Tools > Sketchfab', 'warning': '', @@ -1064,16 +1065,16 @@ def exectue(self, context): def modal(self, context, event): bpy.context.scene.render.engine = Version.ENGINE - gltf_importer = glTFImporter(self.gltf_path, Log.default()) - success, txt = gltf_importer.read() - if not success: - print('Failed to read GLTF') + gltf_importer = glTFImporter(self.gltf_path) + gltf_importer.read() + try: old_objects = [o.name for o in bpy.data.objects] # Get the current objects inorder to find the new node hierarchy - BlenderGlTF.create(gltf_importer, root_name=Utils.make_model_name(gltf_importer.data)) + BlenderGlTF.create(gltf_importer) set_import_status('') Utils.clean_downloaded_model_dir(self.uid) - Utils.clean_node_hierarchy([o for o in bpy.data.objects if o.name not in old_objects]) + root_name = Utils.make_model_name(gltf_importer.data) + Utils.clean_node_hierarchy([o for o in bpy.data.objects if o.name not in old_objects], root_name) return {'FINISHED'} except Exception: import traceback diff --git a/addons/io_sketchfab_plugin/blender/imp/gltf2_blender_material.py b/addons/io_sketchfab_plugin/blender/imp/gltf2_blender_material.py index 077f5af..5e49b7d 100644 --- a/addons/io_sketchfab_plugin/blender/imp/gltf2_blender_material.py +++ b/addons/io_sketchfab_plugin/blender/imp/gltf2_blender_material.py @@ -59,6 +59,7 @@ def create(gltf, material_idx, vertex_color): @staticmethod def set_double_sided(pymaterial, mat): mat.use_backface_culling = (pymaterial.double_sided != True) + mat.show_transparent_back = False @staticmethod def set_alpha_mode(pymaterial, mat): diff --git a/addons/io_sketchfab_plugin/pack_for_export.py b/addons/io_sketchfab_plugin/pack_for_export.py index 140275f..055a3fe 100644 --- a/addons/io_sketchfab_plugin/pack_for_export.py +++ b/addons/io_sketchfab_plugin/pack_for_export.py @@ -1,5 +1,5 @@ """ -Copyright 2019 Sketchfab +Copyright 2021 Sketchfab Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/addons/io_sketchfab_plugin/sketchfab/__init__.py b/addons/io_sketchfab_plugin/sketchfab/__init__.py index e15ea6c..a0a3775 100644 --- a/addons/io_sketchfab_plugin/sketchfab/__init__.py +++ b/addons/io_sketchfab_plugin/sketchfab/__init__.py @@ -1,23 +1,18 @@ """ - * ***** 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. - * - * - * ***** END GPL LICENSE BLOCK ***** - """ +Copyright 2021 Sketchfab + +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 + + https://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 os import bpy @@ -196,7 +191,7 @@ def get_uid_from_model_url(model_url, use_org_profile=False): def get_uid_from_download_url(model_url): return model_url.split('/')[6] - def clean_node_hierarchy(objects): + def clean_node_hierarchy(objects, root_name): """ Removes the useless nodes in a hierarchy TODO: Keep the transform (might impact Yup/Zup) @@ -208,7 +203,6 @@ def clean_node_hierarchy(objects): root = object if root is None: return None - root_name = root.name # Go down its hierarchy until one child has multiple children, or a single mesh # Keep the name while deleting objects in the hierarchy diff --git a/build.sh b/build.sh index c374a9d..17c98bf 100755 --- a/build.sh +++ b/build.sh @@ -20,7 +20,7 @@ then echo "Trying to apply khronos-gltf.patch" cd glTF-Blender-IO/ git apply ../khronos-gltf.patch - cp -r ./addons/io_scene_gltf2/io/ ../addons/io_sketchfab_plugin/io/ + cp -r ./addons/io_scene_gltf2/io/ ../addons/io_sketchfab_plugin/ cd ../addons/io_sketchfab_plugin/io/ sed -i 's/io_scene_gltf2.io/./g' ./*/*.py cd ../../../ diff --git a/glTF-Blender-IO b/glTF-Blender-IO index b35e27d..7096305 160000 --- a/glTF-Blender-IO +++ b/glTF-Blender-IO @@ -1 +1 @@ -Subproject commit b35e27dcbc0e01fd1cca59255dbf1754eec55eb4 +Subproject commit 709630548cdc184af6ea50b2ff3ddc5450bc0af3 diff --git a/khronos-gltf.patch b/khronos-gltf.patch index 7f3a767..58c13db 100644 --- a/khronos-gltf.patch +++ b/khronos-gltf.patch @@ -1,39 +1,18 @@ -diff --git a/addons/io_scene_gltf2/io/com/__init__.py b/addons/io_scene_gltf2/io/com/__init__.py -new file mode 100644 -index 0000000..e69de29 diff --git a/addons/io_scene_gltf2/io/com/gltf2_io.py b/addons/io_scene_gltf2/io/com/gltf2_io.py -index 95a9f9e..c19ae1b 100644 +index d00a914d..a51ff4e3 100644 --- a/addons/io_scene_gltf2/io/com/gltf2_io.py +++ b/addons/io_scene_gltf2/io/com/gltf2_io.py -@@ -22,7 +22,7 @@ +@@ -26,7 +26,7 @@ import sys import traceback - + -from io_scene_gltf2.io.com import gltf2_io_debug +from . import gltf2_io_debug - - + + def from_int(x): -@@ -71,9 +71,15 @@ def from_float(x): - assert isinstance(x, (float, int)) and not isinstance(x, bool) - return float(x) - -- - def from_str(x): -- assert isinstance(x, str) -+ res = "" -+ if "c4d" in sys.modules: # Cinema4D -+ res = isinstance(x, str) or isinstance(x, unicode) -+ elif "bpy" in sys.modules: # Blender, be explicit -+ res = isinstance(x, str) -+ else: # Other cases -+ res = isinstance(x, str) -+ assert res - return x - - diff --git a/addons/io_scene_gltf2/io/com/gltf2_io_debug.py b/addons/io_scene_gltf2/io/com/gltf2_io_debug.py -index 51b825a..ccf45e5 100644 +index 290616cc..f987d9e3 100644 --- a/addons/io_scene_gltf2/io/com/gltf2_io_debug.py +++ b/addons/io_scene_gltf2/io/com/gltf2_io_debug.py @@ -124,3 +124,17 @@ class Log: @@ -54,50 +33,50 @@ index 51b825a..ccf45e5 100644 + + def default(): + return str(logging.ERROR) +\ No newline at end of file diff --git a/addons/io_scene_gltf2/io/imp/__init__.py b/addons/io_scene_gltf2/io/imp/__init__.py -index 15e503a..5c2981e 100644 +index 666fdf3f..81f88469 100644 --- a/addons/io_scene_gltf2/io/imp/__init__.py +++ b/addons/io_scene_gltf2/io/imp/__init__.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. - + -"""IO imp package.""" +from .gltf2_io_gltf import * +\ No newline at end of file diff --git a/addons/io_scene_gltf2/io/imp/gltf2_io_gltf.py b/addons/io_scene_gltf2/io/imp/gltf2_io_gltf.py -index 16a4242..02fd847 100644 +index aa9bda38..f8dca120 100644 --- a/addons/io_scene_gltf2/io/imp/gltf2_io_gltf.py +++ b/addons/io_scene_gltf2/io/imp/gltf2_io_gltf.py -@@ -24,16 +24,20 @@ from os.path import dirname, join, getsize, isfile +@@ -30,19 +30,14 @@ class ImportError(RuntimeError): class glTFImporter(): """glTF Importer class.""" - + - def __init__(self, filename, import_settings): + def __init__(self, filename, loglevel=logging.ERROR): """initialization.""" self.filename = filename - self.import_settings = import_settings + self.glb_buffer = None self.buffers = {} - -+ """ DIFF -+ self.import_settings = import_settings - if 'loglevel' not in self.import_settings.keys(): - self.import_settings['loglevel'] = logging.ERROR + self.accessor_cache = {} + self.decode_accessor_cache = {} - - log = Log(import_settings['loglevel']) -+ """ -+ +- if 'loglevel' not in self.import_settings.keys(): +- self.import_settings['loglevel'] = logging.ERROR +- +- log = Log(import_settings['loglevel']) + log = Log(loglevel) -+ self.log = log.logger self.log_handler = log.hdlr -@@ -159,7 +163,7 @@ class glTFImporter(): - # glTF file - if not self.is_glb_format: - self.content = None -- with open(self.filename, 'r') as f: -+ with open(self.filename, 'r', encoding='utf8') as f: - content = f.read() - try: - self.data = gltf_from_dict(json.loads(content, parse_constant=glTFImporter.bad_json_value)) +@@ -139,7 +134,7 @@ class glTFImporter(): + if not isfile(self.filename): + raise ImportError("Please select a file") + +- with open(self.filename, 'rb') as f: ++ with open(self.filename, 'rb') as f: + content = memoryview(f.read()) + + if content[:4] == b'glTF':