2020-10-30 20:59:54 +00:00
|
|
|
""" Gathers the Blender objects from the current scene and returns them as a list of
|
|
|
|
Model objects. """
|
|
|
|
|
|
|
|
import bpy
|
|
|
|
import bmesh
|
|
|
|
import math
|
2022-01-18 20:16:49 +00:00
|
|
|
|
2020-10-30 20:59:54 +00:00
|
|
|
from enum import Enum
|
|
|
|
from typing import List, Set, Dict, Tuple
|
2022-01-18 20:16:49 +00:00
|
|
|
|
2020-10-30 20:59:54 +00:00
|
|
|
from .msh_scene import Scene
|
2022-01-11 19:02:17 +00:00
|
|
|
from .msh_material_to_blend import *
|
2020-10-30 20:59:54 +00:00
|
|
|
from .msh_model import *
|
2022-01-18 20:16:49 +00:00
|
|
|
from .msh_skeleton_utilities import *
|
|
|
|
from .msh_model_gather import get_is_model_hidden
|
2022-09-19 18:39:02 +00:00
|
|
|
from .msh_mesh_to_blend import model_to_mesh_object
|
2022-01-18 20:16:49 +00:00
|
|
|
|
|
|
|
|
2020-11-01 16:48:07 +00:00
|
|
|
from .crc import *
|
2020-10-30 20:59:54 +00:00
|
|
|
|
2020-11-01 23:11:26 +00:00
|
|
|
import os
|
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
|
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# Extracts and applies anims in the scene to the currently selected armature
|
|
|
|
def extract_and_apply_anim(filename : str, scene : Scene):
|
2020-12-04 06:22:58 +00:00
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
arma = bpy.context.view_layer.objects.active
|
2020-12-04 06:22:58 +00:00
|
|
|
|
2022-01-18 20:46:30 +00:00
|
|
|
if not arma or arma.type != 'ARMATURE':
|
2020-12-11 04:47:45 +00:00
|
|
|
raise Exception("Select an armature to attach the imported animation to!")
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
if scene.animation is None:
|
|
|
|
raise Exception("No animation found in msh file!")
|
|
|
|
|
|
|
|
else:
|
|
|
|
head, tail = os.path.split(filename)
|
|
|
|
anim_name = tail.split(".")[0]
|
2022-01-28 22:28:12 +00:00
|
|
|
|
|
|
|
if anim_name in bpy.data.actions:
|
|
|
|
bpy.data.actions.remove(bpy.data.actions[anim_name], do_unlink=True)
|
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
action = bpy.data.actions.new(anim_name)
|
2020-12-11 16:50:03 +00:00
|
|
|
action.use_fake_user = True
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
if not arma.animation_data:
|
|
|
|
arma.animation_data_create()
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-06 05:19:58 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# Record the starting transforms of each bone. Pose space is relative
|
|
|
|
# to bones starting transforms. Starting = in edit mode
|
2020-12-11 04:47:45 +00:00
|
|
|
bone_bind_poses = {}
|
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
bpy.context.view_layer.objects.active = arma
|
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
2020-12-11 04:47:45 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
for edit_bone in arma.data.edit_bones:
|
|
|
|
if edit_bone.parent:
|
|
|
|
bone_local = edit_bone.parent.matrix.inverted() @ edit_bone.matrix
|
|
|
|
else:
|
|
|
|
bone_local = arma.matrix_local @ edit_bone.matrix
|
2020-12-11 04:47:45 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
bone_bind_poses[edit_bone.name] = bone_local.inverted()
|
2020-12-11 04:47:45 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
2020-12-06 05:19:58 +00:00
|
|
|
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
for bone in arma.pose.bones:
|
2021-01-07 06:25:09 +00:00
|
|
|
if to_crc(bone.name) in scene.animation.bone_frames:
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
bind_mat = bone_bind_poses[bone.name]
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2021-01-07 06:25:09 +00:00
|
|
|
translation_frames, rotation_frames = scene.animation.bone_frames[to_crc(bone.name)]
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
loc_data_path = "pose.bones[\"{}\"].location".format(bone.name)
|
|
|
|
rot_data_path = "pose.bones[\"{}\"].rotation_quaternion".format(bone.name)
|
2020-12-06 05:19:58 +00:00
|
|
|
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-11 16:50:03 +00:00
|
|
|
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)
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
for frame in rotation_frames:
|
|
|
|
i = frame.index
|
|
|
|
q = (bind_mat @ convert_rotation_space(frame.rotation).to_matrix().to_4x4()).to_quaternion()
|
2020-12-06 05:19:58 +00:00
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
fcurve_rot_w.keyframe_points.insert(i,q.w)
|
|
|
|
fcurve_rot_x.keyframe_points.insert(i,q.x)
|
|
|
|
fcurve_rot_y.keyframe_points.insert(i,q.y)
|
|
|
|
fcurve_rot_z.keyframe_points.insert(i,q.z)
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-11 16:50:03 +00:00
|
|
|
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)
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
for frame in translation_frames:
|
|
|
|
i = frame.index
|
|
|
|
t = (bind_mat @ Matrix.Translation(convert_vector_space(frame.translation))).translation
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
fcurve_loc_x.keyframe_points.insert(i,t.x)
|
|
|
|
fcurve_loc_y.keyframe_points.insert(i,t.y)
|
|
|
|
fcurve_loc_z.keyframe_points.insert(i,t.z)
|
|
|
|
|
|
|
|
arma.animation_data.action = action
|
2020-12-04 06:22:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
# Create the msh hierachy. Armatures are not created here. Much of this could use some optimization...
|
2022-01-21 04:40:19 +00:00
|
|
|
|
|
|
|
# TODO: Replace with an approach informed by existing Blender addons (io_scene_obj e.g.)
|
2022-01-07 18:45:50 +00:00
|
|
|
def extract_models(scene: Scene, materials_map : Dict[str, bpy.types.Material]) -> Dict[str, bpy.types.Object]:
|
2020-11-26 04:10:14 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# This will be filled with model names -> Blender objects and returned
|
|
|
|
model_map : Dict[str, bpy.types.Object] = {}
|
2020-10-30 20:59:54 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
sorted_models : List[Model] = sort_by_parent(scene.models)
|
2020-10-30 20:59:54 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
for model in sorted_models:
|
2022-09-19 18:39:02 +00:00
|
|
|
|
2020-11-01 23:11:26 +00:00
|
|
|
new_obj = None
|
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
if model.model_type == ModelType.STATIC or model.model_type == ModelType.SKIN or model.model_type == ModelType.SHADOWVOLUME:
|
2020-10-30 20:59:54 +00:00
|
|
|
|
2022-09-19 18:39:02 +00:00
|
|
|
new_obj = model_to_mesh_object(model, scene, materials_map)
|
2022-01-18 20:16:49 +00:00
|
|
|
|
2020-11-01 16:48:07 +00:00
|
|
|
else:
|
|
|
|
|
|
|
|
new_obj = bpy.data.objects.new(model.name, None)
|
|
|
|
new_obj.empty_display_size = 1
|
|
|
|
new_obj.empty_display_type = 'PLAIN_AXES'
|
2020-10-30 20:59:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
model_map[model.name] = new_obj
|
2022-01-28 22:28:12 +00:00
|
|
|
new_obj.name = model.name
|
2020-10-30 20:59:54 +00:00
|
|
|
|
|
|
|
if model.parent:
|
|
|
|
new_obj.parent = model_map[model.parent]
|
|
|
|
|
|
|
|
new_obj.location = convert_vector_space(model.transform.translation)
|
|
|
|
new_obj.rotation_mode = "QUATERNION"
|
|
|
|
new_obj.rotation_quaternion = convert_rotation_space(model.transform.rotation)
|
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
if model.collisionprimitive is not None:
|
|
|
|
new_obj.swbf_msh_coll_prim.prim_type = model.collisionprimitive.shape.value
|
|
|
|
|
2020-10-30 20:59:54 +00:00
|
|
|
bpy.context.collection.objects.link(new_obj)
|
|
|
|
|
2020-11-22 05:24:20 +00:00
|
|
|
|
2020-11-26 04:10:14 +00:00
|
|
|
return model_map
|
|
|
|
|
2020-10-30 20:59:54 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# TODO: Add to custom material info struct, maybe some material conversion/import?
|
|
|
|
def extract_materials(folder_path: str, scene: Scene) -> Dict[str, bpy.types.Material]:
|
2020-10-30 20:59:54 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
extracted_materials : Dict[str, bpy.types.Material] = {}
|
2020-10-30 20:59:54 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
for material_name, material in scene.materials.items():
|
2020-10-30 20:59:54 +00:00
|
|
|
|
2022-09-21 10:22:49 +00:00
|
|
|
extracted_materials[material_name] = swbf_material_to_blend(material_name, material, folder_path)
|
2020-10-30 20:59:54 +00:00
|
|
|
|
|
|
|
return extracted_materials
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-27 01:23:09 +00:00
|
|
|
|
|
|
|
|
2020-11-01 23:11:26 +00:00
|
|
|
def extract_scene(filepath: str, scene: Scene):
|
|
|
|
|
|
|
|
folder = os.path.join(os.path.dirname(filepath),"")
|
2020-11-22 05:24:20 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# material_map mapes Material names to Blender materials
|
|
|
|
material_map = extract_materials(folder, scene)
|
|
|
|
|
|
|
|
# model_map maps Model names to Blender objects.
|
|
|
|
model_map = extract_models(scene, material_map)
|
2020-11-22 05:24:20 +00:00
|
|
|
|
2022-01-11 19:02:17 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# skel contains all models needed in an armature
|
|
|
|
skel = extract_required_skeleton(scene)
|
2020-11-22 05:24:20 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# Create the armature if skel is non-empty
|
|
|
|
armature = None if not skel else required_skeleton_to_armature(skel, model_map, scene)
|
2020-12-11 04:47:45 +00:00
|
|
|
|
2022-01-13 20:52:30 +00:00
|
|
|
if armature is not None:
|
2022-01-18 20:16:49 +00:00
|
|
|
preserved_skel = armature.data.swbf_msh_skel
|
2022-01-13 20:52:30 +00:00
|
|
|
for model in scene.models:
|
2022-01-18 20:16:49 +00:00
|
|
|
if to_crc(model.name) in scene.skeleton or model.model_type == ModelType.BONE:
|
|
|
|
entry = preserved_skel.add()
|
2022-01-13 20:52:30 +00:00
|
|
|
entry.name = model.name
|
|
|
|
|
2020-12-11 04:47:45 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
'''
|
|
|
|
If an armature was created, we need to do a few extra
|
|
|
|
things to ensure the import makes sense in Blender. It can
|
|
|
|
get a bit messy, as XSI + SWBF have very loose requirements
|
|
|
|
when it comes to skin-skeleton parentage.
|
2020-12-11 04:47:45 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
If not, we're good.
|
|
|
|
'''
|
|
|
|
if armature is not None:
|
2020-12-11 04:47:45 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
has_skin = False
|
2020-12-11 04:47:45 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# Handle armature related parenting
|
|
|
|
for curr_model in scene.models:
|
|
|
|
|
|
|
|
curr_obj = model_map[curr_model.name]
|
|
|
|
|
|
|
|
# Parent all skins to armature
|
|
|
|
if curr_model.model_type == ModelType.SKIN:
|
|
|
|
|
|
|
|
has_skin = True
|
|
|
|
|
2022-09-27 01:23:09 +00:00
|
|
|
worldmat = curr_obj.matrix_world
|
2022-01-11 19:02:17 +00:00
|
|
|
curr_obj.parent = armature
|
|
|
|
curr_obj.parent_type = 'ARMATURE'
|
2022-09-27 01:23:09 +00:00
|
|
|
curr_obj.matrix_world = worldmat
|
2022-01-07 18:45:50 +00:00
|
|
|
|
|
|
|
# Parent the object to a bone if necessary
|
|
|
|
else:
|
|
|
|
if curr_model.parent in armature.data.bones and curr_model.name not in armature.data.bones:
|
2022-01-11 19:02:17 +00:00
|
|
|
# Not sure what the different mats do, but saving the worldmat and
|
|
|
|
# applying it after clearing the other mats yields correct results...
|
2022-01-07 18:45:50 +00:00
|
|
|
worldmat = curr_obj.matrix_world
|
2022-01-11 19:02:17 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
curr_obj.parent = armature
|
|
|
|
curr_obj.parent_type = 'BONE'
|
|
|
|
curr_obj.parent_bone = curr_model.parent
|
|
|
|
# ''
|
|
|
|
curr_obj.matrix_basis = Matrix()
|
|
|
|
curr_obj.matrix_parent_inverse = Matrix()
|
|
|
|
curr_obj.matrix_world = worldmat
|
|
|
|
|
|
|
|
'''
|
|
|
|
Sometimes skins are parented to other skins. We need to find the skin highest in the hierarchy and
|
|
|
|
parent all skins to its parent (armature_reparent_obj).
|
|
|
|
|
|
|
|
If not skin exists, we just reparent the armature to the parent of the highest node in the skeleton
|
|
|
|
'''
|
|
|
|
armature_reparent_obj = None
|
|
|
|
if has_skin:
|
|
|
|
for model in sort_by_parent(scene.models):
|
|
|
|
if model.model_type == ModelType.SKIN:
|
|
|
|
armature_reparent_obj = None if not model.parent else model_map[model.parent]
|
|
|
|
else:
|
|
|
|
skeleton_parent_name = skel[0].parent
|
|
|
|
for model in scene.models:
|
|
|
|
if model.name == skeleton_parent_name:
|
|
|
|
armature_reparent_obj = None if not skeleton_parent_name else model_map[skeleton_parent_name]
|
2020-12-11 04:47:45 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# Now we reparent the armature to the node (armature_reparent_obj) we just found
|
|
|
|
if armature_reparent_obj is not None and armature.name != armature_reparent_obj.name:
|
2022-09-27 01:23:09 +00:00
|
|
|
world_tx = armature.matrix_world
|
2022-01-11 19:02:17 +00:00
|
|
|
armature.parent = armature_reparent_obj
|
2022-09-27 01:23:09 +00:00
|
|
|
armature.matrix_basis = Matrix()
|
|
|
|
armature.matrix_parent_inverse = Matrix()
|
|
|
|
armature.matrix_world = Matrix.Identity(4)
|
2020-11-22 05:24:20 +00:00
|
|
|
|
2020-12-04 06:22:58 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# If an bone exists in the armature, delete its
|
|
|
|
# object counterpart (as created in extract_models)
|
|
|
|
for bone in skel:
|
|
|
|
model_to_remove = model_map[bone.name]
|
|
|
|
if model_to_remove:
|
|
|
|
bpy.data.objects.remove(model_to_remove, do_unlink=True)
|
|
|
|
model_map.pop(bone.name)
|
2022-09-27 01:23:09 +00:00
|
|
|
|
|
|
|
armature.matrix_world = Matrix.Identity(4)
|
2022-01-07 18:45:50 +00:00
|
|
|
|
2020-11-22 05:24:20 +00:00
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
# Lastly, hide all that is hidden in the msh scene
|
2020-11-22 05:24:20 +00:00
|
|
|
for model in scene.models:
|
2022-01-07 18:45:50 +00:00
|
|
|
if model.name in model_map:
|
|
|
|
obj = model_map[model.name]
|
|
|
|
if get_is_model_hidden(obj) and len(obj.children) == 0:
|
2020-12-04 08:33:46 +00:00
|
|
|
obj.hide_set(True)
|
2022-09-27 01:23:09 +00:00
|
|
|
|