-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoperators.py
More file actions
209 lines (158 loc) · 6.25 KB
/
Copy pathoperators.py
File metadata and controls
209 lines (158 loc) · 6.25 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
# SPDX-License-Identifier: GPL-3.0-or-later
import bpy
from pathlib import Path
import os
from .utils import (
increment_version,
update_file_list,
load_handler,
open_current_dir,
update_file_list
)
# Operator class to save the blend file with Increased Versioning and Publish
class SWV_OT_SaveIncrement(bpy.types.Operator):
bl_idname = "swv.save_increment"
bl_label = "Save Increment"
bl_description = "Save file and increment the version number"
@classmethod
def poll(cls, context):
saved = bpy.context.blend_data.is_saved
if not saved:
cls.poll_message_set(
"Blend file is not saved. Please save the file first.")
return False
prefs = context.preferences.addons[__package__].preferences
filename = bpy.path.basename(bpy.data.filepath)
if prefs.publish_suffix in filename:
cls.poll_message_set(
"This file is already published. Should not be incremented")
return False
return True
def execute(self, context):
# Get the file path, name and directory
filepath = Path(bpy.data.filepath)
filename = filepath.stem
directory = filepath.parent
# Get the user-defined suffix from preferences
prefs = context.preferences.addons[__package__].preferences
version_suffix = prefs.version_suffix
# Increment the version number in the filename
filename, incremented_version = increment_version(
filename, version_suffix)
# Save the current with incremented version_suffix
inc_path = f"{filename}{incremented_version}.blend"
new_filepath = directory / inc_path
bpy.ops.wm.save_as_mainfile(filepath=str(new_filepath))
self.report({"INFO"}, f"Saved {inc_path}")
# Update file list immediately
update_file_list(context)
return {"FINISHED"}
# Operator class to save the blend file with Increased Versioning and Publish
class SWV_OT_SavePublish(bpy.types.Operator):
bl_idname = "swv.save_publish"
bl_label = "Save Publish"
bl_description = "Make copy of current file to published file"
@classmethod
def poll(cls, context):
saved = bpy.context.blend_data.is_saved
if not saved:
cls.poll_message_set(
"Blend file is not saved. Please save the file first.")
return False
prefs = context.preferences.addons[__package__].preferences
filename = bpy.path.basename(bpy.data.filepath)
if prefs.publish_suffix in filename:
cls.poll_message_set("This file is already published.")
return False
return True
def execute(self, context):
# Get the file path, name and directory
filepath = Path(bpy.data.filepath)
filename = filepath.stem
directory = filepath.parent
# Get the user-defined suffixes from preferences
prefs = context.preferences.addons[__package__].preferences
version_suffix = prefs.version_suffix
publish_suffix = prefs.publish_suffix
filename, incremented_version = increment_version(
filename, version_suffix, increment=False)
# Update the published file
published_filepath = directory / f"{filename}{publish_suffix}.blend"
bpy.ops.wm.save_as_mainfile(filepath=str(published_filepath))
# Save the current with incremented version_suffix
inc_path = f"{filename}{incremented_version}.blend"
new_filepath = directory / inc_path
bpy.ops.wm.save_as_mainfile(filepath=str(new_filepath))
self.report({"INFO"}, f"Published {inc_path}")
# Update file list immediately
update_file_list(context)
return {"FINISHED"}
class SWV_OT_RefreshFileList(bpy.types.Operator):
bl_idname = "swv.refresh_file_list"
bl_label = "Refresh File List"
bl_description = "Refresh the list of versioned files"
@classmethod
def poll(cls, context):
saved = bpy.context.blend_data.is_saved
if not saved:
cls.poll_message_set(
"Blend file is not saved. Please save the file first.")
return saved
def execute(self, context):
update_file_list(context)
return {'FINISHED'}
class SWV_OT_OpenSelectedFile(bpy.types.Operator):
bl_idname = "swv.open_selected_file"
bl_label = "Open Selected File"
bl_description = "Open the selected file in the current Blender instance"
filepath: bpy.props.StringProperty(subtype="FILE_PATH")
@classmethod
def poll(cls, context):
saved = bpy.context.blend_data.is_saved
if not saved:
cls.poll_message_set(
"Blend file is not saved. Please save the file first.")
return saved
def execute(self, context):
directory = os.path.dirname(bpy.data.filepath)
full_path = os.path.join(directory, self.filepath)
if not os.path.exists(full_path):
self.report({'ERROR'}, f"File not found: {full_path}")
return {'CANCELLED'}
bpy.ops.wm.open_mainfile(
'INVOKE_DEFAULT',
filepath=full_path,
display_file_selector=False)
return {'FINISHED'}
class SVM_OT_open_current_dir(bpy.types.Operator):
bl_idname = "swv.open_current_dir"
bl_label = "Open Current Directory"
bl_description = "Open Current Directory"
@classmethod
def poll(cls, context):
saved = bpy.context.blend_data.is_saved
if not saved:
cls.poll_message_set(
"Blend file is not saved. Please save the file first.")
return saved
def execute(self, context):
open_current_dir()
return {'FINISHED'}
classes = (
SWV_OT_SaveIncrement,
SWV_OT_SavePublish,
SWV_OT_RefreshFileList,
SWV_OT_OpenSelectedFile,
SVM_OT_open_current_dir,
)
# Register the add-on
def register():
for bl_class in classes:
bpy.utils.register_class(bl_class)
bpy.app.handlers.load_post.append(load_handler)
# Unregister the add-on
def unregister():
for bl_class in reversed(classes):
bpy.utils.unregister_class(bl_class)
if load_handler in bpy.app.handlers.load_post:
bpy.app.handlers.load_post.remove(load_handler)