Typing and naming improvements
This commit is contained in:
@@ -112,20 +112,20 @@ def build_psa(context: bpy.types.Context, options: PsaBuildOptions) -> Psa:
|
|||||||
|
|
||||||
psa = Psa()
|
psa = Psa()
|
||||||
|
|
||||||
armature = active_object
|
armature_object = active_object
|
||||||
armature_data = typing.cast(Armature, armature)
|
armature_data = typing.cast(Armature, armature_object.data)
|
||||||
bones: List[Bone] = list(iter(armature_data.bones))
|
bones: List[Bone] = list(iter(armature_data.bones))
|
||||||
|
|
||||||
# The order of the armature bones and the pose bones is not guaranteed to be the same.
|
# The order of the armature bones and the pose bones is not guaranteed to be the same.
|
||||||
# As a result, we need to reconstruct the list of pose bones in the same order as the
|
# As a result, we need to reconstruct the list of pose bones in the same order as the
|
||||||
# armature bones.
|
# armature bones.
|
||||||
bone_names = [x.name for x in bones]
|
bone_names = [x.name for x in bones]
|
||||||
pose_bones = [(bone_names.index(bone.name), bone) for bone in armature.pose.bones]
|
pose_bones = [(bone_names.index(bone.name), bone) for bone in armature_object.pose.bones]
|
||||||
pose_bones.sort(key=lambda x: x[0])
|
pose_bones.sort(key=lambda x: x[0])
|
||||||
pose_bones = [x[1] for x in pose_bones]
|
pose_bones = [x[1] for x in pose_bones]
|
||||||
|
|
||||||
# Get a list of all the bone indices and instigator bones for the bone filter settings.
|
# Get a list of all the bone indices and instigator bones for the bone filter settings.
|
||||||
export_bone_names = get_export_bone_names(armature, options.bone_filter_mode, options.bone_group_indices)
|
export_bone_names = get_export_bone_names(armature_object, options.bone_filter_mode, options.bone_group_indices)
|
||||||
bone_indices = [bone_names.index(x) for x in export_bone_names]
|
bone_indices = [bone_names.index(x) for x in export_bone_names]
|
||||||
|
|
||||||
# Make the bone lists contain only the bones that are going to be exported.
|
# Make the bone lists contain only the bones that are going to be exported.
|
||||||
@@ -263,7 +263,7 @@ def build_psa(context: bpy.types.Context, options: PsaBuildOptions) -> Psa:
|
|||||||
else:
|
else:
|
||||||
if options.root_motion:
|
if options.root_motion:
|
||||||
# Export root motion
|
# Export root motion
|
||||||
pose_bone_matrix = armature.matrix_world @ pose_bone.matrix
|
pose_bone_matrix = armature_object.matrix_world @ pose_bone.matrix
|
||||||
else:
|
else:
|
||||||
pose_bone_matrix = pose_bone.matrix
|
pose_bone_matrix = pose_bone.matrix
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
import typing
|
||||||
|
|
||||||
import bmesh
|
import bmesh
|
||||||
import bpy
|
import bpy
|
||||||
|
from bpy.types import Armature
|
||||||
|
|
||||||
from .data import *
|
from .data import *
|
||||||
from ..helpers import *
|
from ..helpers import *
|
||||||
@@ -59,7 +62,7 @@ def get_psk_input_objects(context) -> PskInputObjects:
|
|||||||
def build_psk(context, options: PskBuildOptions) -> Psk:
|
def build_psk(context, options: PskBuildOptions) -> Psk:
|
||||||
input_objects = get_psk_input_objects(context)
|
input_objects = get_psk_input_objects(context)
|
||||||
|
|
||||||
armature_object = input_objects.armature_object
|
armature_object: bpy.types.Object = input_objects.armature_object
|
||||||
|
|
||||||
psk = Psk()
|
psk = Psk()
|
||||||
bones = []
|
bones = []
|
||||||
@@ -77,7 +80,8 @@ def build_psk(context, options: PskBuildOptions) -> Psk:
|
|||||||
psk.bones.append(psk_bone)
|
psk.bones.append(psk_bone)
|
||||||
else:
|
else:
|
||||||
bone_names = get_export_bone_names(armature_object, options.bone_filter_mode, options.bone_group_indices)
|
bone_names = get_export_bone_names(armature_object, options.bone_filter_mode, options.bone_group_indices)
|
||||||
bones = [armature_object.data.bones[bone_name] for bone_name in bone_names]
|
armature_data = typing.cast(Armature, armature_object.data)
|
||||||
|
bones = [armature_data.bones[bone_name] for bone_name in bone_names]
|
||||||
|
|
||||||
# Check that all bone names are valid.
|
# Check that all bone names are valid.
|
||||||
if not options.should_ignore_bone_name_restrictions:
|
if not options.should_ignore_bone_name_restrictions:
|
||||||
@@ -98,9 +102,9 @@ def build_psk(context, options: PskBuildOptions) -> Psk:
|
|||||||
|
|
||||||
if bone.parent is not None:
|
if bone.parent is not None:
|
||||||
rotation = bone.matrix.to_quaternion().conjugated()
|
rotation = bone.matrix.to_quaternion().conjugated()
|
||||||
quat_parent = bone.parent.matrix.to_quaternion().inverted()
|
inverse_parent_rotation = bone.parent.matrix.to_quaternion().inverted()
|
||||||
parent_head = quat_parent @ bone.parent.head
|
parent_head = inverse_parent_rotation @ bone.parent.head
|
||||||
parent_tail = quat_parent @ bone.parent.tail
|
parent_tail = inverse_parent_rotation @ bone.parent.tail
|
||||||
location = (parent_tail - parent_head) + bone.head
|
location = (parent_tail - parent_head) + bone.head
|
||||||
else:
|
else:
|
||||||
armature_local_matrix = armature_object.matrix_local
|
armature_local_matrix = armature_object.matrix_local
|
||||||
@@ -219,6 +223,7 @@ def build_psk(context, options: PskBuildOptions) -> Psk:
|
|||||||
|
|
||||||
# WEIGHTS
|
# WEIGHTS
|
||||||
if armature_object is not None:
|
if armature_object is not None:
|
||||||
|
armature_data = typing.cast(Armature, armature_object.data)
|
||||||
# Because the vertex groups may contain entries for which there is no matching bone in the armature,
|
# Because the vertex groups may contain entries for which there is no matching bone in the armature,
|
||||||
# we must filter them out and not export any weights for these vertex groups.
|
# we must filter them out and not export any weights for these vertex groups.
|
||||||
bone_names = [x.name for x in bones]
|
bone_names = [x.name for x in bones]
|
||||||
@@ -232,8 +237,8 @@ def build_psk(context, options: PskBuildOptions) -> Psk:
|
|||||||
# Check to see if there is an associated bone for this vertex group that exists in the armature.
|
# Check to see if there is an associated bone for this vertex group that exists in the armature.
|
||||||
# If there is, we can traverse the ancestors of that bone to find an alternate bone to use for
|
# If there is, we can traverse the ancestors of that bone to find an alternate bone to use for
|
||||||
# weighting the vertices belonging to this vertex group.
|
# weighting the vertices belonging to this vertex group.
|
||||||
if vertex_group_name in armature_object.data.bones:
|
if vertex_group_name in armature_data.bones:
|
||||||
bone = armature_object.data.bones[vertex_group_name]
|
bone = armature_data.bones[vertex_group_name]
|
||||||
while bone is not None:
|
while bone is not None:
|
||||||
try:
|
try:
|
||||||
bone_index = bone_names.index(bone.name)
|
bone_index = bone_names.index(bone.name)
|
||||||
|
|||||||
Reference in New Issue
Block a user