Im trying to move different bones to a different vertex group for each bone for use with a particle emitter in my animation. I have 20 bones that I need to select a vertex group for and relocate the bone. How do I loop this code for selecting the vertex group moving the cursor and then moving the bone to where the cursor is located? each bone has a different vertex group to relocate to.
import bpy
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
obj = bpy.data.objects["Gazelle"]
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
vgroups = bpy.context.object.vertex_groups
vgroups.active_index = vgroups["belly"].index
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.object.vertex_group_select()
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
override = bpy.context.copy()
override['area'] = area
override['region'] = area.regions[4]
bpy.ops.view3d.snap_cursor_to_selected(override)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
arm = bpy.data.objects["Lion"]
arm.select_set(True)
bpy.context.view_layer.objects.active = arm
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.armature.select_all(action="DESELECT")
bones_to_select = ["tooth_01"]
for bone in arm.data.edit_bones:
if bone.name in bones_to_select:
bone.select = True
bone.select_tail = True
bone.select_head = True
if bone.name == str(bones_to_select[0]):
arm.data.edit_bones.active = bone
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
override = bpy.context.copy()
override['area'] = area
override['region'] = area.regions[4]
bpy.ops.view3d.snap_selected_to_cursor(override)
I tried a script to grab world position but it didn't work for me as mathematically it wasn't determining the center of the vertex groups which was essential for the particle emitter to function.