forked from b-init/ImagePaste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preferences.py
318 lines (270 loc) · 10 KB
/
preferences.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
import bpy
from .helper import ADDON_NAME
from .operators import (
IMAGEPASTE_OT_imageeditor_copy,
IMAGEPASTE_OT_imageeditor_paste,
IMAGEPASTE_OT_sequenceeditor_paste,
IMAGEPASTE_OT_shadereditor_paste,
IMAGEPASTE_OT_view3d_paste_plane,
IMAGEPASTE_OT_view3d_paste_reference,
)
class IMAGEPASTE_AddonPreferences(bpy.types.AddonPreferences):
"""Add-on preferences for ImagePaste"""
bl_idname = ADDON_NAME
is_use_another_directory: bpy.props.BoolProperty(
name="Use a directory",
description=(
"Save images to another directory instead of temporary directory"
" when the file is not saved"
),
default=False,
)
another_directory: bpy.props.StringProperty(
name="Saving directory",
description="A path to directory where images saved to",
subtype="DIR_PATH",
)
is_force_use_another_directory: bpy.props.BoolProperty(
name="Force use another directory",
description="Save images to above directory even when the file is saved or not",
default=False,
)
is_use_subdirectory: bpy.props.BoolProperty(
name="Use subdirectory",
description="Save images to a subdirectory where the file is saved",
default=True,
)
subdirectory_name: bpy.props.StringProperty(
name="Sub directory name",
description="A name for subdirectory",
default="ImagePaste",
)
image_filename_pattern: bpy.props.StringProperty(
name="Image file name",
description=(
"A name for pasted images\n"
"%y: Year, %m: Month, %d: Day\n"
"%H: Hour, %M: Minute, %S: Second"
),
default="ImagePaste-%y%m%d-%H%M%S",
)
image_type_to_move: bpy.props.EnumProperty(
name="Image type to move",
description="Which type of image will be moved",
items=[
("pasted_images", "Pasted images", "Only pasted images will be moved"),
("all_images", "All images", "All images will be moved"),
("no_moving", "No moving", "Don't move anything when saving"),
],
default="pasted_images",
)
is_disable_debug: bpy.props.BoolProperty(
name="Disable debug message",
description="Debug message will not printed in console",
default=False,
)
def draw(self, _context):
from .helper import is_valid_filename
split_ratio = 0.3
layout = self.layout
# New box
box = layout.box().column()
box.label(
text="Specify a different directory for images when the file is not saved"
)
# New property
prop = box.row(align=True)
split = prop.split(factor=split_ratio)
# First column
column_1 = split.column()
column_1.alignment = "RIGHT"
column_1.label(text="Use a directory")
# Second column
column_2 = split.column().row(align=True)
column_2.prop(self, "is_use_another_directory", text="")
column_2_sub = column_2.column()
column_2_sub.active = self.is_use_another_directory
column_2_sub.prop(self, "another_directory", text="")
# New property
prop = box.row(align=True)
split = prop.split(factor=split_ratio)
# First column
split.column()
# Second column
column_2 = split.column().row(align=True)
column_2.active = self.is_use_another_directory
column_2.prop(self, "is_force_use_another_directory", text="Force use")
# New box
box = layout.box().column()
box.label(text="Specify a subdirectory for images when the file is saved")
# New property
prop = box.row(align=True)
prop.active = not (
self.is_force_use_another_directory and self.is_use_another_directory
)
split = prop.split(factor=split_ratio)
# First column
column_1 = split.column()
column_1.alignment = "RIGHT"
column_1.label(text="Use subdirectory")
# Second column
column_2 = split.column().row(align=True)
column_2.prop(self, "is_use_subdirectory", text="")
column_2_sub = column_2.column()
column_2_sub.active = self.is_use_subdirectory
column_2_sub.prop(self, "subdirectory_name", text="")
# New box
box = layout.box().column()
box.label(text="Custom image file name")
# New property
prop = box.row(align=True)
split = prop.split(factor=split_ratio)
# First column
column_1 = split.column()
column_1.alignment = "RIGHT"
column_1.label(text="Image file name")
# Second column
column_2 = split.column().row(align=True)
column_2.prop(self, "image_filename_pattern", text="")
column_2.alert = is_valid_filename(self.image_filename_pattern)
# New box
box = layout.box().column()
box.label(
text=("Choose the type of images that will be moved when saving the file")
)
# New property
prop = box.row(align=True)
split = prop.split(factor=split_ratio)
# First column
column_1 = split.column()
column_1.alignment = "RIGHT"
column_1.label(text="Image type to move")
# Second column
column_2 = split.row()
column_2.prop(self, "image_type_to_move", expand=True)
# New box
box = layout.box().column()
box.label(text="Miscellaneous")
# New property
prop = box.row(align=True)
split = prop.split(factor=split_ratio)
# First column
column_1 = split.column()
column_1.alignment = "RIGHT"
column_1.label(text="Disable debug message")
# Second column
column_2 = split.row()
column_2.prop(self, "is_disable_debug", text="")
def imageeditor_copy_imagemenu_draw(self, _context):
self.layout.operator(
IMAGEPASTE_OT_imageeditor_copy.bl_idname,
icon="COPYDOWN",
)
def imageeditor_paste_imagemenu_draw(self, _context):
self.layout.operator(
IMAGEPASTE_OT_imageeditor_paste.bl_idname,
icon="FILE_IMAGE",
)
def sequenceeditor_paste_contextmenu_draw(self, _context):
self.layout.separator()
self.layout.operator(
IMAGEPASTE_OT_sequenceeditor_paste.bl_idname,
icon="IMAGE_PLANE",
)
def shadereditor_paste_contextmenu_draw(self, _context):
self.layout.operator(
IMAGEPASTE_OT_shadereditor_paste.bl_idname,
icon="FILE_IMAGE",
)
def view3d_paste_plane_imageaddmenu_draw(self, _context):
self.layout.operator(
IMAGEPASTE_OT_view3d_paste_plane.bl_idname,
icon="IMAGE_PLANE",
)
def view3d_paste_reference_imageaddmenu_draw(self, _context):
self.layout.operator(
IMAGEPASTE_OT_view3d_paste_reference.bl_idname,
icon="FILE_IMAGE",
)
@bpy.app.handlers.persistent
def move_to_saved_directory_handler(self, _context):
bpy.ops.imagepaste.move_to_saved_directory("INVOKE_DEFAULT")
addon_keymaps = []
def register():
bpy.utils.register_class(IMAGEPASTE_AddonPreferences)
bpy.types.IMAGE_MT_image.append(imageeditor_copy_imagemenu_draw)
bpy.types.IMAGE_MT_image.append(imageeditor_paste_imagemenu_draw)
bpy.types.SEQUENCER_MT_context_menu.append(sequenceeditor_paste_contextmenu_draw)
bpy.types.NODE_MT_context_menu.append(shadereditor_paste_contextmenu_draw)
bpy.types.VIEW3D_MT_image_add.append(view3d_paste_plane_imageaddmenu_draw)
bpy.types.VIEW3D_MT_image_add.append(view3d_paste_reference_imageaddmenu_draw)
bpy.app.handlers.save_post.append(move_to_saved_directory_handler)
kc = bpy.context.window_manager.keyconfigs.addon
km = kc.keymaps.new(name="Image Generic", space_type="IMAGE_EDITOR")
kmi = km.keymap_items.new(
IMAGEPASTE_OT_imageeditor_copy.bl_idname,
type="C",
value="PRESS",
ctrl=True,
shift=True,
)
addon_keymaps.append((km, kmi))
km = kc.keymaps.new(name="Image Generic", space_type="IMAGE_EDITOR")
kmi = km.keymap_items.new(
IMAGEPASTE_OT_imageeditor_paste.bl_idname,
type="V",
value="PRESS",
ctrl=True,
shift=True,
)
addon_keymaps.append((km, kmi))
km = kc.keymaps.new(name="Sequencer", space_type="SEQUENCE_EDITOR")
kmi = km.keymap_items.new(
IMAGEPASTE_OT_sequenceeditor_paste.bl_idname,
type="V",
value="PRESS",
ctrl=True,
shift=True,
alt=True,
)
addon_keymaps.append((km, kmi))
km = kc.keymaps.new(name="Node Editor", space_type="NODE_EDITOR")
kmi = km.keymap_items.new(
IMAGEPASTE_OT_shadereditor_paste.bl_idname,
type="V",
value="PRESS",
ctrl=True,
shift=True,
)
addon_keymaps.append((km, kmi))
km = kc.keymaps.new(name="3D View", space_type="VIEW_3D")
kmi = km.keymap_items.new(
IMAGEPASTE_OT_view3d_paste_plane.bl_idname,
type="V",
value="PRESS",
ctrl=True,
shift=True,
alt=True,
)
addon_keymaps.append((km, kmi))
km = kc.keymaps.new(name="3D View", space_type="VIEW_3D")
kmi = km.keymap_items.new(
IMAGEPASTE_OT_view3d_paste_reference.bl_idname,
type="V",
value="PRESS",
ctrl=True,
shift=True,
)
addon_keymaps.append((km, kmi))
def unregister():
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
bpy.app.handlers.save_post.remove(move_to_saved_directory_handler)
bpy.types.VIEW3D_MT_image_add.remove(view3d_paste_reference_imageaddmenu_draw)
bpy.types.VIEW3D_MT_image_add.remove(view3d_paste_plane_imageaddmenu_draw)
bpy.types.NODE_MT_context_menu.remove(shadereditor_paste_contextmenu_draw)
bpy.types.SEQUENCER_MT_context_menu.remove(sequenceeditor_paste_contextmenu_draw)
bpy.types.IMAGE_MT_image.remove(imageeditor_paste_imagemenu_draw)
bpy.types.IMAGE_MT_image.remove(imageeditor_copy_imagemenu_draw)
bpy.utils.unregister_class(IMAGEPASTE_AddonPreferences)