forked from bookyakuno/Blender-Scramble-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDATA_PT_uv_texture.py
216 lines (189 loc) · 6.67 KB
/
DATA_PT_uv_texture.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
# 「プロパティ」エリア > 「オブジェクトデータ」タブ > 「UVマップ」パネル
# "Propaties" Area > "Object Data" Tab > "UV Maps" Panel
import bpy
from bpy.props import *
################
# オペレーター #
################
class RenameSpecificNameUV(bpy.types.Operator):
bl_idname = "object.rename_specific_name_uv"
bl_label = "Rename specific UVs Together"
bl_description = "Rename the selected objects' UV Maps with specific name to the designated one"
bl_options = {'REGISTER', 'UNDO'}
source_name : StringProperty(name="Target", default="")
replace_name : StringProperty(name="New Name", default="New UV")
@classmethod
def poll(cls, context):
if (len(context.selected_objects) == 0):
return False
return True
def execute(self, context):
for obj in context.selected_objects:
if (obj.type != 'MESH'):
continue
me = obj.data
for uv in me.uv_layers[:]:
if (uv.name == self.source_name):
uv.name = self.replace_name
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
class DeleteSpecificNameUV(bpy.types.Operator):
bl_idname = "object.delete_specific_name_uv"
bl_label = "Delete specific UVs together"
bl_description = "Remove the selected objects' UV Maps with specific name"
bl_options = {'REGISTER', 'UNDO'}
name : StringProperty(name="Name", default="UV")
@classmethod
def poll(cls, context):
if (len(context.selected_objects) == 0):
return False
return True
def execute(self, context):
for obj in context.selected_objects:
if (obj.type != 'MESH'):
continue
me = obj.data
for uv in me.uv_layers:
if (uv.name == self.name):
me.uv_layers.remove(uv)
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
class RemoveUnselectedUV(bpy.types.Operator):
bl_idname = "object.remove_unselected_uv"
bl_label = "Remove Unselected UV"
bl_description = "Remove Unselected UV Maps of the active object"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = context.active_object
if (not obj):
return False
if (obj.type != 'MESH'):
return False
me = obj.data
if (len(me.uv_layers) == 0):
return False
return True
def execute(self, context):
me = context.active_object.data
#uv_layersにおいて、要素が削除されるとactiveが更新され頂点グループなどがuv_layersに追加されるバグ?があるので、name要素を指定してuv_layersへの参照を切る
pre_uv_name = me.uv_layers.active.name
uv_names = [a.name for a in me.uv_layers]
for uv in uv_names:
if uv != pre_uv_name:
me.uv_layers.remove(me.uv_layers[uv])
me.uv_layers.active = me.uv_layers[pre_uv_name]
return {'FINISHED'}
class MoveActiveUV(bpy.types.Operator):
bl_idname = "object.move_active_uv"
bl_label = "Move UV"
bl_description = "Move the active UV up or down"
bl_options = {'REGISTER', 'UNDO'}
items = [
('UP', "To Up", "", 1),
('DOWN', "To Down", "", 2),
]
mode : EnumProperty(items=items, name="Direction", default="UP")
@classmethod
def poll(cls, context):
obj = context.active_object
if (not obj):
return False
if (obj.type != 'MESH'):
return False
me = obj.data
if (len(me.uv_layers) <= 1):
return False
return True
def execute(self, context):
obj = context.active_object
me = obj.data
if (self.mode == 'UP'):
if (me.uv_layers.active_index <= 0):
return {'CANCELLED'}
target_index = me.uv_layers.active_index - 1
elif (self.mode == 'DOWN'):
target_index = me.uv_layers.active_index + 1
if (len(me.uv_layers) <= target_index):
return {'CANCELLED'}
pre_mode = obj.mode
bpy.ops.object.mode_set(mode='OBJECT')
uv_layer = me.uv_layers.active
target_uv_layer = me.uv_layers[target_index]
uv_tex = me.uv_layers.active
target_uv_tex = me.uv_layers[target_index]
for data_name in dir(uv_tex):
if (data_name[0] != '_' and data_name != 'bl_rna' and data_name != 'rna_type' and data_name != 'data'):
temp = uv_tex.__getattribute__(data_name)
target_temp = target_uv_tex.__getattribute__(data_name)
target_uv_tex.__setattr__(data_name, temp)
uv_tex.__setattr__(data_name, target_temp)
target_uv_tex.__setattr__(data_name, temp)
uv_tex.__setattr__(data_name, target_temp)
for i in range(len(uv_layer.data)):
for data_name in dir(uv_layer.data[i]):
if (data_name[0] != '_' and data_name != 'bl_rna' and data_name != 'rna_type'):
try:
temp = target_uv_layer.data[i].__getattribute__(data_name)[:]
except TypeError:
temp = target_uv_layer.data[i].__getattribute__(data_name)
target_uv_layer.data[i].__setattr__(data_name, uv_layer.data[i].__getattribute__(data_name))
uv_layer.data[i].__setattr__(data_name, temp)
for i in range(len(uv_tex.data)):
temp = uv_tex.data[i].uv
uv_tex.data[i].uv = target_uv_tex.data[i].uv
target_uv_tex.data[i].uv = temp
me.uv_layers.active_index = target_index
bpy.ops.object.mode_set(mode=pre_mode)
return {'FINISHED'}
################
# サブメニュー #
################
class UVMenu(bpy.types.Menu):
bl_idname = "VIEW3D_MT_object_specials_uv"
bl_label = "Bulk Manipulation"
bl_description = "Manipulate selected objects' UV Maps together"
def draw(self, context):
self.layout.operator(RenameSpecificNameUV.bl_idname, icon="PLUGIN")
self.layout.operator(DeleteSpecificNameUV.bl_idname, icon="PLUGIN")
################
# クラスの登録 #
################
classes = [
RenameSpecificNameUV,
DeleteSpecificNameUV,
RemoveUnselectedUV,
MoveActiveUV,
UVMenu
]
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])):
if (context.active_object.type == 'MESH'):
if (context.active_object.data.uv_layers.active):
row = self.layout.row()
sub = row.row(align=True)
sub.operator(MoveActiveUV.bl_idname, icon='TRIA_UP', text="").mode = 'UP'
sub.operator(MoveActiveUV.bl_idname, icon='TRIA_DOWN', text="").mode = 'DOWN'
row.operator(RemoveUnselectedUV.bl_idname, icon="PLUGIN")
row.menu(UVMenu.bl_idname, icon="PLUGIN")
if (context.preferences.addons[__name__.partition('.')[0]].preferences.use_disabled_menu):
self.layout.operator('wm.toggle_menu_enable', icon='CANCEL').id = __name__.split('.')[-1]