From 5c50e767714d57379fd5c65b82112c6d4826ab58 Mon Sep 17 00:00:00 2001 From: Colin Basnett Date: Sun, 3 Aug 2025 01:42:44 -0700 Subject: [PATCH] Moved `get_collection_*from_context` functions to `helpers.py` --- io_scene_psk_psa/psk/export/operators.py | 28 ++++------------------- io_scene_psk_psa/psk/export/properties.py | 4 +++- io_scene_psk_psa/shared/helpers.py | 22 +++++++++++++++++- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/io_scene_psk_psa/psk/export/operators.py b/io_scene_psk_psa/psk/export/operators.py index c3cb2b1..a9e5c86 100644 --- a/io_scene_psk_psa/psk/export/operators.py +++ b/io_scene_psk_psa/psk/export/operators.py @@ -15,7 +15,7 @@ from ..builder import ( get_psk_input_objects_for_context, ) from ..writer import write_psk -from ...shared.helpers import PsxBoneCollection, populate_bone_collection_list +from ...shared.helpers import PsxBoneCollection, get_collection_export_operator_from_context, populate_bone_collection_list from ...shared.ui import draw_bone_filter_mode @@ -35,29 +35,6 @@ def populate_material_name_list(depsgraph: Depsgraph, mesh_objects: Iterable[Obj -def get_collection_from_context(context: Context) -> Optional[Collection]: - if context.space_data.type != 'PROPERTIES': - return None - - space_data = typing_cast(SpaceProperties, context.space_data) - - if space_data.use_pin_id: - return typing_cast(Collection, space_data.pin_id) - else: - return context.collection - - -def get_collection_export_operator_from_context(context: Context) -> Optional[object]: - collection = get_collection_from_context(context) - if collection is None: - return None - if 0 > collection.active_exporter_index >= len(collection.exporters): - return None - exporter = collection.exporters[collection.active_exporter_index] - # TODO: make sure this is actually an ASE exporter. - return exporter.export_properties - - class PSK_OT_bone_collection_list_populate(Operator): bl_idname = 'psk.bone_collection_list_populate' bl_label = 'Populate Bone Collection List' @@ -69,6 +46,9 @@ class PSK_OT_bone_collection_list_populate(Operator): if export_operator is None: self.report({'ERROR_INVALID_CONTEXT'}, 'No valid export operator found in context') return {'CANCELLED'} + if context.collection is None: + self.report({'ERROR_INVALID_CONTEXT'}, 'No active collection') + return {'CANCELLED'} try: input_objects = get_psk_input_objects_for_collection(context.collection) except RuntimeError as e: diff --git a/io_scene_psk_psa/psk/export/properties.py b/io_scene_psk_psa/psk/export/properties.py index 9f3741f..d13bee1 100644 --- a/io_scene_psk_psa/psk/export/properties.py +++ b/io_scene_psk_psa/psk/export/properties.py @@ -1,13 +1,15 @@ +from bpy.types import Context from bpy.props import ( BoolProperty, CollectionProperty, EnumProperty, - FloatProperty, IntProperty, PointerProperty, StringProperty, ) from bpy.types import Material, PropertyGroup + +from ...shared.helpers import get_collection_export_operator_from_context from ...shared.types import ExportSpaceMixin, TransformMixin, PsxBoneExportMixin object_eval_state_items = ( diff --git a/io_scene_psk_psa/shared/helpers.py b/io_scene_psk_psa/shared/helpers.py index 735258d..6ad2116 100644 --- a/io_scene_psk_psa/shared/helpers.py +++ b/io_scene_psk_psa/shared/helpers.py @@ -1,7 +1,7 @@ import bpy from collections import Counter from typing import List, Iterable, Optional, Dict, Tuple, cast as typing_cast -from bpy.types import Armature, AnimData, Object, ArmatureModifier +from bpy.types import Armature, AnimData, Collection, Context, Object, ArmatureModifier, SpaceProperties from mathutils import Matrix, Vector, Quaternion as BpyQuaternion from .data import Vector3, Quaternion from ..shared.data import PsxBone @@ -480,3 +480,23 @@ def get_armatures_for_mesh_objects(mesh_objects: Iterable[Object]): if armature_object is not None: armature_objects.add(armature_object) yield from armature_objects + + +def get_collection_from_context(context: Context) -> Optional[Collection]: + if context.space_data is None or context.space_data.type != 'PROPERTIES': + return None + space_data = typing_cast(SpaceProperties, context.space_data) + if space_data.use_pin_id: + return typing_cast(Collection, space_data.pin_id) + else: + return context.collection + + +def get_collection_export_operator_from_context(context: Context) -> Optional[object]: + collection = get_collection_from_context(context) + if collection is None or collection.active_exporter_index is None: + return None + if 0 > collection.active_exporter_index >= len(collection.exporters): + return None + exporter = collection.exporters[collection.active_exporter_index] + return exporter.export_properties \ No newline at end of file