-
Notifications
You must be signed in to change notification settings - Fork 0
/
regutil.py
78 lines (59 loc) · 1.74 KB
/
regutil.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
78
"""Utilities for registering bpy types, menus, etc with decorators"""
from bpy.props import PointerProperty
from bpy.types import Menu
from bpy.utils import register_class, unregister_class
classes = []
menus = []
props = []
def menu(cls):
'''Adds a draw function to menu class.
Function must take 2 args (self, context)'''
def decorator(menu_obj):
if issubclass(menu_obj, Menu):
menus.append((cls, menu_obj.menu_draw))
else:
menus.append((cls, menu_obj))
return menu_obj
return decorator
def custom_prop_group(cls, id: str,):
def decorator(prop_cls):
props.append((cls, id, prop_cls,))
return prop_cls
return decorator
def bpy_register(cls):
global classes
classes.append(cls)
return cls
def register():
global classes
global menus
global props
# Register classes
for c in classes:
register_class(c)
# Set custom Property Groups
for owner, prop_id, prop_cls in props:
if prop_cls not in classes:
register_class(prop_cls)
setattr(owner,
prop_id,
PointerProperty(name=prop_id, type=prop_cls))
# Add draw funcs to menus
for menu, submenu in menus:
menu.append(submenu)
def unregister():
global classes
global menus
global props
# Remove custom Property Groups
for owner, prop_id, prop_cls in props:
delattr(owner, prop_id)
print(f'Deleted GroupProperty {prop_id} of {owner}')
if prop_cls not in classes:
unregister_class(prop_cls)
# Remove draw funcs from menus
for menu, func in menus:
menu.remove(func)
# Unregister classes
for c in classes:
unregister_class(c)