I wrote a script to automatically copy some vertex group weights wihin the same model. It is supposed to cycle through the vertices that are selected and find the closest one from the list of unselected vertices. It works well for the most part, however, some of the weights are changed on vertices that were not selected before.
I’m not very good at Python scripting, so the solution is probably simple…but I tried a few solutions I could think of (f.e. completely deslecting everything within the loop, which obviously made the thing very slow) and nothing works, so if anyone has an idea, I’d be grateful.
import bpy import math import bmesh def VectorDistance(VectorA,VectorB): x = VectorA - VectorB y = VectorA - VectorB z = VectorA - VectorB return (x**2 + y**2 + z**2)**0.5 def viewport_update(): bpy.context.scene.objects.active = bpy.context.scene.objects.active def main(): bpy.ops.object.mode_set(mode='EDIT', toggle=False) me = bpy.context.object.data bm = bmesh.from_edit_mesh(me) #save seleced and nonselected verts in lists selectedVerts = [v for v in bm.verts if v.select == True and v.hide != True] nonSelectedVerts = [a for a in bm.verts if a.select != True and a.hide != True] #Deselect everything for v in bm.verts: v.select_set(False) viewport_update() for v in selectedVerts: distance = 0.3 closestVert = v #find closest vertex from unselected for a in nonSelectedVerts: newDistance = VectorDistance(v.co,a.co) if newDistance < distance: distance = newDistance closestVert = a #Get the vertices in the right selection order and copy the weights (probably where the error is?) if(closestVert != v): bm.select_history.add(closestVert) v.select_set(True) closestVert.select_set(True) viewport_update() bpy.ops.object.vertex_weight_copy() v.select_set(False) closestVert.select_set(False) else: print("Warning, no weights were copied") bpy.ops.object.mode_set(mode='WEIGHT_PAINT', toggle=False) main()
Also, is there any pre-made function for viewport updates in bmesh? I found that workaround in a forum, but it strikes me as weird that there is no build in thing for that…