-
Notifications
You must be signed in to change notification settings - Fork 2
/
blend_open_ops.py
254 lines (208 loc) · 8.56 KB
/
blend_open_ops.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import bpy
import os
import re
from os.path import basename, dirname, join, exists
from pathlib import Path
import subprocess
import time
from bpy.types import Operator
from . import path_func
### Open Last file
def open_last_recent_file():
'''opent the last file (last)'''
history = Path(bpy.utils.user_resource('CONFIG')) / 'recent-files.txt'
if history.exists():
try:
# with open(history, 'r') as f:
with history.open('r') as fd:
last_file = fd.readline().strip() # read first line of history txt file
if last_file:
bpy.ops.wm.open_mainfile(filepath=last_file, load_ui=True, use_scripts=True)
else:
return (1, 'error with recent-file.txt, may be empty : ' + history)
except:
return (1, 'error accessing recent-file.txt : ' + history)
else:
return (1, 'error, "recent-file.txt" not found')
return (0, '')
class PATH_OT_open_last_file(Operator):
bl_idname = "path.open_last_file"
bl_label = "Open Last File"
bl_description = "Open the last blend file"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
error, mess = open_last_recent_file()
if error:
self.report({'ERROR'}, mess)
else:
if mess:
self.report({'INFO'}, mess)
return {"FINISHED"}
### Search in open history
def get_history_list(self, context):
'''return (identifier, name, description) of enum content'''
# blends = []
# with open(self.history, 'r') as fd:
# blends = [line.strip() for line in fd]
# if not blends:
# return [("", "", "")]
# return [(i, basename(i), "") for i in blends]
try:
with open(self.history, 'r') as fd:
blends = [line.strip() for line in fd]
except:
return [("", "", "")]
return [(i, basename(i), "") for i in blends]
class PATH_OT_search_open_history(Operator) :
bl_idname = "path.open_from_history"
bl_label = 'Open Blend From History'
# important to have the updated enum here as bl_property
bl_property = "blend_files_enum"
# There is a known bug with using a callback,
# Python must keep a reference to the strings returned by the callback
# or Blender will misbehave or even crash.
history : bpy.props.StringProperty(default='', options={'SKIP_SAVE'}) # need to have a variable to store (to get it in self)
blend_files_enum : bpy.props.EnumProperty(
name="Blends",
description="Take the blend",
items=get_history_list,
options={'HIDDEN'},
)
def invoke(self, context, event):
history = Path(bpy.utils.user_resource('CONFIG')) / 'recent-files.txt'
if not history.exists():
self.report({'WARNING'}, 'No history file found')
return {'CANCELLED'}
self.history = str(history)
blends = []
try:
with history.open('r') as fd:
blends = [fd.readline().strip() for line in fd]
except:
self.report({'ERROR'}, f'Error accessing recent-file.txt : {self.history}')
return {'CANCELLED'}
if not blends:
self.report({'WARNING'}, f'Empty history !')
return {'CANCELLED'}
wm = context.window_manager
wm.invoke_search_popup(self)
return {'FINISHED'}
def execute(self, context):
blend = self.blend_files_enum
bpy.ops.wm.open_mainfile(filepath=blend)
return {'FINISHED'}
class PATH_OT_open_in_new_instance(Operator) :
bl_idname = "wm.open_in_new_instance"
bl_label = 'Open Blend In New Instance'
bl_description = "Open blend file in a new instance of blender"
bl_options = {"REGISTER", "INTERNAL"}
filepath : bpy.props.StringProperty(default='', options={'SKIP_SAVE'})
def execute(self, context):
path_func.open_blend_new_instance(self.filepath)
return {'FINISHED'}
class PATH_OT_full_reopen(Operator) :
bl_idname = "wm.full_reopen"
bl_label = 'Full Reopen Blend'
bl_description = "Close and re-open blender file\nCtrl: Force reopen even if file is not saved"
bl_options = {"REGISTER", "INTERNAL"}
@classmethod
def poll(cls, context):
return True
return bpy.data.is_saved #only if file is saved?
def invoke(self, context, event):
if bpy.data.is_dirty and not event.ctrl:
wm = context.window_manager
return wm.invoke_props_dialog(self)
return self.execute(context)
def draw(self, context):
layout = self.layout
col = layout.column()
col.label(text='File is not saved! Reopen anyway ?')
col.label(text='(use Ctrl to bypass this prompt even if not saved)')
def execute(self, context):
path_func.open_blend_new_instance()
# Delay to avoid quitting before subprocess is launched
time.sleep(0.1) # lower value ?
bpy.ops.wm.quit_blender()
return {'FINISHED'}
class PATH_OT_open_side_blend(Operator) :
bl_idname = "wm.open_side_blend"
bl_label = 'Open Side Blend'
bl_description = "Open blend in same folder as current"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
return bpy.data.is_saved
def invoke(self, context, event):
## list surrounding blends
self.blend_list = [Path(i.path) for i in os.scandir(Path(bpy.data.filepath).parent) if i.is_file() and i.name.endswith('.blend')]
## Best to list the file even if solo (that way can open another instance quickly)
# if len(self.blend_list) < 2:
# self.report({"WARNING"}, 'No other blend file in current blend location !')
# return {'CANCELLED'}
natural_sort = lambda x: [int(t) if t.isdigit() else t.lower() for t in re.split('(\d+)', x.name)]
try:
self.blend_list.sort(key=natural_sort)
except Exception:
self.blend_list.sort(key=lambda x: x.name)
wm = context.window_manager
## using a 'props_popup' crashes blender (using dialog add an OK button...but works)
# return wm.invoke_props_popup(self, event) # need "undo" in bl_options else get an error
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
col = layout.column()
for path in self.blend_list:
if path == Path(bpy.data.filepath):
mainrow=col.row()
subrow = mainrow.row()
subrow.alignment = 'LEFT'
subrow.enabled=False
subrow.operator('wm.open_mainfile', text=path.name, emboss=False).filepath = str(path) # path.as_posix()
else:
mainrow=col.row()
subrow = mainrow.row()
subrow.alignment = 'LEFT'
subrow.operator_context = "EXEC_AREA"# , (should be "INVOKE_AREA" if file was not saved)
op = subrow.operator('wm.open_mainfile', text=path.name, emboss=False)
op.load_ui = context.preferences.filepaths.use_load_ui
op.filepath = str(path)
# subrow.operator('wm.open_mainfile', text=path.name, emboss=False).filepath = str(path)
row = mainrow.row()
row.alignment = 'RIGHT'
row.operator('wm.open_in_new_instance', text='', icon='FILE_BACKUP').filepath = str(path)
def execute(self, context):
return {'FINISHED'}
class PATH_OT_open_browser(Operator):
bl_idname = "path.open_browser"
bl_label = "Open Path In Browser"
bl_description = "Open passed file/folder in OS browser. If a file, the file will be selected"
bl_options = {"REGISTER", "INTERNAL"}
filepath : bpy.props.StringProperty(options={'SKIP_SAVE'})
def execute(self, context):
if not self.filepath:
self.report({'ERROR'}, 'No filepath')
return {"CANCELLED"}
if not exists(self.filepath):
self.report({'ERROR'}, f'Not found : {self.filepath}')
return {"CANCELLED"}
path_func.openFolder(self.filepath)
self.report({'INFO'}, f'Open: {self.filepath}')
return {"FINISHED"}
classes = (
PATH_OT_open_last_file,
PATH_OT_search_open_history,
PATH_OT_open_in_new_instance,
PATH_OT_full_reopen,
PATH_OT_open_side_blend,
PATH_OT_open_browser,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)