From c8907e5375f2654655a0a8a41943011572ca0d86 Mon Sep 17 00:00:00 2001 From: mmmrqs Date: Wed, 28 Sep 2022 21:11:38 -0300 Subject: [PATCH] Fixed issue to prevent multiple simultaneous instances --- bl_ui_widget_demo.py | 20 ++++++++++++++++---- demo_panel_op.py | 29 ++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/bl_ui_widget_demo.py b/bl_ui_widget_demo.py index f8e28dd..3421b8d 100644 --- a/bl_ui_widget_demo.py +++ b/bl_ui_widget_demo.py @@ -20,7 +20,7 @@ bl_info = {"name": "BL UI Widgets", "description": "UI Widgets to draw in the 3D view", "author": "Marcelo M. Marques", - "version": (1, 0, 2), + "version": (1, 0, 3), "blender": (2, 80, 75), "location": "View3D > side panel ([N]), [BL_UI_Widget] tab", "support": "COMMUNITY", @@ -32,6 +32,9 @@ # --- ### Change log +# v1.0.3 (09.28.2022) - by Marcelo M. Marques +# Fixed: issue with a 'context is incorrect' situation that would be caused by user calling 'Set_Demo_Panel' repeatedly and too fast + # v1.0.2 (09.25.2022) - by Marcelo M. Marques # Added: 'is_quadview_region' function to identify whether screen is in QuadView mode and if yes to return the corresponding area and region. # Added: 'btnRemoTime' session variable to hold the clock time which is constantly updated by the 'terminate_execution' function in demo_panel_op.py @@ -113,9 +116,11 @@ def invoke(self, context, event): def execute(self, context): if context.scene.var.RemoVisible and int(time.time()) - context.scene.var.btnRemoTime <= 1: + # If it is active then set its visible status to False so that it be closed and reset the button label context.scene.var.btnRemoText = "Open Remote Control" context.scene.var.RemoVisible = False else: + # If it is not active then set its visible status to True so that it be opened and reset the button label context.scene.var.btnRemoText = "Close Remote Control" context.scene.var.RemoVisible = True is_quadview, area, region = is_quadview_region(context) @@ -123,9 +128,16 @@ def execute(self, context): override = bpy.context.copy() override["area"] = area override["region"] = region - context.scene.var.objRemote = bpy.ops.object.dp_ot_draw_operator(override, 'INVOKE_DEFAULT') - else: - context.scene.var.objRemote = bpy.ops.object.dp_ot_draw_operator('INVOKE_DEFAULT') + # Had to put this "try/except" statement because when user clicked repeatedly too fast + # on the operator's button it would crash the call due to a context incorrect situation + try: + if is_quadview: + context.scene.var.objRemote = bpy.ops.object.dp_ot_draw_operator(override, 'INVOKE_DEFAULT') + else: + context.scene.var.objRemote = bpy.ops.object.dp_ot_draw_operator('INVOKE_DEFAULT') + except: + return {'CANCELLED'} + return {'FINISHED'} diff --git a/demo_panel_op.py b/demo_panel_op.py index f6b88ea..42910d7 100644 --- a/demo_panel_op.py +++ b/demo_panel_op.py @@ -20,7 +20,7 @@ bl_info = {"name": "BL UI Widgets", "description": "UI Widgets to draw in the 3D view", "author": "Marcelo M. Marques (fork of Jayanam's original project)", - "version": (1, 0, 3), + "version": (1, 0, 4), "blender": (2, 80, 75), "location": "View3D > viewport area", "support": "COMMUNITY", @@ -32,6 +32,9 @@ # --- ### Change log +# v1.0.4 (09.28.2022) - by Marcelo M. Marques +# Added: Logic to 'poll' classmethod that prevents the panel to be opened in multiple simultaneous instances. + # v1.0.3 (09.25.2022) - by Marcelo M. Marques # Added: Logic to 'on_invoke' function to identify when in QuadView mode and to position the remote panel appropriately so it # does not get stuck lost out of visible region. @@ -102,7 +105,15 @@ class DP_OT_draw_operator(BL_UI_OT_draw_operator): # in: bl_ui_draw_op.py ## @classmethod def poll(cls, context): # Show this panel in View_3D only - return (context.space_data.type == 'VIEW_3D') + if context.space_data.type != 'VIEW_3D': + return False + # Prevents multiple instances of panel + try: + if context.scene.var.RemoVisible and int(time.time()) - context.scene.var.btnRemoTime <= 1: + return False + except: + return False + return True def __init__(self): @@ -405,14 +416,18 @@ def terminate_execution(self, area, region, event): If not included here the function in the superclass just returns 'False' and no termination is executed. When 'True" is returned below, the execution is auto terminated and the 'Remote Control' panel closes itself. ''' + ''' + BEWARE THAT ARGUMENTS 'AREA' AND/OR 'REGION' CAN BE EQUAL TO "NONE" + ''' if self.panel.quadview and area is None: bpy.context.scene.var.RemoVisible = False else: - if area.type == 'VIEW_3D': - # If user switches between regular and QuadView display modes, the panel is automatically closed - is_quadview = (len(area.spaces.active.region_quadviews) > 0) - if self.panel.quadview != is_quadview: - bpy.context.scene.var.RemoVisible = False + if not area is None: + if area.type == 'VIEW_3D': + # If user switches between regular and QuadView display modes, the panel is automatically closed + is_quadview = (len(area.spaces.active.region_quadviews) > 0) + if self.panel.quadview != is_quadview: + bpy.context.scene.var.RemoVisible = False if event.type == 'TIMER' and bpy.context.scene.var.RemoVisible: # Update the remote panel "clock marker". This marker is used to keep track if the remote panel is