-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshelf.py
executable file
·233 lines (181 loc) · 7.05 KB
/
shelf.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from bpy.types import Context
from pathlib import Path
import bpy
from bpy.props import (
BoolProperty,
CollectionProperty,
IntProperty,
FloatProperty,
StringProperty,
)
from bpy.types import PropertyGroup
from . import catalogue
########################################################################################
# Update functions
########################################################################################
def update_directory(shelf: Shelf, context: Context):
"""
Re-scans scripts on any directory change. Saves userprefs.
Parameters:
- shelf (Shelf)
- context (Context)
"""
if shelf.directory:
# Make sure the path is normalized
posix_path = Path(shelf.directory).resolve().as_posix()
# Set the posix path; this will trigger another update so return
if shelf.directory != posix_path:
shelf.directory = posix_path
return
# Re-initialize scripts
shelf.initialize_scripts()
# Save user preferences
bpy.ops.wm.save_userpref()
def update_save_userpref(shelf: Shelf, context: Context):
"""
Save userprefs on update.
Parameters:
- shelf (Shelf)
- context (Context)
"""
bpy.ops.wm.save_userpref()
########################################################################################
# Script snippet class
########################################################################################
@catalogue.bpy_register
class Script(PropertyGroup):
"""Representation of a single script within a shelf"""
display_name: StringProperty(name="Name")
icon: StringProperty(name="Icon", default="NONE")
is_available: BoolProperty(name="Is Available", default=True)
name: StringProperty(name="File Name")
########################################################################################
# Shelf class
########################################################################################
@catalogue.bpy_register
class Shelf(PropertyGroup):
"""Single shelf, directory containing scripts to load and display settings"""
align: BoolProperty(name="Align Buttons")
columns: IntProperty(name="Columns", default=1, min=1, soft_max=8)
directory: StringProperty(
name="Directory",
subtype="DIR_PATH",
update=update_directory,
)
enabled_view_3d: BoolProperty(name="3D Viewport", default=True)
enabled_image_editor: BoolProperty(name="Image Editor")
enabled_uv: BoolProperty(name="UV Editor")
enabled_compositornodetree: BoolProperty(name="Compositor")
enabled_texturenodetree: BoolProperty(name="Texture Node Editor")
enabled_geometrynodetree: BoolProperty(name="Geometry Node Editor")
enabled_shadernodetree: BoolProperty(name="Shader Editor")
enabled_sequence_editor: BoolProperty(name="Video Sequencer")
enabled_clip_editor: BoolProperty(name="Movie Clip Editor")
enabled_dopesheet: BoolProperty(name="Dope Sheet")
enabled_timeline: BoolProperty(name="Timeline")
enabled_fcurves: BoolProperty(name="Graph Editor")
enabled_drivers: BoolProperty(name="Drivers")
enabled_nla_editor: BoolProperty(name="Nonlinear Animation")
enabled_text_editor: BoolProperty(name="Text Editor")
enabled_spreadsheet: BoolProperty(name="Spreadsheet")
height: FloatProperty(name="Button Height", default=1.0, min=0.5, soft_max=8.0)
icon: StringProperty(name="Icon", default="NONE")
is_available: BoolProperty(name="Is Available")
name: StringProperty(name="Name")
scripts: CollectionProperty(type=Script, name="Scripts")
show_scripts: BoolProperty(name="Show Scripts", default=True)
def exists(self) -> bool:
"""
Checks whether this folder exists and sets 'is_available' flag.
Returns:
- bool: Whether this folder exists at given location
"""
if self.directory and Path(self.directory).is_dir():
self.is_available = True
return True
self.is_available = False
return False
def initialize_scripts(self):
"""
Scan the script directory and initiate a script object for each script found.
"""
if TYPE_CHECKING:
existing_script: Script
script: Script
# Disable all scripts
[setattr(script, "is_available", False) for script in self.scripts]
# Check directory
if not self.exists():
self.is_available = False
return
self.is_available = True
# Iterate directory and find python scripts
for script_file in sorted(Path(self.directory).iterdir()):
if not script_file.suffix == ".py":
continue
# Find existing script
script = None
for existing_script in self.scripts:
if existing_script.name == script_file.name:
script = existing_script
script.is_available = True
# Create a new script
if not script:
script = self.scripts.add()
script.name = script_file.name
script.display_name = script_file.stem
def is_visible(self, context: Context) -> bool:
"""
Returns:
- bool: Whether this shelf should be drawn within the given context
"""
# Always draw in preferences
area_type = context.area.ui_type
if area_type == "PREFERENCES":
return True
# Check for enabled flag
attribute_name = f"enabled_{area_type.lower()}"
if (
self.is_available
and hasattr(self, attribute_name)
and getattr(self, attribute_name)
):
return True
return False
def path_is_in_shelf(self, path: str | Path) -> bool:
"""
Check if given path is located within the shelf directory.
Parameters:
- path (str | Path): Path to check
Returns:
- bool: Whether the given path is relative to this shelf
"""
return self.directory in Path(path).resolve().as_posix()
def script_exists(self, script: int | str) -> bool:
"""
Checks whether a script exists and sets its 'is_available' flag.
Parameters:
- script (int | str): Script index or file name
Returns:
- bool: Whether this script exists at expected path or not
"""
if TYPE_CHECKING:
script: Script
script = self.scripts[script]
if self.script_path(script=script).exists():
script.is_available = True
return True
script.is_available = False
return False
def script_path(self, script: int | str) -> Path:
"""
Generate a path object for given script.
Parameters:
- script (int | str): Script index or file name
Returns:
- Path
"""
return Path(self.directory, self.scripts[script].name)