-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpanels.py
More file actions
254 lines (193 loc) · 7.7 KB
/
Copy pathpanels.py
File metadata and controls
254 lines (193 loc) · 7.7 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# SPDX-License-Identifier: GPL-3.0-or-later
import bpy
from bpy.app.handlers import persistent
from .operators import (
SWV_OT_SaveIncrement,
SWV_OT_SavePublish
)
def update_panel(self, context):
# Check if we're already updating to prevent recursion
if getattr(update_panel, "is_updating", False):
return
update_panel.is_updating = True
try:
# Unregister the panel
try:
bpy.utils.unregister_class(SWV_PT_SaveWithVersioningPanel)
except:
pass
# Update the bl_category
prefs = context.preferences.addons[__package__].preferences
SWV_PT_SaveWithVersioningPanel.bl_category = prefs.panel_category
# Re-register the panel
bpy.utils.register_class(SWV_PT_SaveWithVersioningPanel)
# Save the preference
context.preferences.addons[__package__].preferences.panel_category = prefs.panel_category
finally:
update_panel.is_updating = False
class SWV_UL_FileList(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
if self.layout_type in {'DEFAULT', 'COMPACT'}:
row = layout.row(align=True)
text = " | " * (item.indent - 1) + item.name
row.label(text=text, icon='FILE_BLEND')
# Add publish icon if the file is published
if item.is_published:
row.label(text="", icon='ANTIALIASED')
# Add a small button to open the file
op = row.operator("swv.open_selected_file", text="",
icon='FILEBROWSER', emboss=True)
op.filepath = item.name
def filter_items(self, context, data, propname):
items = getattr(data, propname)
helper_funcs = bpy.types.UI_UL_list
# Default sort
sorted_indices = helper_funcs.sort_items_by_name(items, "name")
# Filter
filtered_indices = helper_funcs.filter_items_by_name(
self.filter_name, self.bitflag_filter_item, items, "name",
reverse=self.use_filter_sort_reverse)
return filtered_indices, sorted_indices
class SWV_PG_FileItem(bpy.types.PropertyGroup):
name: bpy.props.StringProperty()
indent: bpy.props.IntProperty()
is_published: bpy.props.BoolProperty()
class SWV_PT_SaveWithVersioningPanel(bpy.types.Panel):
bl_label = "Save with Versioning"
bl_idname = "SWV_PT_SaveWithVersioningPanel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Tool' # Default category
bl_order = 0 # This will be set dynamically
@classmethod
def poll(cls, context):
tool_order = 100000
item_order = 100000
prefs = context.preferences.addons[__package__].preferences
if hasattr(prefs, "panel_category"):
cls.bl_category = prefs.panel_category
# Set bl_order based on the category
if cls.bl_category == 'Tool':
# High number for Tool category (to be at the end)
cls.bl_order = tool_order
else:
# Low number for Item category (to be at the end)
cls.bl_order = -item_order
else:
cls.bl_category = "Tool"
cls.bl_order = tool_order
return True
def draw(self, context):
layout = self.layout
scene = context.scene
# Add save buttons
row = layout.row()
row.operator("swv.save_increment", text="Increment", icon="PLUS")
row.operator("swv.save_publish", text="Publish", icon="ANTIALIASED")
# Add refresh button
row = layout.row()
# Open current directory button
row.operator("swv.open_current_dir",
text="Open Current Directory", icon="FILE_FOLDER")
row.operator("swv.refresh_file_list", text="", icon="FILE_REFRESH")
# Add file list
row = layout.row()
row.template_list("SWV_UL_FileList", "", scene,
"file_list", scene, "file_list_index", rows=10)
class SWV_PT_VersioningAddonPreferences(bpy.types.AddonPreferences):
bl_idname = __package__
version_suffix: bpy.props.StringProperty(
name="version_suffix",
description="Suffix for the versioning",
default="_v001"
)
publish_suffix: bpy.props.StringProperty(
name="publish suffix",
description="Suffix for the published file",
default="_published"
)
panel_category: bpy.props.EnumProperty(
name="Panel Category",
description="Choose the category for the Save with Versioning panel",
items=[
('Item', "Item", "Place panel in the Item category"),
('Tool', "Tool", "Place panel in the Tool category"),
('View', "View", "Place panel in the View category"),
('Edit', "Edit", "Place panel in the Edit category"),
],
default='Item',
update=update_panel
)
def draw(self, context):
layout = self.layout
layout.label(text="Version Suffix Settings:", icon="PREFERENCES")
box = layout.box()
box.label(text="Version Suffix (e.g., '_v001'):")
box.prop(self, "version_suffix", text="")
box.label(text="Publish Suffix (e.g., '_published'):")
box.prop(self, "publish_suffix", text="")
layout.label(text="Panel Location:", icon="WINDOW")
layout.prop(self, "panel_category")
# Function to add the "Save With Versioning" button to the header
def save_versioning_button(self, context):
self.layout.operator(
SWV_OT_SaveIncrement.bl_idname,
text="",
icon="PLUS"
)
self.layout.operator(
SWV_OT_SavePublish.bl_idname,
text="",
icon="ANTIALIASED"
)
classes = (
SWV_UL_FileList,
SWV_PG_FileItem,
SWV_PT_SaveWithVersioningPanel,
SWV_PT_VersioningAddonPreferences,
)
@persistent
def load_handler(dummy):
# Update panel category on file load
bpy.app.timers.register(lambda: update_panel(None, bpy.context))
def register():
for bl_class in classes:
bpy.utils.register_class(bl_class)
# Register file list properties
bpy.types.Scene.file_list = bpy.props.CollectionProperty(
type=SWV_PG_FileItem)
bpy.types.Scene.file_list_index = bpy.props.IntProperty()
bpy.types.VIEW3D_HT_header.append(save_versioning_button)
bpy.app.handlers.load_post.append(load_handler)
# Load saved preferences
addon_prefs = bpy.context.preferences.addons[__package__].preferences
if addon_prefs:
update_panel(addon_prefs, bpy.context)
def unregister():
for bl_class in reversed(classes):
bpy.utils.unregister_class(bl_class)
bpy.types.VIEW3D_HT_header.remove(save_versioning_button)
# Unregister file list properties
del bpy.types.Scene.file_list
del bpy.types.Scene.file_list_index
bpy.app.handlers.load_post.remove(load_handler)
def update_panel(self, context):
# Check if we're already updating to prevent recursion
if getattr(update_panel, "is_updating", False):
return
update_panel.is_updating = True
try:
# Unregister the panel
try:
bpy.utils.unregister_class(SWV_PT_SaveWithVersioningPanel)
except:
pass
# Update the bl_category
prefs = context.preferences.addons[__package__].preferences
SWV_PT_SaveWithVersioningPanel.bl_category = prefs.panel_category
# Re-register the panel
bpy.utils.register_class(SWV_PT_SaveWithVersioningPanel)
# Save the preference
context.preferences.addons[__package__].preferences.panel_category = prefs.panel_category
finally:
update_panel.is_updating = False