From 30930143efb40396765db3a341607ebec8440d8d Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 16 Oct 2023 13:23:05 +0500 Subject: [PATCH 1/3] Math Vis Console - fixed errors with invalid objects in console locals Noticed an error that sometimes gets in the way - it occurs if some blender object get's invalidated (whether when it's removed or it invalidated after undo). When addon mets this kind of object in console's `locals()` it stops working. Way to reproduce: 1. Default blender scene, make sure cube is selected 2. Run the code below in the console. 3. You won't be able to see the matrix visualization in viewport and you'll be able to see the console errors below in the system console. ``` a = C.object m = Matrix() bpy.data.objects.remove(C.object) ``` Traceback: ``` Traceback (most recent call last): File "\Blender\3.6\scripts\addons\space_view3d_math_vis\draw.py", line 72, in draw_callback_px data_matrix, data_quat, data_euler, data_vector, data_vector_array = utils.console_math_data() File "\Blender\3.6\scripts\addons\space_view3d_math_vis\utils.py", line 156, in console_math_data if isinstance(var, Matrix): ReferenceError: StructRNA of type Object has been removed ``` --- space_view3d_math_vis/utils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/space_view3d_math_vis/utils.py b/space_view3d_math_vis/utils.py index 28ceb554b..00dfadb8b 100644 --- a/space_view3d_math_vis/utils.py +++ b/space_view3d_math_vis/utils.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later import bpy +from _bpy import types as bpy_types def console_namespace(): @@ -154,6 +155,10 @@ def console_math_data(): disp, lock = state_prop.state if not disp: continue + + # avoid bpy types to avoid possible errors caused by invalidated objects + if isinstance(var, bpy_types.ID): + continue if isinstance(var, Matrix): if len(var.col) != 4 or len(var.row) != 4: -- 2.30.2 From bc9dfe14214dda32949acd14d81fce2e2fc5125f Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 7 Nov 2023 15:10:33 +0500 Subject: [PATCH 2/3] math vis console - simpler method to get check if object is ID --- space_view3d_math_vis/utils.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/space_view3d_math_vis/utils.py b/space_view3d_math_vis/utils.py index 00dfadb8b..0727a53c3 100644 --- a/space_view3d_math_vis/utils.py +++ b/space_view3d_math_vis/utils.py @@ -3,7 +3,6 @@ # SPDX-License-Identifier: GPL-2.0-or-later import bpy -from _bpy import types as bpy_types def console_namespace(): @@ -155,9 +154,9 @@ def console_math_data(): disp, lock = state_prop.state if not disp: continue - + # avoid bpy types to avoid possible errors caused by invalidated objects - if isinstance(var, bpy_types.ID): + if isinstance(var, bpy.types.ID): continue if isinstance(var, Matrix): -- 2.30.2 From c1099e6fde8e8d8936bfc819f718286a89d98c29 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Wed, 8 Nov 2023 20:06:02 +0500 Subject: [PATCH 3/3] more robust check for invalidated RNA data --- space_view3d_math_vis/utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/space_view3d_math_vis/utils.py b/space_view3d_math_vis/utils.py index 0727a53c3..526c563b5 100644 --- a/space_view3d_math_vis/utils.py +++ b/space_view3d_math_vis/utils.py @@ -155,8 +155,11 @@ def console_math_data(): if not disp: continue - # avoid bpy types to avoid possible errors caused by invalidated objects - if isinstance(var, bpy.types.ID): + # In case `var` is a RNA struct 'proxy', check if the underlying RNA data is still valid. + # It can become invalid due to Blender data being freed or re-allocated e.g. + try: + hasattr(var, "bl_rna") + except ReferenceError: continue if isinstance(var, Matrix): -- 2.30.2