From 16471344f0d5cd1b6997c55719fa5b500164f04f Mon Sep 17 00:00:00 2001 From: Colin Basnett Date: Tue, 6 May 2025 18:48:44 -0700 Subject: [PATCH] Fix #129: PSK can be imported into UT2 with incorrect weighting Weights within the PSK are now sorted by point_index and have their weights normalized. --- io_scene_psk_psa/psk/builder.py | 3 +++ io_scene_psk_psa/psk/data.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/io_scene_psk_psa/psk/builder.py b/io_scene_psk_psa/psk/builder.py index 5756f8d..3f415fd 100644 --- a/io_scene_psk_psa/psk/builder.py +++ b/io_scene_psk_psa/psk/builder.py @@ -421,6 +421,9 @@ def build_psk(context: Context, input_objects: PskInputObjects, options: PskBuil context.window_manager.progress_end() + # https://github.com/DarklightGames/io_scene_psk_psa/issues/129. + psk.sort_and_normalize_weights() + result.psk = psk return result diff --git a/io_scene_psk_psa/psk/data.py b/io_scene_psk_psa/psk/data.py index 16e991c..0483aea 100644 --- a/io_scene_psk_psa/psk/data.py +++ b/io_scene_psk_psa/psk/data.py @@ -113,6 +113,26 @@ class Psk(object): def has_morph_data(self): return len(self.morph_infos) > 0 + def sort_and_normalize_weights(self): + self.weights.sort(key=lambda x: x.point_index) + weight_index = 0 + weight_total = len(self.weights) + while weight_index < weight_total: + point_weight_total = 1 + point_index: int = self.weights[weight_index].point_index + weight_sum: float = self.weights[weight_index].weight + # Count the number of weights with contiguous point indices and sum the total weights. + for w in range(weight_index + 1, weight_total): + if point_index != self.weights[w].point_index: + break + point_weight_total += 1 + weight_sum += self.weights[w].weight + # Now normalize the weights against the sum of all weights. + for weight in self.weights[weight_index:weight_index+point_weight_total]: + weight.weight /= weight_sum + # Increment + weight_index += point_weight_total + def __init__(self): self.points: List[Vector3] = [] self.wedges: List[Psk.Wedge] = []