From 1ac0870b310c502bbbcd2b52922907cfd5461dc7 Mon Sep 17 00:00:00 2001 From: Yurii Ti Date: Thu, 5 May 2022 15:02:31 +0300 Subject: [PATCH 1/3] [PSA Export] Remember selections for bone groups * Selections for bone groups are now preserved between exporter invocations. * Added typing hints to the `populate_bone_group_list` function. --- io_scene_psk_psa/helpers.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/io_scene_psk_psa/helpers.py b/io_scene_psk_psa/helpers.py index 8c5c69c..897ac96 100644 --- a/io_scene_psk_psa/helpers.py +++ b/io_scene_psk_psa/helpers.py @@ -1,8 +1,9 @@ import datetime from collections import Counter -from typing import List +from typing import List, Iterable -from bpy.types import NlaStrip +from bpy.types import NlaStrip, Object +from .types import BoneGroupListItem class Timer: @@ -56,7 +57,16 @@ def get_nla_strips_in_timeframe(object, frame_min, frame_max) -> List[NlaStrip]: return strips -def populate_bone_group_list(armature_object, bone_group_list): +def populate_bone_group_list(armature_object: Object, bone_group_list: Iterable[BoneGroupListItem]) -> None: + # Preserve group selections before clearing the list. + # We handle selections for the unassigned group separately to cover the edge + # case where there might be an actual group with 'Unassigned' as its name. + unassigned_group_idx, unassigned_group_is_selected = next(iter([ + (i, g.is_selected) for i, g in enumerate(bone_group_list) if g.index == -1]), (-1, False)) + + selected_assigned_group_names = [ + g.name for i, g in enumerate(bone_group_list) if i != unassigned_group_idx and g.is_selected] + bone_group_list.clear() if armature_object and armature_object.pose: @@ -66,14 +76,14 @@ def populate_bone_group_list(armature_object, bone_group_list): item.name = 'Unassigned' item.index = -1 item.count = 0 if None not in bone_group_counts else bone_group_counts[None] - item.is_selected = True + item.is_selected = unassigned_group_is_selected for bone_group_index, bone_group in enumerate(armature_object.pose.bone_groups): item = bone_group_list.add() item.name = bone_group.name item.index = bone_group_index item.count = 0 if bone_group not in bone_group_counts else bone_group_counts[bone_group] - item.is_selected = True + item.is_selected = bone_group.name in selected_assigned_group_names def get_psa_sequence_name(action, should_use_original_sequence_name): From 84954823457e535fd2ee1267ad0527b8753d70c5 Mon Sep 17 00:00:00 2001 From: Yurii Ti Date: Thu, 5 May 2022 15:52:38 +0300 Subject: [PATCH 2/3] [PSA Export] Select all bone groups if none were previously selected This brings back the old behavior (before 1ac0870) where all groups are selected by default. --- io_scene_psk_psa/helpers.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/io_scene_psk_psa/helpers.py b/io_scene_psk_psa/helpers.py index 897ac96..fec4c24 100644 --- a/io_scene_psk_psa/helpers.py +++ b/io_scene_psk_psa/helpers.py @@ -58,14 +58,18 @@ def get_nla_strips_in_timeframe(object, frame_min, frame_max) -> List[NlaStrip]: def populate_bone_group_list(armature_object: Object, bone_group_list: Iterable[BoneGroupListItem]) -> None: - # Preserve group selections before clearing the list. - # We handle selections for the unassigned group separately to cover the edge - # case where there might be an actual group with 'Unassigned' as its name. - unassigned_group_idx, unassigned_group_is_selected = next(iter([ - (i, g.is_selected) for i, g in enumerate(bone_group_list) if g.index == -1]), (-1, False)) + has_selected_groups = any([g.is_selected for g in bone_group_list]) + unassigned_group_is_selected, selected_assigned_group_names = True, [] - selected_assigned_group_names = [ - g.name for i, g in enumerate(bone_group_list) if i != unassigned_group_idx and g.is_selected] + if has_selected_groups: + # Preserve group selections before clearing the list. + # We handle selections for the unassigned group separately to cover the edge case + # where there might be an actual group with 'Unassigned' as its name. + unassigned_group_idx, unassigned_group_is_selected = next(iter([ + (i, g.is_selected) for i, g in enumerate(bone_group_list) if g.index == -1]), (-1, False)) + + selected_assigned_group_names = [ + g.name for i, g in enumerate(bone_group_list) if i != unassigned_group_idx and g.is_selected] bone_group_list.clear() @@ -83,7 +87,7 @@ def populate_bone_group_list(armature_object: Object, bone_group_list: Iterable[ item.name = bone_group.name item.index = bone_group_index item.count = 0 if bone_group not in bone_group_counts else bone_group_counts[bone_group] - item.is_selected = bone_group.name in selected_assigned_group_names + item.is_selected = bone_group.name in selected_assigned_group_names if has_selected_groups else True def get_psa_sequence_name(action, should_use_original_sequence_name): From df6bdb96a4165374c311cb580b1380bdf742f07d Mon Sep 17 00:00:00 2001 From: Yurii Ti Date: Thu, 5 May 2022 17:38:22 +0300 Subject: [PATCH 3/3] Added a docstring for `populate_bone_group_list` --- io_scene_psk_psa/helpers.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/io_scene_psk_psa/helpers.py b/io_scene_psk_psa/helpers.py index fec4c24..937ea05 100644 --- a/io_scene_psk_psa/helpers.py +++ b/io_scene_psk_psa/helpers.py @@ -58,6 +58,12 @@ def get_nla_strips_in_timeframe(object, frame_min, frame_max) -> List[NlaStrip]: def populate_bone_group_list(armature_object: Object, bone_group_list: Iterable[BoneGroupListItem]) -> None: + """ + Updates the bone group collection. + + Bone group selections are preserved between updates unless none of the groups were previously selected; + otherwise, all groups are selected by default. + """ has_selected_groups = any([g.is_selected for g in bone_group_list]) unassigned_group_is_selected, selected_assigned_group_names = True, []