1
$\begingroup$

I made an addon for blender (3,3,1). It works. I managed to install it. But after restarting the program, it stops working. At least the checkbox in the edit is active. I just can't find a solution to the problem.

bl_info = {
    "name": "Render Finish Sound",
    "author": "WhiteFlame",
    "version": (1, 0),
    "blender": (3, 3, 1),
    "description": "Plays a sound when rendering is complete.",
    "warning": "",
    "doc_url": "",
    "category": "Render",
}

import bpy
import platform
import subprocess

def play_sound(file_path):
    os_platform = platform.system()
    
    if os_platform == "Windows":
        # Playing sound in Windows with winsound
        import winsound
        winsound.PlaySound(file_path, winsound.SND_FILENAME)

def render_complete_handler(dummy):
    # Play a sound signal when rendering is finished
    sound_file = r'C:\Users\WhiteFlame\Music\For blender\Blender Notification.wav'

    # Check if there is a handler in the render_complete list
    if render_complete_handler in bpy.app.handlers.render_complete:
        # Call the function to play the sound
        play_sound(sound_file)

def register():
    bpy.app.handlers.render_complete.append(render_complete_handler)

def unregister():
    if render_complete_handler in bpy.app.handlers.render_complete:
        bpy.app.handlers.render_complete.remove(render_complete_handler)

if __name__ == "__main__":
    register()
```
$\endgroup$
1
  • $\begingroup$ If you open the WIndow::Console, does it say anything... error message? $\endgroup$
    – james_t
    Sep 19, 2023 at 23:11

1 Answer 1

0
$\begingroup$

The problem is that bpy.app.handlers.render_complete.append(render_complete_handler) will not persist, even when you create a new file. To remedy this issue, you can use the @persistent decorator

bl_info = {
    "name": "Render Finish Sound",
    "author": "WhiteFlame",
    "version": (1, 0),
    "blender": (3, 3, 1),
    "description": "Plays a sound when rendering is complete.",
    "warning": "",
    "doc_url": "",
    "category": "Render",
}

import bpy
import platform
import subprocess
from bpy.app.handlers import persistent

def play_sound(file_path):

    if platform.system() == "Windows":
        import winsound
        winsound.PlaySound(file_path, winsound.SND_FILENAME)

@persistent
def render_complete_handler(dummy):
    sound_file = r'C:\Users\WhiteFlame\Music\For blender\Blender Notification.wav'

    if render_complete_handler in bpy.app.handlers.render_complete:
        play_sound(sound_file)

def register():
    bpy.app.handlers.render_complete.append(render_complete_handler)

def unregister():
    if render_complete_handler in bpy.app.handlers.render_complete:
        bpy.app.handlers.render_complete.remove(render_complete_handler)

if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .