Skeleton impurities (roots,effectors) now included by default, zaa_reader and msh_reader combined in chunked_file_reader, skin and skeleton parentage issues worked out. TODO: Fill material properties on import, decide what to do with SkeletonProperties.
This commit is contained in:
@@ -17,9 +17,8 @@ from .crc import *
|
||||
|
||||
import os
|
||||
|
||||
|
||||
|
||||
def extract_and_apply_anim(filename, scene):
|
||||
# Extracts and applies anims in the scene to the currently selected armature
|
||||
def extract_and_apply_anim(filename : str, scene : Scene):
|
||||
|
||||
arma = bpy.context.view_layer.objects.active
|
||||
|
||||
@@ -39,29 +38,26 @@ def extract_and_apply_anim(filename, scene):
|
||||
arma.animation_data_create()
|
||||
|
||||
|
||||
# Record the starting transforms of each bone. Pose space is relative
|
||||
# to bones starting transforms. Starting = in edit mode
|
||||
bone_bind_poses = {}
|
||||
|
||||
for bone in arma.data.bones:
|
||||
bone_obj = bpy.data.objects[bone.name]
|
||||
bone_obj_parent = bone_obj.parent
|
||||
bpy.context.view_layer.objects.active = arma
|
||||
bpy.ops.object.mode_set(mode='EDIT')
|
||||
|
||||
bind_mat = bone_obj.matrix_local
|
||||
stack_mat = Matrix.Identity(4)
|
||||
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
|
||||
|
||||
bone_bind_poses[edit_bone.name] = bone_local.inverted()
|
||||
|
||||
while(True):
|
||||
if bone_obj_parent is None or bone_obj_parent.name in arma.data.bones:
|
||||
break
|
||||
bind_mat = bone_obj_parent.matrix_local @ bind_mat
|
||||
stack_mat = bone_obj_parent.matrix_local @ stack_mat
|
||||
bone_obj_parent = bone_obj_parent.parent
|
||||
|
||||
bone_bind_poses[bone.name] = bind_mat.inverted() @ stack_mat
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
|
||||
|
||||
for bone in arma.pose.bones:
|
||||
if to_crc(bone.name) in scene.animation.bone_frames:
|
||||
#print("Inserting anim data for bone: {}".format(bone.name))
|
||||
|
||||
bind_mat = bone_bind_poses[bone.name]
|
||||
|
||||
@@ -85,7 +81,6 @@ def extract_and_apply_anim(filename, scene):
|
||||
fcurve_rot_y.keyframe_points.insert(i,q.y)
|
||||
fcurve_rot_z.keyframe_points.insert(i,q.z)
|
||||
|
||||
|
||||
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)
|
||||
@@ -103,58 +98,62 @@ def extract_and_apply_anim(filename, scene):
|
||||
|
||||
|
||||
|
||||
def parent_object_to_bone(obj, armature, bone_name):
|
||||
|
||||
worldmat = obj.matrix_world
|
||||
|
||||
obj.parent = None
|
||||
obj.parent = armature
|
||||
obj.parent_type = 'BONE'
|
||||
obj.parent_bone = bone_name
|
||||
|
||||
obj.matrix_basis = Matrix()
|
||||
obj.matrix_parent_inverse = Matrix()
|
||||
|
||||
obj.matrix_world = worldmat
|
||||
|
||||
|
||||
'''
|
||||
Creates armature from the required nodes.
|
||||
Assumes the required_skeleton is already sorted by parent.
|
||||
|
||||
def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map):
|
||||
Uses model_map to get the world matrix of each bone (hacky, see NOTE)
|
||||
'''
|
||||
def required_skeleton_to_armature(required_skeleton : List[Model], model_map : Dict[str, bpy.types.Object], msh_scene : Scene) -> bpy.types.Object:
|
||||
|
||||
armature = bpy.data.armatures.new("skeleton")
|
||||
armature_obj = bpy.data.objects.new("skeleton", armature)
|
||||
|
||||
bpy.context.view_layer.active_layer_collection.collection.objects.link(armature_obj)
|
||||
armature_obj.select_set(True)
|
||||
|
||||
|
||||
preserved = armature_obj.data.swbf_msh_skel
|
||||
for model in refined_skeleton:
|
||||
loc,rot,_ = model_map[model.name].matrix_world.decompose()
|
||||
print(str(loc))
|
||||
entry = preserved.add()
|
||||
entry.name = model.name
|
||||
entry.loc = loc
|
||||
entry.rot = rot
|
||||
entry.parent = model.parent
|
||||
for model in required_skeleton:
|
||||
if to_crc(model.name) in msh_scene.skeleton:
|
||||
entry = preserved.add()
|
||||
entry.name = model.name
|
||||
#loc,rot,_ = model_map[model.name].matrix_world.decompose()
|
||||
#entry.loc = loc
|
||||
#entry.rot = rot
|
||||
#entry.parent = model.parent
|
||||
|
||||
|
||||
bones_set = set([model.name for model in required_skeleton])
|
||||
|
||||
armature_obj.select_set(True)
|
||||
bpy.context.view_layer.objects.active = armature_obj
|
||||
bpy.ops.object.mode_set(mode='EDIT')
|
||||
|
||||
for bone in refined_skeleton:
|
||||
for bone in required_skeleton:
|
||||
|
||||
edit_bone = armature.edit_bones.new(bone.name)
|
||||
|
||||
if bone.parent:
|
||||
if bone.parent and bone.parent in bones_set:
|
||||
edit_bone.parent = armature.edit_bones[bone.parent]
|
||||
|
||||
'''
|
||||
NOTE: I recall there being some rare issue with the get_world_matrix utility func.
|
||||
Never bothered to figure it out and referencing the bone object's world mat always works.
|
||||
Bone objects will be deleted later.
|
||||
'''
|
||||
bone_obj = model_map[bone.name]
|
||||
|
||||
edit_bone.matrix = bone_obj.matrix_world
|
||||
edit_bone.tail = bone_obj.matrix_world @ Vector((0.0,1.0,0.0))
|
||||
|
||||
|
||||
bone_children = [b for b in get_model_children(bone, refined_skeleton)]
|
||||
bone_children = [b for b in get_model_children(bone, required_skeleton)]
|
||||
|
||||
'''
|
||||
Perhaps we'll add an option for importing bones tip-to-tail, but that would
|
||||
require preserving their original transforms as changing the tail position
|
||||
changes the bones' transform...
|
||||
'''
|
||||
tail_pos = Vector()
|
||||
if bone_children:
|
||||
for bone_child in bone_children:
|
||||
@@ -164,8 +163,6 @@ def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map):
|
||||
else:
|
||||
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')
|
||||
armature_obj.select_set(True)
|
||||
@@ -176,14 +173,26 @@ def refined_skeleton_to_armature(refined_skeleton : List[Model], model_map):
|
||||
|
||||
|
||||
|
||||
'''
|
||||
Ok, so this method is crucial. What this does is:
|
||||
1) Find all nodes that are weighted to by skinned segments.
|
||||
2) A node must be included in the armature if:
|
||||
- It is in SKL2 and is not the scene root
|
||||
- It is weighted to
|
||||
- It has a parent and child that must be in the armature
|
||||
'''
|
||||
def extract_required_skeleton(scene: Scene) -> List[Model]:
|
||||
|
||||
# Will map Model names to Models in scene, for convenience
|
||||
model_dict : Dict[str, Model] = {}
|
||||
|
||||
def extract_refined_skeleton(scene: Scene):
|
||||
|
||||
model_dict = {}
|
||||
skeleton_models = []
|
||||
|
||||
# Will contain hashes of all models that definitely need to be in the skeleton/armature.
|
||||
# We initialize it with the contents of SKL2 i.e. the nodes that are animated.
|
||||
# For now this includes the scene root, but that'll be excluded later.
|
||||
skeleton_hashes = set(scene.skeleton)
|
||||
|
||||
# We also need to add all nodes that are weighted to. These are not necessarily in
|
||||
# SKL2, as SKL2 seems to only reference nodes that are keyframed.
|
||||
for model in scene.models:
|
||||
model_dict[model.name] = model
|
||||
|
||||
@@ -194,57 +203,73 @@ def extract_refined_skeleton(scene: Scene):
|
||||
for weight in weight_set:
|
||||
model_weighted_to = scene.models[weight.bone]
|
||||
|
||||
if to_crc(model_weighted_to.name) not in scene.skeleton:
|
||||
scene.skeleton.append(to_crc(model_weighted_to.name))
|
||||
|
||||
for model in scene.models:
|
||||
if to_crc(model.name) in scene.skeleton:
|
||||
skeleton_models.append(model)
|
||||
if to_crc(model_weighted_to.name) not in skeleton_hashes:
|
||||
skeleton_hashes.add(to_crc(model_weighted_to.name))
|
||||
|
||||
# The result of this function (to be sorted by parent)
|
||||
required_skeleton_models = []
|
||||
|
||||
refined_skeleton_models = []
|
||||
|
||||
for bone in skeleton_models:
|
||||
# Set of nodes to be included in required skeleton/were visited
|
||||
visited_nodes = set()
|
||||
|
||||
if bone.parent:
|
||||
'''
|
||||
Here we add all skeleton nodes (except root) and any necessary ancestors to the armature.
|
||||
- e.g. in bone_x/eff_x/eff_y, the effectors do not have to be in armature, as they are not ancestors of a bone
|
||||
- but in bone_x/eff_x/eff_y/bone_y, they do.
|
||||
'''
|
||||
for bone in sort_by_parent(scene.models):
|
||||
|
||||
curr_ancestor = model_dict[bone.parent]
|
||||
stacked_transform = model_transform_to_matrix(bone.transform)
|
||||
# make sure we exclude the scene root and any nodes irrelevant to the armature
|
||||
if not bone.parent or to_crc(bone.name) not in skeleton_hashes:
|
||||
continue
|
||||
|
||||
while True:
|
||||
potential_bones = [bone]
|
||||
visited_nodes.add(bone.name)
|
||||
|
||||
if to_crc(curr_ancestor.name) in scene.skeleton or curr_ancestor.name == scene.models[0].name:
|
||||
new_model = Model()
|
||||
new_model.name = bone.name
|
||||
new_model.parent = curr_ancestor.name if curr_ancestor.name != scene.models[0].name else ""
|
||||
# Stacked transform will be needed if we decide to include an option for excluding effectors/roots
|
||||
#stacked_transform = model_transform_to_matrix(bone.transform)
|
||||
|
||||
curr_ancestor = model_dict[bone.parent]
|
||||
|
||||
loc, rot, _ = stacked_transform.decompose()
|
||||
while True:
|
||||
|
||||
new_model.transform.rotation = rot
|
||||
new_model.transform.translation = loc
|
||||
|
||||
refined_skeleton_models.append(new_model)
|
||||
break
|
||||
# If we hit a non-skin scene root, that means we just add the bone we started with, no ancestors.
|
||||
if not curr_ancestor.parent and curr_ancestor.model_type != ModelType.SKIN:
|
||||
required_skeleton_models.append(bone)
|
||||
visited_nodes.add(bone.name)
|
||||
break
|
||||
|
||||
else:
|
||||
curr_ancestor = model_dict[curr_ancestor.parent]
|
||||
stacked_transform = model_transform_to_matrix(curr_ancestor.transform) @ stacked_transform
|
||||
|
||||
return sort_by_parent(refined_skeleton_models)
|
||||
# If we encounter another bone, a skin, or a previously visited object, we need to add the bone and its
|
||||
# ancestors.
|
||||
elif to_crc(curr_ancestor.name) in scene.skeleton or curr_ancestor.model_type == ModelType.SKIN or curr_ancestor.name in visited_nodes:
|
||||
for potential_bone in potential_bones:
|
||||
required_skeleton_models.append(potential_bone)
|
||||
visited_nodes.add(potential_bone.name)
|
||||
break
|
||||
|
||||
# Add ancestor to potential bones, update next ancestor
|
||||
else:
|
||||
if curr_ancestor.name not in visited_nodes:
|
||||
potential_bones.insert(0, curr_ancestor)
|
||||
curr_ancestor = model_dict[curr_ancestor.parent]
|
||||
|
||||
#stacked_transform = model_transform_to_matrix(curr_ancestor.transform) @ stacked_transform
|
||||
|
||||
return required_skeleton_models
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Create the msh hierachy. Armatures are not created here.
|
||||
def extract_models(scene: Scene, materials_map : Dict[str, bpy.types.Material]) -> Dict[str, bpy.types.Object]:
|
||||
|
||||
# This will be filled with model names -> Blender objects and returned
|
||||
model_map : Dict[str, bpy.types.Object] = {}
|
||||
|
||||
sorted_models : List[Model] = sort_by_parent(scene.models)
|
||||
|
||||
|
||||
def extract_models(scene: Scene, materials_map):
|
||||
|
||||
model_map = {}
|
||||
|
||||
for model in sort_by_parent(scene.models):
|
||||
for model in sorted_models:
|
||||
new_obj = None
|
||||
|
||||
if model.model_type == ModelType.STATIC or model.model_type == ModelType.SKIN:
|
||||
@@ -358,19 +383,19 @@ def extract_models(scene: Scene, materials_map):
|
||||
return model_map
|
||||
|
||||
|
||||
# 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]:
|
||||
|
||||
def extract_materials(folder_path: str, scene: Scene) -> Dict[str,bpy.types.Material]:
|
||||
extracted_materials : Dict[str, bpy.types.Material] = {}
|
||||
|
||||
extracted_materials = {}
|
||||
|
||||
for material_name in scene.materials.keys():
|
||||
for material_name, material in scene.materials.items():
|
||||
|
||||
new_mat = bpy.data.materials.new(name=material_name)
|
||||
new_mat.use_nodes = True
|
||||
bsdf = new_mat.node_tree.nodes["Principled BSDF"]
|
||||
|
||||
tex_path_def = os.path.join(folder_path, scene.materials[material_name].texture0)
|
||||
tex_path_alt = os.path.join(folder_path, "PC", scene.materials[material_name].texture0)
|
||||
tex_path_def = os.path.join(folder_path, material.texture0)
|
||||
tex_path_alt = os.path.join(folder_path, "PC", material.texture0)
|
||||
|
||||
tex_path = tex_path_def if os.path.exists(tex_path_def) else tex_path_alt
|
||||
|
||||
@@ -379,6 +404,20 @@ def extract_materials(folder_path: str, scene: Scene) -> Dict[str,bpy.types.Mate
|
||||
texImage.image = bpy.data.images.load(tex_path)
|
||||
new_mat.node_tree.links.new(bsdf.inputs['Base Color'], texImage.outputs['Color'])
|
||||
|
||||
# Fill MaterialProperties datablock
|
||||
'''
|
||||
material_properties = new_mat.swbf_msh
|
||||
material_properties.specular_color = material.specular_color.copy()
|
||||
material_properties.diffuse_map = material.texture0
|
||||
|
||||
result.rendertype = _read_material_props_rendertype(props)
|
||||
result.flags = _read_material_props_flags(props)
|
||||
result.data = _read_material_props_data(props)
|
||||
result.texture1 = _read_normal_map_or_distortion_map_texture(props)
|
||||
result.texture2 = _read_detail_texture(props)
|
||||
result.texture3 = _read_envmap_texture(props)
|
||||
'''
|
||||
|
||||
extracted_materials[material_name] = new_mat
|
||||
|
||||
return extracted_materials
|
||||
@@ -388,82 +427,112 @@ def extract_materials(folder_path: str, scene: Scene) -> Dict[str,bpy.types.Mate
|
||||
def extract_scene(filepath: str, scene: Scene):
|
||||
|
||||
folder = os.path.join(os.path.dirname(filepath),"")
|
||||
matmap = extract_materials(folder, scene)
|
||||
|
||||
model_map = extract_models(scene, matmap)
|
||||
# material_map mapes Material names to Blender materials
|
||||
material_map = extract_materials(folder, scene)
|
||||
|
||||
skel = extract_refined_skeleton(scene)
|
||||
armature = refined_skeleton_to_armature(skel, model_map)
|
||||
# model_map maps Model names to Blender objects.
|
||||
model_map = extract_models(scene, material_map)
|
||||
|
||||
# skel contains all models needed in an armature
|
||||
skel = extract_required_skeleton(scene)
|
||||
|
||||
# Create the armature if skel is non-empty
|
||||
armature = None if not skel else required_skeleton_to_armature(skel, model_map, scene)
|
||||
|
||||
|
||||
for bone in armature.data.bones:
|
||||
bone_local = bone.matrix_local
|
||||
if bone.parent:
|
||||
bone_local = bone.parent.matrix_local.inverted() @ bone_local
|
||||
'''
|
||||
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.
|
||||
|
||||
bone_obj_local = bpy.data.objects[bone.name].matrix_local
|
||||
obj_loc, obj_rot, _ = bone_obj_local.decompose()
|
||||
If not, we're good.
|
||||
'''
|
||||
if armature is not None:
|
||||
|
||||
loc, rot, _ = bone_local.decompose()
|
||||
has_skin = False
|
||||
|
||||
# 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
|
||||
|
||||
curr_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')
|
||||
|
||||
curr_obj.select_set(False)
|
||||
armature.select_set(False)
|
||||
bpy.context.view_layer.objects.active = None
|
||||
|
||||
# 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:
|
||||
# Some of this is redundant, but necessary...
|
||||
worldmat = curr_obj.matrix_world
|
||||
# ''
|
||||
curr_obj.parent = None
|
||||
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]
|
||||
|
||||
|
||||
# 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:
|
||||
|
||||
reparent_obj = None
|
||||
for model in scene.models:
|
||||
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
|
||||
armature_reparent_obj.select_set(True)
|
||||
|
||||
bpy.ops.object.parent_clear(type='CLEAR')
|
||||
bpy.ops.object.parent_set(type='ARMATURE')
|
||||
bpy.context.view_layer.objects.active = armature_reparent_obj
|
||||
bpy.ops.object.parent_set(type='OBJECT')
|
||||
|
||||
skin_obj.select_set(False)
|
||||
armature.select_set(False)
|
||||
armature_reparent_obj.select_set(False)
|
||||
bpy.context.view_layer.objects.active = None
|
||||
|
||||
if armature is not None:
|
||||
for bone in armature.data.bones:
|
||||
for model in scene.models:
|
||||
if model.parent in armature.data.bones and model.model_type != ModelType.NULL:
|
||||
pass#parent_object_to_bone(model_map[model.name], armature, model.parent)
|
||||
|
||||
'''
|
||||
if reparent_obj is not None and armature.name != reparent_obj.name:
|
||||
|
||||
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
|
||||
'''
|
||||
|
||||
for model in scene.models:
|
||||
if model.name in bpy.data.objects:
|
||||
obj = bpy.data.objects[model.name]
|
||||
if get_is_model_hidden(obj) and len(obj.children) == 0 and model.model_type != ModelType.NULL:
|
||||
obj.hide_set(True)
|
||||
# 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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Lastly, hide all that is hidden in the msh scene
|
||||
for model in scene.models:
|
||||
if model.name in model_map:
|
||||
obj = model_map[model.name]
|
||||
if get_is_model_hidden(obj) and len(obj.children) == 0:
|
||||
obj.hide_set(True)
|
||||
|
||||
Reference in New Issue
Block a user