-
Notifications
You must be signed in to change notification settings - Fork 1
/
preferences.py
executable file
·144 lines (116 loc) · 4.11 KB
/
preferences.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from bpy.types import Context, UILayout
import bpy
from bpy.props import BoolProperty, CollectionProperty
from bpy.types import AddonPreferences
from . import catalogue, shelf
########################################################################################
# Add-on root
########################################################################################
@catalogue.bpy_register
class Preferences(AddonPreferences):
"""Add-on preferences"""
bl_idname = __package__
is_locked: BoolProperty(name="(Un)Lock Shelves", update=shelf.update_save_userpref)
shelves: CollectionProperty(type=shelf.Shelf, name="Directories")
def clean(self):
"""
Remove all unavailable shelves and scripts.
"""
if TYPE_CHECKING:
script: shelf.Script
shelf: shelf.Shelf
for i_sh, shelf in reversed(list(enumerate(self.shelves))):
if not shelf.is_available:
self.shelves.remove(i_sh)
for i_sc, script in reversed(list(enumerate(shelf.scripts))):
if not script.is_available:
shelf.scripts.remove(i_sc)
def draw(self, context: Context):
"""
Draw add-on the preferences panel. Displays an overview of all shelves with all
their respective properties and operators laid out.
Parameters:
- context (Context)
"""
if TYPE_CHECKING:
layout: UILayout
shelf: shelf.Shelf
# Add shelf button
layout = self.layout
col_shelves = layout.column(align=True)
col_shelves.row(align=True).operator(
operator="shelfmade.add_shelf",
icon="ADD",
)
if not self.shelves:
return
# Draw all shelves
box_shelves = col_shelves.box()
box_shelves.separator()
for i, shelf in enumerate(self.shelves):
row_shelf = box_shelves.row()
# Icon
row_name = row_shelf.row(align=True)
row_name.operator(
operator="shelfmade.set_shelf_icon",
text="",
icon="BLANK1" if shelf.icon == "NONE" else shelf.icon,
).index = i
# Name
row_name.prop(data=shelf, property="name", text="")
# Visibility
row_shelf.operator(
operator="shelfmade.edit_shelf_visibility",
text="",
icon="VIS_SEL_11",
).index = i
# Path
row_shelf.prop(data=shelf, property="directory", text="")
# Move
row_move = row_shelf.row(align=True)
row_up = row_move.row(align=True)
if i == 0:
row_up.enabled = False
up = row_up.operator(
operator="shelfmade.move_shelf",
text="",
icon="TRIA_UP",
)
up.direction = "UP"
up.index = i
row_down = row_move.row(align=True)
if i == len(self.shelves) - 1:
row_down.enabled = False
down = row_down.operator(
operator="shelfmade.move_shelf",
text="",
icon="TRIA_DOWN",
)
down.direction = "DOWN"
down.index = i
# Remove
row_shelf.operator(
operator="shelfmade.remove_shelf",
text="",
icon="X",
).index = i
def initialize_shelves(self):
"""
Scan the script directories and initiate a script object for each script found.
"""
if TYPE_CHECKING:
shelf: shelf.Shelf
# Scan all shelf directories
for shelf in self.shelves:
shelf.initialize_scripts()
@staticmethod
def this() -> Preferences:
"""
Preference class instance pointer for shortcuts.
Returns:
Preferences: bpy instance
"""
return bpy.context.preferences.addons[__package__].preferences