Math Vis Console - fixed errors with invalid objects in console locals #104958

Closed
Andrej wants to merge 3 commits from Andrej730/blender-addons:math_vis_fix_invalid_objects into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Contributor

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

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 ```
Andrej added 1 commit 2023-10-16 10:27:18 +02:00
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

```
Andrej requested review from Campbell Barton 2023-10-16 10:27:32 +02:00
Damien Picard reviewed 2023-11-07 11:05:40 +01:00
@ -3,6 +3,7 @@
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
from _bpy import types as bpy_types
Member

It looks like bpy.types works just as well, why not use that?

It looks like `bpy.types` works just as well, why not use that?
Author
Contributor

Agreed, fixed.

Agreed, fixed.
Andrej730 marked this conversation as resolved
Andrej added 1 commit 2023-11-07 11:12:25 +01:00
Andrej requested review from Bastien Montagne 2023-11-08 14:23:18 +01:00
Andrej requested review from Jacques Lucke 2023-11-08 14:23:23 +01:00
Bastien Montagne requested changes 2023-11-08 15:51:45 +01:00
Bastien Montagne left a comment
Owner

Checking for instances of ID is not going to be enough, one can store all kind of other RNA structs in variables, that would become invalid once owner ID (or something else) is freed or re-allocated.

Something like that should be more general and safe:

# 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
Checking for instances of `ID` is not going to be enough, one can store all kind of other RNA structs in variables, that would become invalid once owner ID (or something else) is freed or re-allocated. Something like that should be more general and safe: ``` # 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 ```
Andrej added 1 commit 2023-11-08 16:06:30 +01:00
Andrej requested review from Bastien Montagne 2023-11-08 16:07:15 +01:00
Bastien Montagne approved these changes 2023-11-08 16:18:04 +01:00
Bastien Montagne left a comment
Owner

Would expect this code to work, provided it has been tested LGTM.

Would expect this code to work, provided it has been tested LGTM.
Author
Contributor

Have been testing version with isinstance(var, bpy.types.ID): for awhile now without issues.
Tested the new version with hasattr(var, "bl_rna") on typical cases - no issues.

Have been testing version with `isinstance(var, bpy.types.ID):` for awhile now without issues. Tested the new version with `hasattr(var, "bl_rna")` on typical cases - no issues.
Author
Contributor

@mont29 any other changes needed or it can be merged?

@mont29 any other changes needed or it can be merged?

Would not mind @ideasman42 to have a look at this too, he understands this area of code better than I do...

Would not mind @ideasman42 to have a look at this too, he understands this area of code better than I do...

It should be possible to run isinstance(..) on any data without raising exceptions.
Committed an alternative fix blender/blender@e480b6a50c.

It should be possible to run `isinstance(..)` on any data without raising exceptions. Committed an alternative fix blender/blender@e480b6a50c159f2fbcd74f7335b437d4a232468b.
Campbell Barton closed this pull request 2024-03-11 10:32:06 +01:00

Pull request closed

Sign in to join this conversation.
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#104958
No description provided.