From 8a20c3813225adb545cb536dfd3cfce9bdd711b4 Mon Sep 17 00:00:00 2001 From: William Herald Snyder Date: Fri, 11 Dec 2020 11:50:03 -0500 Subject: [PATCH] Animation batch import --- addons/io_scene_swbf_msh/__init__.py | 24 ++++++--- addons/io_scene_swbf_msh/msh_to_blend.py | 69 ++++++------------------ 2 files changed, 31 insertions(+), 62 deletions(-) diff --git a/addons/io_scene_swbf_msh/__init__.py b/addons/io_scene_swbf_msh/__init__.py index 387c0f2..5e0d95f 100644 --- a/addons/io_scene_swbf_msh/__init__.py +++ b/addons/io_scene_swbf_msh/__init__.py @@ -54,7 +54,7 @@ if "bpy" in locals(): import bpy from bpy_extras.io_utils import ExportHelper, ImportHelper -from bpy.props import BoolProperty, EnumProperty +from bpy.props import BoolProperty, EnumProperty, CollectionProperty from bpy.types import Operator from .msh_scene import create_scene from .msh_scene_save import save_scene @@ -145,6 +145,11 @@ class ImportMSH(Operator, ImportHelper): bl_label = "Import SWBF .msh File" filename_ext = ".msh" + files: CollectionProperty( + name="File Path", + type=bpy.types.OperatorFileListElement, + ) + filter_glob: StringProperty( default="*.msh", options={'HIDDEN'}, @@ -159,13 +164,16 @@ class ImportMSH(Operator, ImportHelper): def execute(self, context): - with open(self.filepath, 'rb') as input_file: - scene = read_scene(input_file, self.animation_only) - - if not self.animation_only: - extract_scene(self.filepath, scene) - else: - extract_and_apply_anim(self.filepath, scene) + dirname = os.path.dirname(self.filepath) + for file in self.files: + filepath = os.path.join(dirname, file.name) + with open(filepath, 'rb') as input_file: + scene = read_scene(input_file, self.animation_only) + + if not self.animation_only: + extract_scene(filepath, scene) + else: + extract_and_apply_anim(filepath, scene) return {'FINISHED'} diff --git a/addons/io_scene_swbf_msh/msh_to_blend.py b/addons/io_scene_swbf_msh/msh_to_blend.py index cf65bb2..93fd4c7 100644 --- a/addons/io_scene_swbf_msh/msh_to_blend.py +++ b/addons/io_scene_swbf_msh/msh_to_blend.py @@ -32,22 +32,13 @@ def extract_and_apply_anim(filename, scene): head, tail = os.path.split(filename) anim_name = tail.split(".")[0] action = bpy.data.actions.new(anim_name) + action.use_fake_user = True if not arma.animation_data: arma.animation_data_create() - bone_bind_poses = {} - bone_stack_mats = {} - - ''' - for bone in arma.data.bones: - local_mat = bone.matrix_local - if bone.parent: - local_mat = bone.parent.matrix_local.inverted() @ local_mat - bone_bind_poses[bone.name] = local_mat - ''' for bone in arma.data.bones: bone_obj = bpy.data.objects[bone.name] @@ -67,7 +58,6 @@ def extract_and_apply_anim(filename, scene): bone_bind_poses[bone.name] = bind_mat.inverted() @ stack_mat - for bone in arma.pose.bones: if crc(bone.name) in scene.animation.bone_frames: #print("Inserting anim data for bone: {}".format(bone.name)) @@ -80,10 +70,10 @@ def extract_and_apply_anim(filename, scene): rot_data_path = "pose.bones[\"{}\"].rotation_quaternion".format(bone.name) - fcurve_rot_w = action.fcurves.new(rot_data_path, index=0) - fcurve_rot_x = action.fcurves.new(rot_data_path, index=1) - fcurve_rot_y = action.fcurves.new(rot_data_path, index=2) - fcurve_rot_z = action.fcurves.new(rot_data_path, index=3) + fcurve_rot_w = action.fcurves.new(rot_data_path, index=0, action_group=bone.name) + fcurve_rot_x = action.fcurves.new(rot_data_path, index=1, action_group=bone.name) + fcurve_rot_y = action.fcurves.new(rot_data_path, index=2, action_group=bone.name) + fcurve_rot_z = action.fcurves.new(rot_data_path, index=3, action_group=bone.name) for frame in rotation_frames: i = frame.index @@ -95,9 +85,9 @@ def extract_and_apply_anim(filename, scene): fcurve_rot_z.keyframe_points.insert(i,q.z) - fcurve_loc_x = action.fcurves.new(loc_data_path, index=0) - fcurve_loc_y = action.fcurves.new(loc_data_path, index=1) - fcurve_loc_z = action.fcurves.new(loc_data_path, index=2) + fcurve_loc_x = action.fcurves.new(loc_data_path, index=0, action_group=bone.name) + fcurve_loc_y = action.fcurves.new(loc_data_path, index=1, action_group=bone.name) + fcurve_loc_z = action.fcurves.new(loc_data_path, index=2, action_group=bone.name) for frame in translation_frames: i = frame.index @@ -148,25 +138,22 @@ def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map): bone_obj = model_map[bone.name] - #edit_bone.head = bone_obj.matrix_world.translation - edit_bone.matrix = bone_obj.matrix_world edit_bone.tail = bone_obj.matrix_world @ Vector((0.0,1.0,0.0)) - #edit_bone.length = 1 - ''' bone_children = [b for b in get_model_children(bone, refined_skeleton)] - + tail_pos = Vector() if bone_children: - 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) + tail_pos += bone_obj.matrix_world.translation + tail_pos = tail_pos / len(bone_children) + edit_bone.length = .5 #(tail_pos - edit_bone.head).magnitude else: - edit_bone.tail = model_map[bone.name].matrix_world @ Vector((-0.2,0.0,0.0)) - ''' + bone_length = .5# edit_bone.parent.length if edit_bone.parent is not None else .5 + edit_bone.tail = bone_obj.matrix_world @ Vector((0.0,bone_length,0.0)) + bpy.ops.object.mode_set(mode='OBJECT') @@ -205,14 +192,6 @@ def extract_refined_skeleton(scene: Scene): refined_skeleton_models = [] - - ''' - for bone in skeleton_models: - - if bone.parent: - if - ''' - for bone in skeleton_models: @@ -226,7 +205,6 @@ def extract_refined_skeleton(scene: Scene): if crc(curr_ancestor.name) in scene.skeleton or curr_ancestor.name == scene.models[0].name: new_model = Model() new_model.name = bone.name - 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() @@ -417,23 +395,6 @@ def extract_scene(filepath: str, scene: Scene): loc, rot, _ = bone_local.decompose() - locdiff = obj_loc - loc - quatdiff = obj_rot - rot - - if quatdiff.magnitude > .01: - print("Big quat diff here") - print("\t{}: obj quat: {} bone quat: {}".format(bone.name, quat_to_str(obj_rot), quat_to_str(rot))) - - - #if locdiff.magnitude > .01: - # print("Big loc diff here") - # print("\t{}: obj loc: {} bone loc: {}".format(bone.name, vec_to_str(obj_loc), vec_to_str(loc))) - - - - - - reparent_obj = None