-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_vertex_groups.py
51 lines (46 loc) · 2.06 KB
/
copy_vertex_groups.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
import bpy
active = bpy.context.active_object
for ob in bpy.context.selected_objects:
me_source = active.data
me_target = ob.data
# sanity check: do source and target have the same amount of verts?
if len(me_source.vertices) != len(me_target.vertices):
print('ERROR: objects {} and {} have different vertex counts.'.format(active.name,ob.name))
continue
vgroups_IndexName = {}
for i in range(0, len(active.vertex_groups)):
groups = active.vertex_groups[i]
vgroups_IndexName[groups.index] = groups.name
data = {} # vert_indices, [(vgroup_index, weights)]
for v in me_source.vertices:
vg = v.groups
vi = v.index
if len(vg) > 0:
vgroup_collect = []
for i in range(0, len(vg)):
vgroup_collect.append((vg[i].group, vg[i].weight))
data[vi] = vgroup_collect
# write data to target
if ob != active:
# add missing vertex groups
for vgroup_idx, vgroup_name in vgroups_IndexName.items():
#check if group already exists...
already_present = 0
for i in range(0, len(ob.vertex_groups)):
if ob.vertex_groups[i].name == vgroup_name:
vgroup_name = vgroup_name+'_from_'+active.name
vgroups_IndexName[vgroup_idx] = vgroup_name
# ... if not, then add
if already_present == 0:
ob.vertex_groups.new(name=vgroup_name)
# write weights
for v in me_target.vertices:
for vi_source, vgroupIndex_weight in data.items():
if v.index == vi_source:
for i in range(0, len(vgroupIndex_weight)):
groupName = vgroups_IndexName[vgroupIndex_weight[i][0]]
groups = ob.vertex_groups
for vgs in range(0, len(groups)):
if groups[vgs].name == groupName:
groups[vgs].add((v.index,),
vgroupIndex_weight[i][1], "REPLACE")