Last active
October 24, 2023 14:50
-
-
Save p2or/b1d2eee91c64790ee6ff4e82dbd193c0 to your computer and use it in GitHub Desktop.
Merge Vertex Groups #Blender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
group_input = {"Group", "Group.001", "Group.002"} | |
ob = bpy.context.active_object | |
group_lookup = {g.index: g.name for g in ob.vertex_groups} | |
group_candidates = {n for n in group_lookup.values() if n in group_input} | |
# test whether all candidates components of group_lookup | |
if all(n in group_lookup.values() for n in group_candidates): | |
pass | |
# general tests | |
if (len(group_candidates) and ob.type == 'MESH' and | |
bpy.context.mode == 'OBJECT'): | |
# iterate through the vertices and sum the weights per group | |
vertex_weights = {} | |
for vert in ob.data.vertices: | |
if len(vert.groups): | |
for item in vert.groups: | |
vg = ob.vertex_groups[item.group] | |
if vg.name in group_candidates: | |
if vert.index in vertex_weights: | |
vertex_weights[vert.index] += vg.weight(vert.index) | |
else: | |
vertex_weights[vert.index] = vg.weight(vert.index) | |
# clamp/slice values above 1.0 | |
for key in vertex_weights.keys(): | |
if (vertex_weights[key] > 1.0): vertex_weights[key] = 1.0 | |
# create new vertex group | |
vgroup = ob.vertex_groups.new(name="+".join(group_candidates)) | |
# add the values to the group | |
for key, value in vertex_weights.items(): | |
vgroup.add([key], value ,'REPLACE') #'ADD','SUBTRACT' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment