Merge branch 'mshread' into fully_featured

This commit is contained in:
Will Snyder 2020-12-03 18:38:35 -05:00
commit b2ad1cfa1a
2 changed files with 96 additions and 53 deletions

View File

@ -10,16 +10,18 @@ from .msh_utilities import *
from .crc import * from .crc import *
envls = []
model_counter = 0 model_counter = 0
def read_scene(input_file) -> Scene: def read_scene(input_file) -> Scene:
scene = Scene() scene = Scene()
scene.models = [] scene.models = []
scene.materials = {} scene.materials = {}
global model_counter
model_counter = 0
with Reader(file=input_file) as hedr: with Reader(file=input_file) as hedr:
while hedr.could_have_child(): while hedr.could_have_child():
@ -59,12 +61,6 @@ def read_scene(input_file) -> Scene:
with hedr.read_child() as skl2: with hedr.read_child() as skl2:
num_bones = skl2.read_u32() num_bones = skl2.read_u32()
scene.skeleton = [skl2.read_u32(5)[0] for i in range(num_bones)] 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: elif "ANM2" in next_header:
with hedr.read_child() as anm2: with hedr.read_child() as anm2:
@ -74,10 +70,12 @@ def read_scene(input_file) -> Scene:
with hedr.read_child() as null: with hedr.read_child() as null:
pass pass
for envl in envls: if scene.skeleton:
#print("Envelope: ") print("Skeleton models: ")
for index in envl: for model in scene.models:
pass#print("\t" + scene.models[index].name) if crc(model.name) in scene.skeleton:
print("\t" + model.name)
return scene return scene
@ -156,9 +154,9 @@ def _read_modl(modl: Reader, materials_list: List[Material]) -> Model:
elif "MNDX" in next_header: elif "MNDX" in next_header:
with modl.read_child() as mndx: with modl.read_child() as mndx:
index = mndx.read_u32()
global model_counter 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 model_counter += 1
elif "NAME" in next_header: elif "NAME" in next_header:
@ -198,9 +196,6 @@ def _read_modl(modl: Reader, materials_list: List[Material]) -> Model:
else: else:
with geom.read_child() as null: with geom.read_child() as null:
pass pass
if envelope:
global envls
envls.append(envelope)
for seg in model.geometry: for seg in model.geometry:
if seg.weights: if seg.weights:
@ -219,9 +214,11 @@ def _read_modl(modl: Reader, materials_list: List[Material]) -> Model:
model.collisionprimitive = prim model.collisionprimitive = prim
else: else:
with modl.read_child() as unknown: with modl.read_child() as null:
pass pass
print("Reading model " + model.name + " of type: " + str(model.model_type)[10:])
return model return model
@ -303,32 +300,24 @@ def _read_segm(segm: Reader, materials_list: List[Material]) -> GeometrySegment:
segm.skip_bytes(-2) segm.skip_bytes(-2)
elif "WGHT" in next_header: elif "WGHT" in next_header:
print("===================================")
with segm.read_child() as wght: with segm.read_child() as wght:
geometry_seg.weights = [] geometry_seg.weights = []
num_weights = wght.read_u32() num_weights = wght.read_u32()
for _ in range(num_weights): for _ in range(num_weights):
weight_set = [] weight_set = []
print_str = ""
for _ in range(4): for _ in range(4):
index = wght.read_u32() index = wght.read_u32()
value = wght.read_f32() value = wght.read_f32()
print_str += "({}, {}) ".format(index,value)
if value > 0.000001: if value > 0.000001:
weight_set.append((index,value)) weight_set.append((index,value))
#print(print_str)
geometry_seg.weights.append(weight_set) geometry_seg.weights.append(weight_set)
print("===================================")
else: else:
with segm.read_child() as unknown: with segm.read_child() as null:
pass pass
return geometry_seg return geometry_seg

View File

@ -18,6 +18,10 @@ import os
#def import_anim(scene : Scene):
def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map): def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map):
@ -42,7 +46,7 @@ def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map):
bone_children = [b for b in get_model_children(bone, refined_skeleton)] bone_children = [b for b in get_model_children(bone, refined_skeleton)]
if bone_children: 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: for bone_child in bone_children:
edit_bone.tail += model_map[bone_child.name].matrix_world.translation 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)
@ -50,11 +54,12 @@ 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)) edit_bone.tail = model_map[bone.name].matrix_world @ Vector((-0.2,0.0,0.0))
bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='OBJECT')
armature_obj.select_set(True) armature_obj.select_set(True)
bpy.context.view_layer.update() 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): def extract_refined_skeleton(scene: Scene):
model_dict = {} model_dict = {}
children_dict = {}
skeleton_models = [] skeleton_models = []
for model in scene.models: for model in scene.models:
model_dict[model.name] = model 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: for model in scene.models:
if model.parent:
children_dict[model.parent].append(model)
if crc(model.name) in scene.skeleton: if crc(model.name) in scene.skeleton:
skeleton_models.append(model) skeleton_models.append(model)
refined_skeleton_models = [] refined_skeleton_models = []
for bone in skeleton_models: for bone in skeleton_models:
@ -88,10 +101,11 @@ def extract_refined_skeleton(scene: Scene):
while True: 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 = Model()
new_model.name = bone.name 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() loc, rot, _ = stacked_transform.decompose()
@ -122,9 +136,6 @@ def extract_models(scene: Scene, materials_map):
for model in sort_by_parent(scene.models): for model in sort_by_parent(scene.models):
new_obj = None 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: if model.model_type == ModelType.STATIC or model.model_type == ModelType.SKIN:
new_mesh = bpy.data.meshes.new(model.name) 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) new_obj = bpy.data.objects.new(new_mesh.name, new_mesh)
for offset in weights_offsets:
vertex_groups_indicies = {} vertex_groups_indicies = {}
for offset in weights_offsets:
for i, weight_set in enumerate(weights_offsets[offset]): for i, weight_set in enumerate(weights_offsets[offset]):
for weight in weight_set: for weight in weight_set:
index = weight[0] index = weight[0]
@ -213,7 +225,6 @@ def extract_models(scene: Scene, materials_map):
new_obj.empty_display_type = 'PLAIN_AXES' new_obj.empty_display_type = 'PLAIN_AXES'
model_map[model.name] = new_obj model_map[model.name] = new_obj
if model.parent: if model.parent:
@ -225,6 +236,7 @@ def extract_models(scene: Scene, materials_map):
bpy.context.collection.objects.link(new_obj) bpy.context.collection.objects.link(new_obj)
return model_map 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): def extract_scene(filepath: str, scene: Scene):
folder = os.path.join(os.path.dirname(filepath),"") folder = os.path.join(os.path.dirname(filepath),"")
matmap = extract_materials(folder, 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) model_map = extract_models(scene, matmap)
skel = extract_refined_skeleton(scene) 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)