-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
handlers.py
77 lines (52 loc) · 1.97 KB
/
handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import bpy
import logging
from bpy.app.handlers import persistent
logger = logging.getLogger(__name__)
_builtin_handlers = {}
# Utility functions to simplify registering bpy.app handlers
#
# Builtin handlers have to be registered and unregistered,
# call register_handlers after all modules are registered and
# vice versa when unregistering
#
# Example usage:
# from event_system import add_builtin_handler
#
# add_builtin_handler("save_pre", write_addon_version)
# add_builtin_handler("version_update", do_versioning)
def add_builtin_handler(event: str, callback):
"""
Add to bpy.app.handlers, gets (un)registered on addon enable or disabled.
Does not support registering handlers at runtime
"""
global _builtin_handlers
func = persistent(callback)
_builtin_handlers.setdefault(event, list()).append(func)
def register_handlers():
global _builtin_handlers
for handler_name in _builtin_handlers.keys():
msg = "Append <{}> builtin handlers: ".format(handler_name)
for cb in _builtin_handlers[handler_name]:
getattr(bpy.app.handlers, handler_name).append(cb)
msg += "\n - {}".format(cb.__name__)
logger.debug(msg)
def unregister_handlers():
global _builtin_handlers
for handler_name in _builtin_handlers.keys():
msg = "Remove <{}> builtin handlers: ".format(handler_name)
for cb in _builtin_handlers[handler_name]:
handler_list = getattr(bpy.app.handlers, handler_name)
if cb not in handler_list:
continue
msg += "\n - {}".format(cb.__name__)
handler_list.remove(cb)
logger.debug(msg)
def _setup_builtin_handlers():
from .versioning import write_addon_version, do_versioning
add_builtin_handler("version_update", do_versioning)
add_builtin_handler("save_pre", write_addon_version)
def register():
_setup_builtin_handlers()
register_handlers()
def unregister():
unregister_handlers()