From 7db0591cc0f4c0b64ea37fc0809f5e5532ca7d82 Mon Sep 17 00:00:00 2001 From: William Herald Snyder Date: Sun, 22 Nov 2020 00:24:20 -0500 Subject: [PATCH] Armature extraction more robust (looks at skinning requirements, not just SKL2), automatic skin-skeleton reparenting, gave bone tails default epsilon position --- addons/io_scene_swbf_msh/msh_scene_read.py | 45 ++++----- addons/io_scene_swbf_msh/msh_to_blend.py | 104 ++++++++++++++++----- 2 files changed, 96 insertions(+), 53 deletions(-) diff --git a/addons/io_scene_swbf_msh/msh_scene_read.py b/addons/io_scene_swbf_msh/msh_scene_read.py index a2563f9..2ebf4d1 100644 --- a/addons/io_scene_swbf_msh/msh_scene_read.py +++ b/addons/io_scene_swbf_msh/msh_scene_read.py @@ -10,16 +10,18 @@ from .msh_utilities import * from .crc import * -envls = [] - model_counter = 0 + def read_scene(input_file) -> Scene: scene = Scene() scene.models = [] scene.materials = {} + global model_counter + model_counter = 0 + with Reader(file=input_file) as hedr: while hedr.could_have_child(): @@ -59,13 +61,7 @@ def read_scene(input_file) -> Scene: with hedr.read_child() as skl2: num_bones = skl2.read_u32() scene.skeleton = [skl2.read_u32(5)[0] for i in range(num_bones)] - #print("Skeleton models: ") - for crc_hash in scene.skeleton: - for model in scene.models: - if crc_hash == crc(model.name): - pass - #print("\t" + model.name + " with type: " + str(model.model_type)) - + elif "ANM2" in next_header: with hedr.read_child() as anm2: _read_anm2(anm2, scene.models) @@ -74,10 +70,12 @@ def read_scene(input_file) -> Scene: with hedr.read_child() as null: pass - for envl in envls: - #print("Envelope: ") - for index in envl: - pass#print("\t" + scene.models[index].name) + if scene.skeleton: + print("Skeleton models: ") + for model in scene.models: + if crc(model.name) in scene.skeleton: + print("\t" + model.name) + return scene @@ -156,9 +154,9 @@ def _read_modl(modl: Reader, materials_list: List[Material]) -> Model: elif "MNDX" in next_header: with modl.read_child() as mndx: - index = mndx.read_u32() global model_counter - print("Encountered model {} with index {}".format(model_counter, index)) + if mndx.read_u32() - 1 != model_counter: + print("MODEL INDEX DIDNT MATCH COUNTER!") model_counter += 1 elif "NAME" in next_header: @@ -198,9 +196,6 @@ def _read_modl(modl: Reader, materials_list: List[Material]) -> Model: else: with geom.read_child() as null: pass - if envelope: - global envls - envls.append(envelope) for seg in model.geometry: if seg.weights: @@ -219,9 +214,11 @@ def _read_modl(modl: Reader, materials_list: List[Material]) -> Model: model.collisionprimitive = prim else: - with modl.read_child() as unknown: + with modl.read_child() as null: pass + print("Reading model " + model.name + " of type: " + str(model.model_type)[10:]) + return model @@ -303,32 +300,24 @@ def _read_segm(segm: Reader, materials_list: List[Material]) -> GeometrySegment: segm.skip_bytes(-2) elif "WGHT" in next_header: - print("===================================") with segm.read_child() as wght: geometry_seg.weights = [] - num_weights = wght.read_u32() for _ in range(num_weights): weight_set = [] - print_str = "" for _ in range(4): index = wght.read_u32() value = wght.read_f32() - print_str += "({}, {}) ".format(index,value) - if value > 0.000001: weight_set.append((index,value)) - #print(print_str) - geometry_seg.weights.append(weight_set) - print("===================================") else: - with segm.read_child() as unknown: + with segm.read_child() as null: pass return geometry_seg diff --git a/addons/io_scene_swbf_msh/msh_to_blend.py b/addons/io_scene_swbf_msh/msh_to_blend.py index 49be9f4..dc1ed09 100644 --- a/addons/io_scene_swbf_msh/msh_to_blend.py +++ b/addons/io_scene_swbf_msh/msh_to_blend.py @@ -18,6 +18,10 @@ import os +#def import_anim(scene : Scene): + + + def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map): @@ -42,19 +46,20 @@ def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map): bone_children = [b for b in get_model_children(bone, refined_skeleton)] if bone_children: - edit_bone.tail = Vector((0.0,0.0,0.0)) + edit_bone.tail = Vector((-0.00001,0.0,0.0)) for bone_child in bone_children: edit_bone.tail += model_map[bone_child.name].matrix_world.translation - edit_bone.tail = edit_bone.tail / len(bone_children) + edit_bone.tail = edit_bone.tail / len(bone_children) else: edit_bone.tail = model_map[bone.name].matrix_world @ Vector((-0.2,0.0,0.0)) - bpy.ops.object.mode_set(mode='OBJECT') armature_obj.select_set(True) bpy.context.view_layer.update() + return armature_obj + @@ -63,20 +68,28 @@ def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map): def extract_refined_skeleton(scene: Scene): model_dict = {} - children_dict = {} skeleton_models = [] + for model in scene.models: model_dict[model.name] = model - children_dict[model.name] = [] + if model.geometry: + for seg in model.geometry: + if seg.weights: + for weight_set in seg.weights: + for weight in weight_set: + model_weighted_to = scene.models[weight[0]] + + if crc(model_weighted_to.name) not in scene.skeleton: + scene.skeleton.append(crc(model_weighted_to.name)) + print("Found additional bone: " + model_weighted_to.name) + for model in scene.models: - if model.parent: - children_dict[model.parent].append(model) - if crc(model.name) in scene.skeleton: skeleton_models.append(model) + refined_skeleton_models = [] for bone in skeleton_models: @@ -88,10 +101,11 @@ def extract_refined_skeleton(scene: Scene): while True: - if crc(curr_ancestor.name) in scene.skeleton or "dummyroot" in curr_ancestor.name.lower(): + if crc(curr_ancestor.name) in scene.skeleton or curr_ancestor.name == scene.models[0].name: new_model = Model() new_model.name = bone.name - new_model.parent = curr_ancestor.name if "dummyroot" not in curr_ancestor.name.lower() else "" + print("Adding {} to refined skeleton...".format(bone.name)) + new_model.parent = curr_ancestor.name if curr_ancestor.name != scene.models[0].name else "" loc, rot, _ = stacked_transform.decompose() @@ -122,9 +136,6 @@ def extract_models(scene: Scene, materials_map): for model in sort_by_parent(scene.models): new_obj = None - if model.name.startswith("p_") or "collision" in model.name or model.name.startswith("c_") or model.name.startswith("sv_"): - continue - if model.model_type == ModelType.STATIC or model.model_type == ModelType.SKIN: new_mesh = bpy.data.meshes.new(model.name) @@ -183,8 +194,9 @@ def extract_models(scene: Scene, materials_map): new_obj = bpy.data.objects.new(new_mesh.name, new_mesh) + vertex_groups_indicies = {} + for offset in weights_offsets: - vertex_groups_indicies = {} for i, weight_set in enumerate(weights_offsets[offset]): for weight in weight_set: index = weight[0] @@ -213,7 +225,6 @@ def extract_models(scene: Scene, materials_map): new_obj.empty_display_type = 'PLAIN_AXES' - model_map[model.name] = new_obj if model.parent: @@ -225,6 +236,7 @@ def extract_models(scene: Scene, materials_map): bpy.context.collection.objects.link(new_obj) + return model_map @@ -258,20 +270,62 @@ def extract_materials(folder_path: str, scene: Scene) -> Dict[str,bpy.types.Mate def extract_scene(filepath: str, scene: Scene): folder = os.path.join(os.path.dirname(filepath),"") - - matmap = extract_materials(folder,scene) - - if scene.skeleton: - #print("Skeleton models: ") - for model in scene.models: - if crc(model.name) in scene.skeleton: - pass#print("\tName: " + model.name + " Parent: " + model.parent) + matmap = extract_materials(folder, scene) model_map = extract_models(scene, matmap) - skel = extract_refined_skeleton(scene) - refined_skeleton_to_armature(skel, model_map) + armature = refined_skeleton_to_armature(skel, model_map) + + for model in scene.models: + reparent_obj = None + if model.model_type == ModelType.SKIN: + + if model.parent: + reparent_obj = model_map[model.parent] + + skin_obj = model_map[model.name] + skin_obj.select_set(True) + armature.select_set(True) + bpy.context.view_layer.objects.active = armature + + bpy.ops.object.parent_clear(type='CLEAR') + bpy.ops.object.parent_set(type='ARMATURE') + + skin_obj.select_set(False) + armature.select_set(False) + bpy.context.view_layer.objects.active = None + + if reparent_obj is not None: + armature.select_set(True) + reparent_obj.select_set(True) + bpy.context.view_layer.objects.active = reparent_obj + bpy.ops.object.parent_set(type='OBJECT') + + armature.select_set(False) + reparent_obj.select_set(False) + bpy.context.view_layer.objects.active = None + + + if armature is not None: + for bone in armature.data.bones: + model_map[bone.name].select_set(True) + bpy.ops.object.delete() + + for model in scene.models: + if model.name in bpy.data.objects: + if model.hidden and len(bpy.data.objects[model.name].children) == 0: + bpy.data.objects[model.name].hide_set(True) + + + + + + + + + +