-
Notifications
You must be signed in to change notification settings - Fork 1
/
ops.py
365 lines (280 loc) · 10.1 KB
/
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#
# Copyright (c) 2018 Alexander "CheeryLee" Pluzhnikov
#
# This file is part of blender_native_file_dialog.
#
# blender_native_file_dialog 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.
#
# blender_native_file_dialog 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 blender_native_file_dialog. If not, see <http://www.gnu.org/licenses/>.
#
import subprocess
import copy
import bpy
from bpy.props import BoolProperty
# TODO: It is Mac path. Change it to cross-platform
# DOUBLE POSTING
def get_working_dir():
import sys
path = sys.executable
i = 0
while i < 3:
path = path.rpartition('/')[0]
i += 1
return path
command = get_working_dir() + "/Contents/Resources/2.79/python/bin/python3.5m "
command = command + get_working_dir() + "/Contents/Resources/2.79/scripts/addons/native_file_dialog/file_dialog.py"
###############
## Save file ##
###############
class SaveFile(bpy.types.Operator):
bl_idname = "native_wm.save_mainfile"
bl_label = "NATIVE: Save Blender File"
def __init__(self):
print("Native file dialog is working now")
def execute(self, context):
print("Dialog is opened")
# Exclude repeating when you save file once more
if bpy.data.filepath == '':
fileName = run_save_dialog(filetype = "save_mainfile", initialDir = None)
try:
if bpy.data.filepath == '':
bpy.ops.wm.save_mainfile(filepath = fileName)
print("INFO: File " + fileName + " saved")
else:
bpy.ops.wm.save_mainfile(filepath = bpy.data.filepath)
print("INFO: File " + bpy.data.filepath + " saved")
except RuntimeError:
print("ERROR: Can't save file!")
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
##################
## Save file as ##
##################
class SaveFileAs(bpy.types.Operator):
bl_idname = "native_wm.save_as_mainfile"
bl_label = "NATIVE: Save As Blender File"
copy = BoolProperty()
def __init__(self):
print("Native file dialog is working now")
def execute(self, context):
print("Dialog is opened")
if bpy.data.filepath == '':
fileName = run_save_dialog(filetype = "save_mainfile", initialDir = None)
else:
_initialDir = bpy.data.filepath.rpartition('/')[0]
fileName = run_save_dialog(filetype = "save_mainfile", initialDir = _initialDir)
try:
bpy.ops.wm.save_mainfile(filepath = fileName)
print("INFO: File " + fileName + " saved")
except RuntimeError:
print("ERROR: Can't save file!")
return {'FINISHED'}
def invoke(self, context, event):
self.copy = False
return self.execute(context)
################
## Save image ##
################
class SaveImage(bpy.types.Operator):
bl_idname = "native_image.save"
bl_label = "NATIVE: Save Image"
def __init__(self):
print("Native file dialog is working now")
def execute(self, context):
print("Dialog is opened")
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
filePath = area.spaces[0].image.filepath
try:
bpy.ops.image.save()
print("INFO: File " + filePath + " saved")
except RuntimeError:
print("ERROR: Can't save file!")
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
###################
## Save image as ##
###################
class SaveImageAs(bpy.types.Operator):
bl_idname = "native_image.save_as"
bl_label = "NATIVE: Save As Image"
copy = BoolProperty()
def __init__(self):
print("Native file dialog is working now")
def execute(self, context):
print("Dialog is opened")
# Get opened textfile
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
if area.spaces[0].image is None:
return {'FINISHED'}
filePath = area.spaces[0].image.filepath
break
if filePath == '':
fileName = run_save_dialog(filetype = "image_save", initialDir = None)
else:
_initialDir = filePath.rpartition('/')[0]
fileName = run_save_dialog(filetype = "image_save", initialDir = _initialDir)
if fileName != '':
try:
bpy.ops.image.save_as(filepath = fileName)
print("INFO: Image file " + fileName + " saved")
except RuntimeError:
print("ERROR: Can't save text file!")
return {'FINISHED'}
def invoke(self, context, event):
self.copy = False
return self.execute(context)
###############
## Save text ##
###############
class SaveText(bpy.types.Operator):
bl_idname = "native_text.save"
bl_label = "NATIVE: Save Text"
def __init__(self):
print("Native file dialog is working now")
def execute(self, context):
print("Dialog is opened")
# Get opened textfile
for area in bpy.context.screen.areas:
if area.type == 'TEXT_EDITOR':
filePath = area.spaces[0].text.filepath
try:
bpy.ops.text.save_as(filepath = filePath)
print("INFO: Text file " + area.spaces[0].text.name + " saved")
except RuntimeError:
print("ERROR: Can't save text file!")
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
##################
## Save text as ##
##################
class SaveTextAs(bpy.types.Operator):
bl_idname = "native_text.save_as"
bl_label = "NATIVE: Save As Text"
def __init__(self):
print("Native file dialog is working now")
def execute(self, context):
print("Dialog is opened")
# Get opened textfile
for area in bpy.context.screen.areas:
if area.type == 'TEXT_EDITOR':
filePath = area.spaces[0].text.filepath
if filePath == '':
fileName = run_save_dialog(filetype = "text_save", initialDir = None)
else:
_initialDir = filePath.rpartition('/')[0]
fileName = run_save_dialog(filetype = "text_save", initialDir = _initialDir)
if fileName != '':
try:
bpy.ops.text.save_as(filepath = fileName)
print("INFO: Text file " + fileName + " saved")
except RuntimeError:
print("ERROR: Can't save text file!")
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
###############
## Open file ##
###############
class OpenFile(bpy.types.Operator):
bl_idname = "native_wm.open_mainfile"
bl_label = "NATIVE: Open Blender File"
def __init__(self):
print("Native file dialog is working now")
def execute(self, context):
print("Dialog is opened")
fileName = run_open_dialog(filetype = "open_mainfile")
try:
bpy.ops.wm.open_mainfile(filepath = fileName)
print("INFO: File " + fileName + " opened")
except RuntimeError:
print("ERROR: Can't open file!")
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
################
## Open image ##
################
class OpenImage(bpy.types.Operator):
bl_idname = "native_image.open"
bl_label = "NATIVE: Open Image"
def __init__(self):
print("Native file dialog is working now")
def execute(self, context):
print("Dialog is opened")
fileName = run_open_dialog(filetype = "image_open")
try:
bpy.ops.image.open(filepath = fileName)
print("INFO: File " + fileName + " opened")
except RuntimeError:
print("ERROR: Can't open file!")
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
###############
## Open text ##
###############
class OpenText(bpy.types.Operator):
bl_idname = "native_text.open"
bl_label = "NATIVE: Open Text Block"
def __init__(self):
print("Native file dialog is working now")
def execute(self, context):
print("Dialog is opened")
fileName = run_open_dialog(filetype = "text_open")
try:
bpy.ops.text.open(filepath = fileName)
print("INFO: File " + fileName + " opened")
except RuntimeError:
print("ERROR: Can't open file!")
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
##
## Dialog functions
##
def run_open_dialog(filetype):
fileName = subprocess.check_output(command + " " + filetype, shell = True)
fileName = fileName.decode('UTF-8')
fileName = fileName.strip()
return fileName
def run_save_dialog(filetype, initialDir = None):
if initialDir is None:
fileName = subprocess.check_output(command + " " + filetype, shell = True)
else:
fileName = subprocess.check_output(command + " " + filetype + " " + initialDir, shell = True)
fileName = fileName.decode('UTF-8')
fileName = fileName.strip()
return fileName
classes = (
SaveFile,
SaveFileAs,
SaveImage,
SaveImageAs,
SaveText,
SaveTextAs,
OpenFile,
OpenImage,
OpenText
)
def _register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def _unregister():
from bpy.utils import unregister_class
for cls in classes:
unregister_class(cls)