-
Notifications
You must be signed in to change notification settings - Fork 0
/
vg_tools.py
307 lines (235 loc) · 10.1 KB
/
vg_tools.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
import bpy
from bpy.props import *
from .common import *
THRES = 0.0
def any_vert_in_vgroup(group, mesh):
for vert in mesh.vertices:
for vg in vert.groups:
if vg.group == group.index:
if vg.weight != 0.0:
return True
return False
def any_mirror_in_modifiers(obj):
for modf in obj.modifiers:
if modf.type == 'MIRROR':
return True
return False
def remove_unused_vgs(objs):
for o in objs:
if o.type != 'MESH': continue
mesh = o.data
groups = o.vertex_groups
mirrored = any_mirror_in_modifiers(o)
# Remove empty groups
if not mirrored:
for group in groups:
if not any_vert_in_vgroup(group, mesh):
groups.remove(group)
else:
for group in groups:
if not any_vert_in_vgroup(group, mesh):
#print(group.name)
#if there's 'L' or 'R' in group name, check if mirrored_group have vertex
if '.L' in group.name or '.R' in group.name:
if '.L' in group.name:
mirrored_group_name = group.name.replace('.L','.R')
else:
mirrored_group_name = group.name.replace('.R','.L')
mirrored_group_index = groups.find(mirrored_group_name)
if mirrored_group_index != -1:
mirrored_group = groups[mirrored_group_index]
#if there are no vertex in mirrored group, remove the group
if not any_vert_in_vgroup(mirrored_group,mesh):
groups.remove(group)
else:
groups.remove(group)
else:
groups.remove(group)
# Remove zero verts
for v in o.data.vertices:
for g in v.groups:
if g.weight <= THRES:
o.vertex_groups[g.group].remove([v.index])
class YRemoveunusedVG(bpy.types.Operator):
bl_idname = "mesh.y_remove_unused_vertex_groups"
bl_label = "Remove Unused Vertex Groups"
bl_description = "Remove Unused Vertex Groups"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
obj = context.object
ori_mode = ''
if obj.mode != 'OBJECT':
ori_mode = obj.mode
bpy.ops.object.mode_set()
remove_unused_vgs(context.selected_objects)
if ori_mode != '':
bpy.ops.object.mode_set(mode=ori_mode)
return {'FINISHED'}
class YMergeVGDown(bpy.types.Operator):
bl_idname = "mesh.y_merge_vg_down"
bl_label = "Merge Vertex Group Down"
bl_description = "Merge active vertex group down"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = context.object
if not obj or obj.type != 'MESH': return False
vgs = obj.vertex_groups
active_vg = vgs.active
return active_vg and vgs[-1] != active_vg
def invoke(self, context, event):
obj = context.object
self.vg = obj.vertex_groups.active
self.vg_index = -1
# Get vg index
for i, v in enumerate(obj.vertex_groups):
if v == self.vg:
self.vg_index = i
break
self.vg1_index = self.vg_index+1
self.vg1 = obj.vertex_groups[self.vg1_index]
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
self.layout.label(text="Are you sure you want to merge vertex group")
self.layout.label(text="'" + self.vg1.name + "' to '" + self.vg.name + "'?")
def execute(self, context):
obj = context.object
# Merge vg
for v in obj.data.vertices:
for vg in v.groups:
if vg.group == self.vg1_index:
obj.vertex_groups[self.vg_index].add([v.index],vg.weight,'ADD')
# Remove merged vg
obj.vertex_groups.remove(self.vg1)
return {'FINISHED'}
class YTransferWeightsAndSetup(bpy.types.Operator):
bl_idname = "mesh.y_transfer_weights_and_setup"
bl_label = "Transfer Weights and Armature Setup"
bl_description = "Transfer Weights and Armature Setup from last to other selected objects"
bl_options = {'REGISTER', 'UNDO'}
put_new_mod_above_subsurf : BoolProperty(
name = "Above Subsurf",
description = "Put New Modifier above Subdivision Surface",
default = True)
do_normalize : BoolProperty(
name = 'Use Normalize',
description = "Do normalize all after transfer",
default = True
)
@classmethod
def poll(cls, context):
obj = context.object
objs = [o for o in context.selected_objects if o.type == 'MESH']
return obj and obj.type == 'MESH' and len(objs) > 1
def invoke(self, context, event):
self.num_of_flags = 32
return context.window_manager.invoke_props_dialog(self)
def check(self, context):
return True
def draw(self, context):
self.layout.prop(self, 'do_normalize')
self.layout.prop(self, 'put_new_mod_above_subsurf')
def execute(self, context):
obj = ori_obj = context.object
ori_objs = [o for o in context.selected_objects]
objs = [o for o in context.selected_objects if o != obj and o.type == 'MESH']
ori_mode = obj.mode
# Check armature setup
rig = None
mod = [m for m in obj.modifiers if m.type == 'ARMATURE' and m.object and m.show_viewport]
if mod:
mod = mod[0]
rig = mod.object
# Deselect all first
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
obj.select_set(True)
for o in objs:
# Select object
context.view_layer.objects.active = o
o.select_set(True)
# Go to vertex paint mode
bpy.ops.object.mode_set(mode='WEIGHT_PAINT')
try:
bpy.ops.object.data_transfer(use_reverse_transfer=True, data_type='VGROUP_WEIGHTS', vert_mapping='POLYINTERP_NEAREST', layers_select_src='NAME', layers_select_dst='ALL')
except Exception as e:
print(e)
# Set armature
if rig:
# Check if armature already been set
mod_founds = [m for m in o.modifiers if m.type == 'ARMATURE' and m.object == rig and m.show_render and m.show_viewport]
if not mod_founds:
new_mod = o.modifiers.new('Armature', 'ARMATURE')
#new_mod.object = rig
for prop in dir(mod):
try: setattr(new_mod, prop, getattr(mod, prop))
except: pass
# Put new modifier above subsurf
if self.put_new_mod_above_subsurf:
subsurf_ids = [i for i, m in enumerate(o.modifiers) if m.type == 'SUBSURF']
if subsurf_ids:
cur_id = len(o.modifiers)-1
step = cur_id - subsurf_ids[0]
for i in range(step):
bpy.ops.object.modifier_move_up(modifier=new_mod.name)
# Check if object is parented to bone
if o.parent == rig and o.parent_type == 'BONE':
o.parent_type = 'OBJECT'
if self.do_normalize:
if is_greater_than_400():
bpy.ops.object.vertex_group_normalize_all(group_select_mode='ALL')
else: bpy.ops.object.vertex_group_normalize_all(group_select_mode='BONE_DEFORM')
# Deselect object
bpy.ops.object.mode_set(mode='OBJECT')
o.select_set(False)
# Remove unused vertex groups
remove_unused_vgs(objs)
# Recover selections
for o in ori_objs: o.select_set(True)
context.view_layer.objects.active = ori_obj
bpy.ops.object.mode_set(mode=ori_mode)
return {'FINISHED'}
class YWeightPaintEnableSelectBones(bpy.types.Operator):
bl_idname = "mesh.y_weight_paint_enable_select_bones"
bl_label = "Weight Paint with Bones Selection Mode"
bl_description = "Enable select bones mode on weight paint"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'MESH'
def execute(self, context):
obj = context.object
rig = None
# Search for the rig
for mod in obj.modifiers:
if mod.type == 'ARMATURE':
if mod.object:
rig = mod.object
break
if not rig:
self.report({'ERROR'}, "No armature object found!")
return {'CANCELLED'}
# Go to weight paint mode
if obj.mode != 'WEIGHT_PAINT':
bpy.ops.object.mode_set(mode='WEIGHT_PAINT')
# Select rig object
context.view_layer.objects.active = rig
rig.select_set(True)
# Go to pose mode
bpy.ops.object.mode_set(mode='POSE')
# Back to original object
context.view_layer.objects.active = obj
return {'FINISHED'}
def register():
bpy.utils.register_class(YRemoveunusedVG)
bpy.utils.register_class(YMergeVGDown)
bpy.utils.register_class(YTransferWeightsAndSetup)
bpy.utils.register_class(YWeightPaintEnableSelectBones)
def unregister():
bpy.utils.unregister_class(YRemoveunusedVG)
bpy.utils.unregister_class(YMergeVGDown)
bpy.utils.unregister_class(YTransferWeightsAndSetup)
bpy.utils.unregister_class(YWeightPaintEnableSelectBones)