From d4a58caafe1de6956361c45577c7b1d6595cd9ac Mon Sep 17 00:00:00 2001 From: Colin Basnett Date: Thu, 5 May 2022 20:56:52 -0700 Subject: [PATCH] The PSK and PSA import menu items will now be greyed out if in the wrong context, and a tooltip will be shown indicating what the issue is (e.g., having the wrong type of object selected) --- io_scene_psk_psa/psa/exporter.py | 27 ++++++++++++++++++++------- io_scene_psk_psa/psk/exporter.py | 9 +++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/io_scene_psk_psa/psa/exporter.py b/io_scene_psk_psa/psa/exporter.py index b669f8b..cbb1caa 100644 --- a/io_scene_psk_psa/psa/exporter.py +++ b/io_scene_psk_psa/psa/exporter.py @@ -159,6 +159,15 @@ class PsaExportOperator(Operator, ExportHelper): def __init__(self): self.armature = None + @classmethod + def poll(cls, context): + try: + cls._check_context(context) + except RuntimeError as e: + cls.poll_message_set((str(e))) + return False + return True + def draw(self, context): layout = self.layout pg = context.scene.psa_export @@ -245,17 +254,21 @@ class PsaExportOperator(Operator, ExportHelper): return True return False - def invoke(self, context, event): - pg = context.scene.psa_export - + @classmethod + def _check_context(cls, context): if context.view_layer.objects.active is None: - self.report({'ERROR_INVALID_CONTEXT'}, 'An armature must be selected') - return {'CANCELLED'} + raise RuntimeError('An armature must be selected') if context.view_layer.objects.active.type != 'ARMATURE': - self.report({'ERROR_INVALID_CONTEXT'}, 'The selected object must be an armature.') - return {'CANCELLED'} + raise RuntimeError('The selected object must be an armature') + def invoke(self, context, event): + try: + self._check_context(context) + except RuntimeError as e: + self.report({'ERROR_INVALID_CONTEXT'}, str(e)) + + pg = context.scene.psa_export self.armature = context.view_layer.objects.active # Populate actions list. diff --git a/io_scene_psk_psa/psk/exporter.py b/io_scene_psk_psa/psk/exporter.py index 3327940..809232e 100644 --- a/io_scene_psk_psa/psk/exporter.py +++ b/io_scene_psk_psa/psk/exporter.py @@ -102,6 +102,15 @@ class PskExportOperator(Operator, ExportHelper): return {'RUNNING_MODAL'} + @classmethod + def poll(cls, context): + try: + PskBuilder.get_input_objects(context) + except RuntimeError as e: + cls.poll_message_set(str(e)) + return False + return True + def draw(self, context): layout = self.layout scene = context.scene