-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_incremental.py
115 lines (85 loc) · 3.14 KB
/
save_incremental.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "Save Incremental",
"author": "Laurenz Rasche",
"version": (1, 0, 0),
"blender": (2, 82, 0),
"description": "Enables incremental saving for projects",
"warning": "",
"wiki_url": "https://github.com/rush42",
"category": "System",
}
import bpy
from bpy.types import Operator, Menu
from bpy.utils import register_class
import os
def save_incremental( self, context ):
file_folder = os.path.dirname( bpy.data.filepath )
file_name = os.path.basename( bpy.data.filepath )
print(f"old name: {file_name}")
try:
if( file_name[ -6: ] == ".blend" ):
file_name = file_name[:-6]
else:
raise IndexError
except:
print("invalid file extension")
return -1
num_digits = 0
for c in file_name[::-1]:
if( c.isdigit() ):
num_digits += 1
else:
break
if( num_digits ):
version = int( file_name[-num_digits:] )
file_name = file_name[:-num_digits]
version += 1
else:
version = 1
num_digits = 2
version = str( version )
version = "0" * ( num_digits - len( version ) ) + version
file_name = f"{ file_name }{ version }.blend"
file_list = [file for file in os.listdir(file_folder) if file.count(".blend") and not file.count("blend1") ]
if( file_name in file_list ):
print("file already exists")
return -1
save_path = os.path.join( file_folder, file_name )
#bpy.ops.wm.save_as_mainfile()
bpy.ops.wm.save_as_mainfile(filepath=save_path)
return 0
class Save_Incremental( bpy.types.Operator ):
"""Save the current file with increased version number"""
bl_idname = "file.save_incremental"
bl_label = "Save Incremental"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
if( not( save_incremental(self, context) ) ):
return {'FINISHED'}
return {'CANCELLED'}
def menu_draw(self, context):
self.layout.operator( "file.save_incremental" )
def register():
bpy.utils.register_class( Save_Incremental )
bpy.types.TOPBAR_MT_file.prepend( menu_draw )
def unregister():
bpy.types.TOPBAR_MT_file.remove( menu_draw )
bpy.utils.unregister_class( Save_Incremental )
if __name__ == "__main__":
register()