From aa62fd47ea7d410c9e203f9b23a66e5dcd0bdb3d Mon Sep 17 00:00:00 2001 From: William Herald Snyder Date: Sat, 28 Nov 2020 21:42:27 -0500 Subject: [PATCH] Vertex groups importing properly. ENVL indicies refer to each model's MNDX, which start from 1, not 0/don't necessarily refer to order in file... --- addons/io_scene_swbf_msh/msh_scene_read.py | 41 ++++++++++++++++++---- addons/io_scene_swbf_msh/msh_to_blend.py | 37 +++++++++++++------ 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/addons/io_scene_swbf_msh/msh_scene_read.py b/addons/io_scene_swbf_msh/msh_scene_read.py index b4d6462..a2563f9 100644 --- a/addons/io_scene_swbf_msh/msh_scene_read.py +++ b/addons/io_scene_swbf_msh/msh_scene_read.py @@ -12,6 +12,8 @@ from .crc import * envls = [] +model_counter = 0 + def read_scene(input_file) -> Scene: scene = Scene() @@ -71,6 +73,11 @@ def read_scene(input_file) -> Scene: else: with hedr.read_child() as null: pass + + for envl in envls: + #print("Envelope: ") + for index in envl: + pass#print("\t" + scene.models[index].name) return scene @@ -149,7 +156,10 @@ def _read_modl(modl: Reader, materials_list: List[Material]) -> Model: elif "MNDX" in next_header: with modl.read_child() as mndx: - pass + index = mndx.read_u32() + global model_counter + print("Encountered model {} with index {}".format(model_counter, index)) + model_counter += 1 elif "NAME" in next_header: with modl.read_child() as name: @@ -169,7 +179,10 @@ def _read_modl(modl: Reader, materials_list: List[Material]) -> Model: elif "GEOM" in next_header: model.geometry = [] + envelope = [] + with modl.read_child() as geom: + while geom.could_have_child(): next_header_geom = geom.peak_next_header() @@ -179,14 +192,22 @@ def _read_modl(modl: Reader, materials_list: List[Material]) -> Model: elif "ENVL" in next_header_geom: with geom.read_child() as envl: - global envls num_indicies = envl.read_u32() - print("reading ENVL with " + str(num_indicies) + " indicies") - envls += [envl.read_u32() for _ in range(num_indicies)] + envelope += [envl.read_u32() - 1 for _ in range(num_indicies)] else: with geom.read_child() as null: pass + if envelope: + global envls + envls.append(envelope) + + for seg in model.geometry: + if seg.weights: + for weight_set in seg.weights: + for i in range(len(weight_set)): + weight = weight_set[i] + weight_set[i] = (envelope[weight[0]], weight[1]) elif "SWCI" in next_header: prim = CollisionPrimitive() @@ -282,24 +303,30 @@ 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: - pass - ''' + 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: pass diff --git a/addons/io_scene_swbf_msh/msh_to_blend.py b/addons/io_scene_swbf_msh/msh_to_blend.py index a768cc5..49be9f4 100644 --- a/addons/io_scene_swbf_msh/msh_to_blend.py +++ b/addons/io_scene_swbf_msh/msh_to_blend.py @@ -41,7 +41,7 @@ def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map): bone_children = [b for b in get_model_children(bone, refined_skeleton)] - if len(bone_children) > 0: + if bone_children: edit_bone.tail = Vector((0.0,0.0,0.0)) for bone_child in bone_children: edit_bone.tail += model_map[bone_child.name].matrix_world.translation @@ -50,6 +50,7 @@ def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map): 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() @@ -121,12 +122,6 @@ def extract_models(scene: Scene, materials_map): for model in sort_by_parent(scene.models): new_obj = None - if "bone_l_toe" in model.name: - loc = get_model_world_matrix(model, scene.models).translation - print("World bone_l_toe: " + str(loc)) - - - if model.name.startswith("p_") or "collision" in model.name or model.name.startswith("c_") or model.name.startswith("sv_"): continue @@ -141,6 +136,8 @@ def extract_models(scene: Scene, materials_map): full_texcoords = [] + weights_offsets = {} + for i,seg in enumerate(model.geometry): if i == 0: @@ -148,6 +145,9 @@ def extract_models(scene: Scene, materials_map): verts += [tuple(convert_vector_space(v)) for v in seg.positions] + if seg.weights: + weights_offsets[offset] = seg.weights + if seg.texcoords is not None: full_texcoords += seg.texcoords else: @@ -162,7 +162,7 @@ def extract_models(scene: Scene, materials_map): new_mesh.validate() - if len(full_texcoords) > 0: + if full_texcoords: edit_mesh = bmesh.new() edit_mesh.from_mesh(new_mesh) @@ -180,10 +180,21 @@ def extract_models(scene: Scene, materials_map): edit_mesh.to_mesh(new_mesh) edit_mesh.free() - - new_obj = bpy.data.objects.new(new_mesh.name, new_mesh) + + 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] + + if index not in vertex_groups_indicies: + model_name = scene.models[index].name + vertex_groups_indicies[index] = new_obj.vertex_groups.new(name=model_name) + + vertex_groups_indicies[index].add([offset + i], weight[1], 'ADD') + ''' Assign Materials - will do per segment later... ''' @@ -250,6 +261,12 @@ def extract_scene(filepath: str, scene: Scene): 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) + model_map = extract_models(scene, matmap)