forked from bookyakuno/Blender-Scramble-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTOPBAR_MT_file_external_data.py
182 lines (164 loc) · 5.53 KB
/
TOPBAR_MT_file_external_data.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
# 「トップバー」エリア > > 「ファイル」メニュー > 「外部データ」メニュー
# "TOPBAR" Area > "File" Menu > "External Data" Menu
import bpy
import os, shutil
################
# オペレーター #
################
class ResaveAllImage(bpy.types.Operator):
bl_idname = "image.resave_all_image"
bl_label = "Save All Image Files in 'textures' Folder"
bl_description = "Select the existed 'textures' folder or create it, and save all the referenced image files in it"
bl_options = {'REGISTER'}
@classmethod
def poll(cls, context):
if (context.blend_data.filepath == ""):
return False
for img in bpy.data.images:
if (img.filepath != ""):
return True
return False
def execute(self, context):
for img in context.blend_data.images:
if (img.filepath != ""):
try:
img.pack()
img.unpack()
except RuntimeError:
pass
self.report(type={"INFO"}, message="Saved image files into 'textures' folder")
return {'FINISHED'}
class IsolationTexturesUnusedFiles(bpy.types.Operator):
bl_idname = "image.isolation_textures_unused_files"
bl_label = "Isolate Unused Image Files in 'textures' Folder"
bl_description = "Crate 'backup' folder in the existed 'textures' folder, and move unused image files in the 'textures' folder into it"
bl_options = {'REGISTER'}
@classmethod
def poll(cls, context):
path = context.blend_data.filepath
if (context.blend_data.filepath == ""):
return False
dir = os.path.dirname(path)
if (not os.path.isdir( os.path.join(dir, "textures") )):
return False
for img in bpy.data.images:
if (img.filepath != ""):
return True
return False
def execute(self, context):
names = []
for img in context.blend_data.images:
if (img.filepath != ""):
names.append(bpy.path.basename(img.filepath))
tex_dir = os.path.join( os.path.dirname(context.blend_data.filepath), "textures")
backup_dir = os.path.join(tex_dir, "backup")
if (not os.path.isdir(backup_dir)):
os.mkdir(backup_dir)
for name in os.listdir(tex_dir):
path = os.path.join(tex_dir, name)
if (not os.path.isdir(path)):
if (name not in names):
src = path
dst = os.path.join(path, backup_dir, name)
shutil.move(src, dst)
self.report(type={'INFO'}, message=name+"Isolate")
return {'FINISHED'}
class OpenRecentFiles(bpy.types.Operator):
bl_idname = "wm.open_recent_files"
bl_label = "Display List of 'Open Recent' as Text"
bl_description = "Display the list of recently-opened files in Blender's text editor"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
path = os.path.join(bpy.utils.user_resource('CONFIG'), "recent-files.txt")
pre_texts = context.blend_data.texts[:]
bpy.ops.text.open(filepath=path)
for text in context.blend_data.texts[:]:
for pre in pre_texts:
if (text.name == pre.name):
break
else:
new_text = text
break
max_area = 0
target_area = None
for area in context.screen.areas:
if (area.type == 'TEXT_EDITOR'):
target_area = area
break
if (max_area < area.height * area.width):
max_area = area.height * area.width
target_area = area
target_area.type = 'TEXT_EDITOR'
for space in target_area.spaces:
if (space.type == 'TEXT_EDITOR'):
space.text = new_text
return {'FINISHED'}
class OpenBookmarkText(bpy.types.Operator):
bl_idname = "wm.open_bookmark_text"
bl_label = "Display List of 'Bookmarks' as Text"
bl_description = "Display the list of bookmarked files at file browser in Blender's text editor"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
path = os.path.join(bpy.utils.user_resource('CONFIG'), "bookmarks.txt")
pre_texts = context.blend_data.texts[:]
bpy.ops.text.open(filepath=path)
for text in context.blend_data.texts[:]:
for pre in pre_texts:
if (text.name == pre.name):
break
else:
new_text = text
break
max_area = 0
target_area = None
for area in context.screen.areas:
if (area.type == 'TEXT_EDITOR'):
target_area = area
break
if (max_area < area.height * area.width):
max_area = area.height * area.width
target_area = area
target_area.type = 'TEXT_EDITOR'
for space in target_area.spaces:
if (space.type == 'TEXT_EDITOR'):
space.text = new_text
return {'FINISHED'}
################
# クラスの登録 #
################
classes = [
ResaveAllImage,
IsolationTexturesUnusedFiles,
OpenRecentFiles,
OpenBookmarkText
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
################
# メニュー追加 #
################
# メニューのオン/オフの判定
def IsMenuEnable(self_id):
for id in bpy.context.preferences.addons[__name__.partition('.')[0]].preferences.disabled_menu.split(','):
if (id == self_id):
return False
else:
return True
# メニューを登録する関数
def menu(self, context):
if (IsMenuEnable(__name__.split('.')[-1])):
self.layout.separator()
self.layout.operator('image.reload_all_image', icon="PLUGIN")#IMAGE_MT_image.py で定義
self.layout.separator()
self.layout.operator(ResaveAllImage.bl_idname, icon="PLUGIN")
self.layout.operator(IsolationTexturesUnusedFiles.bl_idname, icon="PLUGIN")
self.layout.separator()
self.layout.operator(OpenRecentFiles.bl_idname, icon="PLUGIN")
self.layout.operator(OpenBookmarkText.bl_idname, icon="PLUGIN")
if (context.preferences.addons[__name__.partition('.')[0]].preferences.use_disabled_menu):
self.layout.separator()
self.layout.operator('wm.toggle_menu_enable', icon='CANCEL').id = __name__.split('.')[-1]