Skip to content

Commit

Permalink
[#D3D-6202] Updates Sketchfab plugin code to match updated import
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
loicSketchfab authored and cedricpinson committed Jun 30, 2021
1 parent b0d3798 commit 0d0220f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -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
19 changes: 10 additions & 9 deletions addons/io_sketchfab_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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': '',
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion addons/io_sketchfab_plugin/pack_for_export.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
36 changes: 15 additions & 21 deletions addons/io_sketchfab_plugin/sketchfab/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ../../../
Expand Down
2 changes: 1 addition & 1 deletion glTF-Blender-IO
Submodule glTF-Blender-IO updated 206 files
79 changes: 29 additions & 50 deletions khronos-gltf.patch
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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':

0 comments on commit 0d0220f

Please sign in to comment.