Artelia / NIMPHS

Blender add-on. Numerous Instruments to Manipulate and Post-process Hydraulic Simulations (OpenFOAM, TELEMAC).
https://Artelia.github.io/NIMPHS
GNU General Public License v3.0
13 stars 2 forks source link

Crash when disabling add-on #5

Closed Failxxx closed 2 years ago

Failxxx commented 2 years ago

Short description

Blender crashes after disabling the add-on from preferences.

Current behavior

When you go in the editor preferences and disable the add-on, after closing the window, blender simply crashes.

Expected behavior

We would like Blender not to crash after disabling the add-on!

Steps to reproduce

System information

Add-on: v0.4.0 Blender: 3.0.1 Python: 3.9.12 OS: Ubuntu 22.04.1 LTS

Logs and screenshots

Crash report:

# Blender 3.0.1, Commit date: 2022-01-25 17:19, Hash dc2d18018171
bpy.data.window_managers["WinMan"].addon_search = "nimp"  # Property
Python: Traceback (most recent call last):
  File "/home/hnum/.config/blender/3.0/scripts/addons/nimphs/properties/utils/others.py", line 51, in new_draw
    m_op_value = context.scene.nimphs.m_op_value
AttributeError: 'NIMPHS_Scene' object has no attribute 'm_op_value'

location: <unknown location>:-1
  # Error
Preferences saved  # Info

# backtrace
./blender(BLI_system_backtrace+0x20) [0xb0b3dd0]
./blender() [0x10bad3a]
/lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7f33368e2520]
[0x7f32b03228b0]

# Python backtrace
  File "/home/hnum/.config/blender/3.0/scripts/addons/nimphs/properties/utils/others.py", line 51 in new_draw
Failxxx commented 2 years ago

The problem seems to come from the fact the original draw function is not restored when the add-on is unregistered.

Thus, in __init__.py we could add these lines:

from bpy.types import VIEW3D_HT_tool_header
from nimphs.properties.utils.others import info_header_draw

...

# Reset draw function of VIEW_3D header
global info_header_draw
VIEW3D_HT_tool_header.draw = info_header_draw

And in nimphs/properties/utils/others.py:

# A variable where we can store the original draw function
def info_header_draw(s, c) -> None:  # noqa: D103
    return None

# State variable to prevent saving multiple times original VIEW_3D header draw function
global info_header_draw_saved
info_header_draw_saved = False

# Inspired by: https://blog.michelanders.nl/2017/04/how-to-add-progress-indicator-to-the-info-header-in-blender.html
def register_custom_progress_bar() -> None:
    """Register the custom progress bar."""

    # Save the original draw method of Info header
    global info_header_draw
    global info_header_draw_saved
    if not info_header_draw_saved:
        info_header_draw = deepcopy(VIEW3D_HT_tool_header.draw)
        info_header_draw_saved = True

    # Create a new draw function
    def new_info_header_draw(self, context):
        global info_header_draw
        # First call to the original function
        info_header_draw(self, context)

        # Then add the prop that acts as a progress bar
        scene_prop = context.scene.get("nimphs", None)
        if scene_prop is None:
            return

        m_op_value = context.scene.nimphs.get("m_op_value", -1)
        if m_op_value >= 0.0 and m_op_value <= 100.0:
            self.layout.separator()
            text = context.scene.nimphs.m_op_label
            self.layout.prop(context.scene.nimphs, "m_op_value", text=text, slider=True)

    # Replace the draw function by our new function
    # Blender crashes sometimes when using the progress bar in dev mode
    if not DEV_MODE:
        VIEW3D_HT_tool_header.draw = new_info_header_draw

However, this does not work because a part of the header is removed when restoring the original draw function, but comes back when the add-on is re-registered: Screenshot from 2022-09-01 09-35-26 Screenshot from 2022-09-01 09-35-37