Skip to content

Commit

Permalink
Fixed issue to prevent multiple simultaneous instances
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmrqs committed Sep 29, 2022
1 parent d9c9551 commit c8907e5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
20 changes: 16 additions & 4 deletions bl_ui_widget_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -113,19 +116,28 @@ 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)
if is_quadview:
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'}


Expand Down
29 changes: 22 additions & 7 deletions demo_panel_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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.
Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c8907e5

Please sign in to comment.