diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py index edac58bd769dca3bd4a2c1d35db4a233e9c5c161..0d388d9705a9a63c43c3664dee2e99c8f807c14d 100755 --- a/io_scene_gltf2/__init__.py +++ b/io_scene_gltf2/__init__.py @@ -4,7 +4,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": (3, 3, 2), + "version": (3, 3, 3), 'blender': (3, 3, 0), 'location': 'File > Import-Export', 'description': 'Import-Export as glTF 2.0', diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_animation.py b/io_scene_gltf2/blender/imp/gltf2_blender_animation.py index c0dcd84e2672caa3f0968e9c24c39d2dfd0d0a9d..76fe02c5d97dfd0b8c99c78b25d30f2c3e1ec3f8 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_animation.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation.py @@ -5,7 +5,7 @@ from .gltf2_blender_animation_node import BlenderNodeAnim from .gltf2_blender_animation_weight import BlenderWeightAnim from .gltf2_blender_animation_utils import simulate_stash, restore_animation_on_object from .gltf2_blender_vnode import VNode - +from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions class BlenderAnimation(): """Dispatch Animation to node or morph weights animation.""" @@ -20,6 +20,8 @@ class BlenderAnimation(): # Things we need to stash when we're done. gltf.needs_stash = [] + import_user_extensions('gather_import_animation_before_hook', gltf, anim_idx) + for vnode_id in gltf.vnodes: if isinstance(vnode_id, int): BlenderNodeAnim.anim(gltf, anim_idx, vnode_id) @@ -30,6 +32,8 @@ class BlenderAnimation(): for (obj, action) in gltf.needs_stash: simulate_stash(obj, track_name, action) + import_user_extensions('gather_import_animation_after_hook', gltf, anim_idx, track_name) + @staticmethod def restore_animation(gltf, animation_name): """Restores the actions for an animation by its track 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 acc7767fd5e006e8c242d058b1a7687f22afc534..ab2f496c2c85fadcb0d2cf9b3c7a608cc87a96b5 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py @@ -20,6 +20,7 @@ class BlenderNodeAnim(): """Manage animation targeting a node's TRS.""" animation = gltf.data.animations[anim_idx] node = gltf.data.nodes[node_idx] + if anim_idx not in node.animations.keys(): return diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py b/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py index 384cae7d0c75bf5616a3ed9b235b22cfa8d49f02..a3c1bd349b1e15b82c6d6fa65a51b9d306a43b54 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py @@ -58,6 +58,16 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob): """Put all primitive data into the mesh.""" pymesh = gltf.data.meshes[mesh_idx] + # Use a class here, to be able to pass data by reference to hook (to be able to change them inside hook) + class IMPORT_mesh_options: + def __init__(self, skinning: bool = True, skin_into_bind_pose: bool = True, use_auto_smooth: bool = True): + self.skinning = skinning + self.skin_into_bind_pose = skin_into_bind_pose + self.use_auto_smooth = use_auto_smooth + + mesh_options = IMPORT_mesh_options() + import_user_extensions('gather_import_mesh_options', gltf, mesh_options, pymesh, skin_idx) + # Scan the primitives to find out what we need to create has_normals = False @@ -135,6 +145,8 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob): print_console('INFO', 'Draco Decoder: Decode primitive {}'.format(pymesh.name or '[unnamed]')) decode_primitive(gltf, prim) + import_user_extensions('gather_import_decode_primitive', gltf, pymesh, prim, skin_idx) + if prim.indices is not None: indices = BinaryData.decode_accessor(gltf, prim.indices) indices = indices.reshape(len(indices)) @@ -240,7 +252,7 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob): for sk_locs in sk_vert_locs: gltf.locs_batch_gltf_to_blender(sk_locs) - if num_joint_sets: + if num_joint_sets and mesh_options.skin_into_bind_pose: skin_into_bind_pose( gltf, skin_idx, vert_joints, vert_weights, locs=[vert_locs] + sk_vert_locs, @@ -286,14 +298,14 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob): if layer is None: print("WARNING: Vertex colors are ignored because the maximum number of vertex color layers has been " - "reached.") + "reached.") break mesh.color_attributes[layer.name].data.foreach_set('color', squish(loop_cols[col_i])) # Skinning # TODO: this is slow :/ - if num_joint_sets: + if num_joint_sets and mesh_options.skinning: pyskin = gltf.data.skins[skin_idx] for i, node_idx in enumerate(pyskin.joints): bone = gltf.vnodes[node_idx] @@ -340,7 +352,7 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob): if prim.material is not None: # Get the material pymaterial = gltf.data.materials[prim.material] - vertex_color = 'COLOR_0' if 'COLOR_0' in prim.attributes else None + vertex_color = 'COLOR_0' if ('COLOR_0' in prim.attributes) else None if vertex_color not in pymaterial.blender_material: BlenderMaterial.create(gltf, prim.material, vertex_color) material_name = pymaterial.blender_material[vertex_color] @@ -374,7 +386,7 @@ def do_primitives(gltf, mesh_idx, skin_idx, mesh, ob): if has_normals: mesh.create_normals_split() mesh.normals_split_custom_set_from_vertices(vert_normals) - mesh.use_auto_smooth = True + mesh.use_auto_smooth = mesh_options.use_auto_smooth def points_edges_tris(mode, indices): diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py index 672b68c8146b4b615c9f26310f2aeaf422eba912..d853aded848cf309de307e348cd9701eeb4f715a 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py @@ -57,6 +57,15 @@ class BlenderScene(): @staticmethod def create_animations(gltf): """Create animations.""" + + # Use a class here, to be able to pass data by reference to hook (to be able to change them inside hook) + class IMPORT_animation_options: + def __init__(self, restore_first_anim: bool = True): + self.restore_first_anim = restore_first_anim + + animation_options = IMPORT_animation_options() + import_user_extensions('gather_import_animations', gltf, gltf.data.animations, animation_options) + if gltf.data.animations: # NLA tracks are added bottom to top, so create animations in # reverse so the first winds up on top @@ -64,8 +73,9 @@ class BlenderScene(): BlenderAnimation.anim(gltf, anim_idx) # Restore first animation - anim_name = gltf.data.animations[0].track_name - BlenderAnimation.restore_animation(gltf, anim_name) + if animation_options.restore_first_anim: + anim_name = gltf.data.animations[0].track_name + BlenderAnimation.restore_animation(gltf, anim_name) @staticmethod def select_imported_objects(gltf):