diff --git a/model_import.py b/model_import.py index 0d6ebdb..a50eb1a 100644 --- a/model_import.py +++ b/model_import.py @@ -6,7 +6,7 @@ sys.path.append(r"F:\PYTHON_STUFF\blender_wmd") from PyWMD.byte_io_wmd import ByteIO -from PyWMD.pragma_model import PragmaModel, PragmaBone +from PyWMD.pragma_model import PragmaModel, PragmaBone, PragmaMeshV24Plus import bpy from mathutils import Vector, Quaternion, Matrix @@ -22,6 +22,7 @@ def pose_bone(root_bone: PragmaBone, armature): bl_bone = armature.pose.bones.get(root_bone.name) pos = Vector(root_bone.position.values) rot = Quaternion(root_bone.rotation.values) + print(f'Posing "{root_bone.name}" POS:{pos} ROT:{rot}') mat = Matrix.Translation(pos) @ rot.to_matrix().to_4x4() bl_bone.matrix_basis.identity() if bl_bone.parent: @@ -57,17 +58,40 @@ def create_armature(model: PragmaModel, collection): for bone in model.armature.bones: # type: PragmaBone bl_bone = armature.edit_bones.get(bone.name) - bl_bone.tail = Vector([0, 0, 1]) + bl_bone.head + bl_bone.tail = Vector([1, 0, 0]) + bl_bone.head - bpy.ops.object.mode_set(mode='POSE') - for root_bone in model.armature.roots: - pose_bone(root_bone, armature_obj) + pos = Vector(bone.position.values) + rot = Quaternion(bone.rotation.values) + bl_bone.transform(Matrix.Translation(pos) @ rot.to_matrix().to_4x4()) - bpy.ops.pose.armature_apply() bpy.ops.object.mode_set(mode='OBJECT') return armature_obj +def create_model(model, armature, collection): + for bodygroup_name, bodygroup in model.mesh.bodygroups.items(): + bodygroup_collection = bpy.data.collections.new("BP_" + bodygroup_name) + collection.children.link(bodygroup_collection) + for group in bodygroup: + group_collection = bpy.data.collections.new(group.name) + bodygroup_collection.children.link(group_collection) + if len(group.sub_meshes) == 0: + continue + for sub_mesh in group.sub_meshes: + mesh_obj = bpy.data.objects.new(group.name, bpy.data.meshes.new('{}_MESH'.format(group.name))) + mesh_obj.parent = armature + group_collection.objects.link(mesh_obj) + modifier = mesh_obj.modifiers.new( + type="ARMATURE", name="Armature") + modifier.object = armature + + mesh_data = mesh_obj.data + mesh_data.from_pydata(sub_mesh.vertices, [], sub_mesh.indices) + mesh_data.update() + mesh_data.use_auto_smooth = True + mesh_data.normals_split_custom_set_from_vertices (sub_mesh.normals) + + def import_model(model_path: str): model_path = Path(model_path) reader = ByteIO(path=model_path) @@ -77,8 +101,8 @@ def import_model(model_path: str): model.set_name(model_path.stem) main_collection = bpy.data.collections.new(model_path.stem) bpy.context.scene.collection.children.link(main_collection) - create_armature(model, main_collection) - pass + armature = create_armature(model, main_collection) + create_model(model, armature, main_collection) if __name__ == '__main__':