diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py index 97ef843b55a6e237de2f095d35c5443969036d58..1a97bee83455cb76d6d41f2f9a8cb0653593801e 100755 --- a/io_scene_gltf2/__init__.py +++ b/io_scene_gltf2/__init__.py @@ -15,7 +15,7 @@ bl_info = { 'name': 'glTF 2.0 format', 'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin SchmithĂĽsen, Jim Eckerlein, and many external contributors', - "version": (1, 8, 12), + "version": (1, 8, 13), 'blender': (3, 1, 0), 'location': 'File > Import-Export', 'description': 'Import-Export as glTF 2.0', @@ -67,7 +67,8 @@ from bpy_extras.io_utils import ImportHelper, ExportHelper # Functions / Classes. # -extension_panel_unregister_functors = [] +exporter_extension_panel_unregister_functors = [] +importer_extension_panel_unregister_functors = [] def ensure_filepath_matches_export_format(filepath, export_format): @@ -479,11 +480,11 @@ class ExportGLTF2_Base: for addon_name in preferences.addons.keys(): try: if hasattr(sys.modules[addon_name], 'glTF2ExportUserExtension') or hasattr(sys.modules[addon_name], 'glTF2ExportUserExtensions'): - extension_panel_unregister_functors.append(sys.modules[addon_name].register_panel()) + exporter_extension_panel_unregister_functors.append(sys.modules[addon_name].register_panel()) except Exception: pass - self.has_active_extensions = len(extension_panel_unregister_functors) > 0 + self.has_active_exporter_extensions = len(exporter_extension_panel_unregister_functors) > 0 return ExportHelper.invoke(self, context, event) def save_settings(self, context): @@ -945,7 +946,7 @@ class GLTF_PT_export_animation_skinning(bpy.types.Panel): class GLTF_PT_export_user_extensions(bpy.types.Panel): bl_space_type = 'FILE_BROWSER' bl_region_type = 'TOOL_PROPS' - bl_label = "Extensions" + bl_label = "Exporter Extensions" bl_parent_id = "FILE_PT_operator" bl_options = {'DEFAULT_CLOSED'} @@ -954,13 +955,30 @@ class GLTF_PT_export_user_extensions(bpy.types.Panel): sfile = context.space_data operator = sfile.active_operator - return operator.bl_idname == "EXPORT_SCENE_OT_gltf" and operator.has_active_extensions + return operator.bl_idname == "EXPORT_SCENE_OT_gltf" and operator.has_active_exporter_extensions def draw(self, context): layout = self.layout layout.use_property_split = True layout.use_property_decorate = False # No animation. +class GLTF_PT_import_user_extensions(bpy.types.Panel): + bl_space_type = 'FILE_BROWSER' + bl_region_type = 'TOOL_PROPS' + bl_label = "Importer Extensions" + bl_parent_id = "FILE_PT_operator" + bl_options = {'DEFAULT_CLOSED'} + + @classmethod + def poll(cls, context): + sfile = context.space_data + operator = sfile.active_operator + return operator.bl_idname == "IMPORT_SCENE_OT_gltf" and operator.has_active_importer_extensions + + def draw(self, context): + layout = self.layout + layout.use_property_split = True + layout.use_property_decorate = False # No animation. class ExportGLTF2(bpy.types.Operator, ExportGLTF2_Base, ExportHelper): """Export scene as glTF 2.0 file""" @@ -1060,6 +1078,19 @@ class ImportGLTF2(Operator, ImportHelper): layout.prop(self, 'guess_original_bind_pose') layout.prop(self, 'bone_heuristic') + def invoke(self, context, event): + import sys + preferences = bpy.context.preferences + for addon_name in preferences.addons.keys(): + try: + if hasattr(sys.modules[addon_name], 'glTF2ImportUserExtension') or hasattr(sys.modules[addon_name], 'glTF2ImportUserExtensions'): + importer_extension_panel_unregister_functors.append(sys.modules[addon_name].register_panel()) + except Exception: + pass + + self.has_active_importer_extensions = len(importer_extension_panel_unregister_functors) > 0 + return ImportHelper.invoke(self, context, event) + def execute(self, context): return self.import_gltf2(context) @@ -1069,6 +1100,20 @@ class ImportGLTF2(Operator, ImportHelper): self.set_debug_log() import_settings = self.as_keywords() + user_extensions = [] + + import sys + preferences = bpy.context.preferences + for addon_name in preferences.addons.keys(): + try: + module = sys.modules[addon_name] + except Exception: + continue + if hasattr(module, 'glTF2ImportUserExtension'): + extension_ctor = module.glTF2ImportUserExtension + user_extensions.append(extension_ctor()) + import_settings['import_user_extensions'] = user_extensions + if self.files: # Multiple file import ret = {'CANCELLED'} @@ -1137,7 +1182,8 @@ classes = ( GLTF_PT_export_animation_shapekeys, GLTF_PT_export_animation_skinning, GLTF_PT_export_user_extensions, - ImportGLTF2 + ImportGLTF2, + GLTF_PT_import_user_extensions ) @@ -1154,9 +1200,13 @@ def register(): def unregister(): for c in classes: bpy.utils.unregister_class(c) - for f in extension_panel_unregister_functors: + for f in exporter_extension_panel_unregister_functors: + f() + exporter_extension_panel_unregister_functors.clear() + + for f in importer_extension_panel_unregister_functors: f() - extension_panel_unregister_functors.clear() + importer_extension_panel_unregister_functors.clear() # bpy.utils.unregister_module(__name__) diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py b/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py index 642515aa94d8fad5484124d80de9c2aecc0ccd54..03f8592ccc9908208356fe659cf10ec46e674593 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py @@ -18,6 +18,7 @@ from mathutils import Vector from ...io.imp.gltf2_io_binary import BinaryData from .gltf2_blender_animation_utils import make_fcurve from .gltf2_blender_vnode import VNode +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions class BlenderNodeAnim(): @@ -46,6 +47,8 @@ class BlenderNodeAnim(): vnode = gltf.vnodes[node_idx] path = channel.target.path + import_user_extensions('gather_import_animation_channel_before_hook', gltf, animation, vnode, path, channel) + action = BlenderNodeAnim.get_or_create_action(gltf, node_idx, animation.track_name) keys = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].input) @@ -152,6 +155,8 @@ class BlenderNodeAnim(): interpolation=animation.samplers[channel.sampler].interpolation, ) + import_user_extensions('gather_import_animation_channel_after_hook', gltf, animation, vnode, path, channel, action) + @staticmethod def get_or_create_action(gltf, node_idx, anim_name): vnode = gltf.vnodes[node_idx] diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.py b/io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.py index 19723ed92396910a01a2d430f36b861338d9ccd7..3b864d6bf8abd05f5e3b2dacbb38ef132cbe563e 100644 --- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation_weight.py @@ -16,6 +16,7 @@ import bpy from ...io.imp.gltf2_io_binary import BinaryData from .gltf2_blender_animation_utils import make_fcurve +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions class BlenderWeightAnim(): @@ -29,6 +30,9 @@ class BlenderWeightAnim(): vnode = gltf.vnodes[vnode_id] node_idx = vnode.mesh_node_idx + + import_user_extensions('gather_import_animation_weight_before_hook', gltf, vnode, gltf.data.animations[anim_idx]) + if node_idx is None: return @@ -90,3 +94,5 @@ class BlenderWeightAnim(): max_weight = max(coords[1:2]) if min_weight < kb.slider_min: kb.slider_min = min_weight if max_weight > kb.slider_max: kb.slider_max = max_weight + + import_user_extensions('gather_import_animation_weight_after_hook', gltf, vnode, animation) diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_camera.py b/io_scene_gltf2/blender/imp/gltf2_blender_camera.py index cc73a690a63df32a9932610a00d42e5c8670ae03..e5f6e3d1636e66e367c21bbe1b25199b2d5694ae 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_camera.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_camera.py @@ -14,6 +14,7 @@ import bpy from ..com.gltf2_blender_extras import set_extras +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions class BlenderCamera(): @@ -22,10 +23,12 @@ class BlenderCamera(): raise RuntimeError("%s should not be instantiated" % cls) @staticmethod - def create(gltf, camera_id): + def create(gltf, vnode, camera_id): """Camera creation.""" pycamera = gltf.data.cameras[camera_id] + import_user_extensions('gather_import_camera_before_hook', gltf, vnode, pycamera) + if not pycamera.name: pycamera.name = "Camera" @@ -55,5 +58,4 @@ class BlenderCamera(): # Infinite projection cam.clip_end = 1e12 # some big number - return cam diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_image.py b/io_scene_gltf2/blender/imp/gltf2_blender_image.py index 3acf17864b22e04bc2c5fd8c1a618d40bfd95eab..0df6dc0e6f83b4658b8f2e49d622abbf9e75eb4d 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_image.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_image.py @@ -20,6 +20,7 @@ import urllib.parse import re from ...io.imp.gltf2_io_binary import BinaryData +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions # Note that Image is not a glTF2.0 object @@ -32,6 +33,9 @@ class BlenderImage(): def create(gltf, img_idx): """Image creation.""" img = gltf.data.images[img_idx] + + import_user_extensions('gather_import_image_before_hook', gltf, img) + img_name = img.name if img.blender_image_name is not None: @@ -90,6 +94,8 @@ class BlenderImage(): if not is_placeholder and needs_pack: blender_image.pack() + import_user_extensions('gather_import_image_after_hook', gltf, img, blender_image) + def _placeholder_image(name, path): image = bpy.data.images.new(name, 128, 128) # allow the path to be resolved later diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_light.py b/io_scene_gltf2/blender/imp/gltf2_blender_light.py index 71990eb3b45f1df6bc077d0a933c645aeaff1635..9e6b8a963e6323445df24114443f264f4a072010 100644 --- a/io_scene_gltf2/blender/imp/gltf2_blender_light.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_light.py @@ -16,6 +16,7 @@ import bpy from math import pi from ..com.gltf2_blender_extras import set_extras +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions class BlenderLight(): @@ -24,9 +25,12 @@ class BlenderLight(): raise RuntimeError("%s should not be instantiated" % cls) @staticmethod - def create(gltf, light_id): + def create(gltf, vnode, light_id): """Light creation.""" pylight = gltf.data.extensions['KHR_lights_punctual']['lights'][light_id] + + import_user_extensions('gather_import_light_before_hook', gltf, vnode, pylight) + if pylight['type'] == "directional": light = BlenderLight.create_directional(gltf, light_id) elif pylight['type'] == "point": diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_material.py b/io_scene_gltf2/blender/imp/gltf2_blender_material.py index 077f5af6ee49fb1a95f2b312c1e281620d8e1cec..1a5be06f9e9ab161f65452b3913ec90b2b24a280 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_material.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_material.py @@ -18,6 +18,7 @@ from ..com.gltf2_blender_extras import set_extras from .gltf2_blender_pbrMetallicRoughness import MaterialHelper, pbr_metallic_roughness from .gltf2_blender_KHR_materials_pbrSpecularGlossiness import pbr_specular_glossiness from .gltf2_blender_KHR_materials_unlit import unlit +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions class BlenderMaterial(): @@ -30,6 +31,8 @@ class BlenderMaterial(): """Material creation.""" pymaterial = gltf.data.materials[material_idx] + import_user_extensions('gather_import_material_before_hook', gltf, pymaterial, vertex_color) + name = pymaterial.name if name is None: name = "Material_" + str(material_idx) @@ -56,6 +59,8 @@ class BlenderMaterial(): else: pbr_metallic_roughness(mh) + import_user_extensions('gather_import_material_after_hook', gltf, pymaterial, vertex_color, mat) + @staticmethod def set_double_sided(pymaterial, mat): mat.use_backface_culling = (pymaterial.double_sided != True) diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py b/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py index bbff340c65071b51e74a1d9e9c969ae438baf877..41dd4d0342380a1acc32efb64d01609bb1de3142 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py @@ -21,6 +21,7 @@ from ..com.gltf2_blender_extras import set_extras from .gltf2_blender_material import BlenderMaterial from ...io.com.gltf2_io_debug import print_console from .gltf2_io_draco_compression_extension import decode_primitive +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions class BlenderMesh(): @@ -41,6 +42,9 @@ COLOR_MAX = 8 def create_mesh(gltf, mesh_idx, skin_idx): pymesh = gltf.data.meshes[mesh_idx] + + import_user_extensions('gather_import_mesh_before_hook', gltf, pymesh) + name = pymesh.name or 'Mesh_%d' % mesh_idx mesh = bpy.data.meshes.new(name) @@ -56,6 +60,8 @@ def create_mesh(gltf, mesh_idx, skin_idx): if tmp_ob: bpy.data.objects.remove(tmp_ob) + import_user_extensions('gather_import_mesh_after_hook', gltf, pymesh, mesh) + return mesh diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_node.py b/io_scene_gltf2/blender/imp/gltf2_blender_node.py index ed164a1e7ccf8bdeefe91744e1e57b414f6b3e87..2fdbcfa563f24d55b94e65a9586e95bba54a0356 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_node.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_node.py @@ -19,6 +19,7 @@ from .gltf2_blender_mesh import BlenderMesh from .gltf2_blender_camera import BlenderCamera from .gltf2_blender_light import BlenderLight from .gltf2_blender_vnode import VNode +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions class BlenderNode(): """Blender Node.""" @@ -35,7 +36,10 @@ class BlenderNode(): gltf.log.critical("Node %d of %d (id %s)", gltf.display_current_node, len(gltf.vnodes), vnode_id) if vnode.type == VNode.Object: - BlenderNode.create_object(gltf, vnode_id) + gltf_node = gltf.data.nodes[vnode_id] if isinstance(vnode_id, int) else None + import_user_extensions('gather_import_node_before_hook', gltf, vnode, gltf_node) + obj = BlenderNode.create_object(gltf, vnode_id) + import_user_extensions('gather_import_node_after_hook', gltf, vnode, gltf_node, obj) if vnode.is_arma: BlenderNode.create_bones(gltf, vnode_id) @@ -59,16 +63,22 @@ class BlenderNode(): elif vnode.camera_node_idx is not None: pynode = gltf.data.nodes[vnode.camera_node_idx] - cam = BlenderCamera.create(gltf, pynode.camera) + cam = BlenderCamera.create(gltf, vnode, pynode.camera) name = vnode.name or cam.name obj = bpy.data.objects.new(name, cam) + # Since we create the actual Blender object after the create call, we call the hook here + import_user_extensions('gather_import_camera_after_hook', gltf, vnode, obj, cam) + elif vnode.light_node_idx is not None: pynode = gltf.data.nodes[vnode.light_node_idx] - light = BlenderLight.create(gltf, pynode.extensions['KHR_lights_punctual']['light']) + light = BlenderLight.create(gltf, vnode, pynode.extensions['KHR_lights_punctual']['light']) name = vnode.name or light.name obj = bpy.data.objects.new(name, light) + # Since we create the actual Blender object after the create call, we call the hook here + import_user_extensions('gather_import_light_after_hook', gltf, vnode, obj, light) + elif vnode.is_arma: armature = bpy.data.armatures.new(vnode.arma_name) name = vnode.name or armature.name diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py index 055202281327efefad415ffdf2adcb173cb0eb88..57c53527478e02286fca2fd4877f97493764a3d8 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py @@ -18,6 +18,7 @@ from .gltf2_blender_node import BlenderNode from .gltf2_blender_animation import BlenderAnimation from .gltf2_blender_vnode import VNode, compute_vnodes from ..com.gltf2_blender_extras import set_extras +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions class BlenderScene(): @@ -36,6 +37,7 @@ class BlenderScene(): scene.render.engine = "BLENDER_EEVEE" if gltf.data.scene is not None: + import_user_extensions('gather_import_scene_before_hook', gltf, gltf.data.scenes[gltf.data.scene], scene) pyscene = gltf.data.scenes[gltf.data.scene] set_extras(scene, pyscene.extras) @@ -44,8 +46,14 @@ class BlenderScene(): gltf.display_current_node = 0 # for debugging BlenderNode.create_vnode(gltf, 'root') + # User extensions before scene creation + import_user_extensions('gather_import_scene_after_nodes_hook', gltf, gltf.data.scenes[gltf.data.scene], scene) + + # User extensions after scene creation BlenderScene.create_animations(gltf) + import_user_extensions('gather_import_scene_after_animation_hook', gltf, gltf.data.scenes[gltf.data.scene], scene) + if bpy.context.mode != 'OBJECT': bpy.ops.object.mode_set(mode='OBJECT') BlenderScene.select_imported_objects(gltf) diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_texture.py b/io_scene_gltf2/blender/imp/gltf2_blender_texture.py index ddeb5bfc33dbf44c31f73778a0eff79f60d9263b..2a41c28e98e804d33204139a1b59bbafa95822d0 100644 --- a/io_scene_gltf2/blender/imp/gltf2_blender_texture.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_texture.py @@ -18,6 +18,7 @@ from .gltf2_blender_image import BlenderImage from ..com.gltf2_blender_conversion import texture_transform_gltf_to_blender from io_scene_gltf2.io.com.gltf2_io import Sampler from io_scene_gltf2.io.com.gltf2_io_constants import TextureFilter, TextureWrap +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions def texture( mh, @@ -31,6 +32,9 @@ def texture( """Creates nodes for a TextureInfo and hooks up the color/alpha outputs.""" x, y = location pytexture = mh.gltf.data.textures[tex_info.index] + + import_user_extensions('gather_import_texture_before_hook', mh.gltf, pytexture, mh, tex_info, location, label, color_socket, alpha_socket, is_data) + if pytexture.sampler is not None: pysampler = mh.gltf.data.samplers[pytexture.sampler] else: @@ -166,6 +170,8 @@ def texture( # Outputs mh.node_tree.links.new(uv_socket, uv_map.outputs[0]) + import_user_extensions('gather_import_texture_after_hook', mh.gltf, pytexture, mh.node_tree, mh, tex_info, location, label, color_socket, alpha_socket, is_data) + def set_filtering(tex_img, pysampler): """Set the filtering/interpolation on an Image Texture from the glTf sampler.""" minf = pysampler.min_filter diff --git a/io_scene_gltf2/io/imp/gltf2_io_gltf.py b/io_scene_gltf2/io/imp/gltf2_io_gltf.py index aa9bda38346d60055c7d635dc757924981996c3c..407afccd1bc694c8c7afb43a444c1b27d3eb719d 100755 --- a/io_scene_gltf2/io/imp/gltf2_io_gltf.py +++ b/io_scene_gltf2/io/imp/gltf2_io_gltf.py @@ -38,6 +38,7 @@ class glTFImporter(): self.buffers = {} self.accessor_cache = {} self.decode_accessor_cache = {} + self.import_user_extensions = import_settings['import_user_extensions'] if 'loglevel' not in self.import_settings.keys(): self.import_settings['loglevel'] = logging.ERROR @@ -57,6 +58,13 @@ class glTFImporter(): 'KHR_draco_mesh_compression' ] + # Add extensions required supported by custom import extensions + for import_extension in self.import_user_extensions: + if hasattr(import_extension, "extensions"): + for custom_extension in import_extension.extensions: + if custom_extension.required: + self.extensions_managed.append(custom_extension.name) + @staticmethod def load_json(content): def bad_constant(val): diff --git a/io_scene_gltf2/io/imp/gltf2_io_user_extensions.py b/io_scene_gltf2/io/imp/gltf2_io_user_extensions.py new file mode 100644 index 0000000000000000000000000000000000000000..0c43925154c300075ec44606a5c895bbbc28fa8d --- /dev/null +++ b/io_scene_gltf2/io/imp/gltf2_io_user_extensions.py @@ -0,0 +1,23 @@ +# 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 import_user_extensions(hook_name, gltf_importer, *args): + for extension in gltf_importer.import_user_extensions: + hook = getattr(extension, hook_name, None) + if hook is not None: + try: + hook(*args, gltf_importer) + except Exception as e: + print(hook_name, "fails on", extension) + print(str(e))