Setup bones on cloth sim so that you can transfer it to Unity via FBX export So, I would like to have this happen with a script for each vertice of an object:

I just couldn't to make it work :/ Except only some parts of it (scripts found from everywhere).
Building blocks:
Add an empty to each vertex:
    import bpy

vertices = [i.co for i in bpy.context.active_object.data.vertices if i.select]
vertices

for vert in vertices:
    print(vert)
    bpy.ops.object.empty_add(type='PLAIN_AXES', location=vert)

Add a bone to each vertex (thanks):
import bpy
from mathutils import Vector

def AddBonesAtVertices(length, use_normals):
    objects = bpy.context.view_layer.objects
    obj = objects.active
    if not obj or obj.type != 'MESH':
        return

    points = []
    normals = []
    data = []
    for v in obj.data.vertices:
        p = obj.matrix_world @ v.co
        target = v.normal @ obj.matrix_world
        dir = target - p
        dir.normalize()
        dir = dir * length
        n = p + dir * (-1)
        points.append(p)
        if not use_normals:
            n = Vector((p[0], p[1], p[2] + length))
        normals.append(n)
        data.append([p, n])

    amt = bpy.data.armatures.new(obj.name + &quot;_vBones&quot;)
    rig = bpy.data.objects.new(obj.name + '_vRig', amt)

    bpy.context.collection.objects.link(rig)
    objects.active = rig

    bpy.ops.object.editmode_toggle()
    for i, l in enumerate(zip(points, normals)):        
        bone = amt.edit_bones.new(str(i))
        bone.head = l[0]
        bone.tail = l[1]
    bpy.ops.object.editmode_toggle()



bpy.ops.object.mode_set(mode='OBJECT')
AddBonesAtVertices(0.5, False)

The command for vertex parenting (I think)
bpy.ops.object.parent_set(type='VERTEX')


Yay!
Thanks for the answer below :) I managed to make a flag cloth sim and bake it to the bones and transfer that to Unity via FBX nicely.
Without edit bones.



Edit bones can be a little tricky, in script below instead have added the default single bone object, copied it to match number of verts, joined and constrained to the empties. 

IMO this is a little easier to use for something like this and avoids dicking around with edit mode and heads and tails.


Select a mesh object in object mode. Run Script.
Adds an empty vertex-parented to each vertex.
Empties added to a collection and linked to scene.
Add a single bone armature using the operator.
Make a copy, select and link to scene for each other vertex.
Join to original.
For each pose bone, constrain to each empty.
Make the constrained pose the rest pose. (Else all bones are Y up at origin)


Test script.

import bpy

context = bpy.context
scene = context.scene
ob = context.object
me = ob.data

mtcol = bpy.data.collections.new("Empties")
scene.collection.children.link(mtcol)

for v in me.vertices:
    mt = bpy.data.objects.new(f"Vert {v.index}", None)
    mtcol.objects.link(mt)
    mt.parent = ob
    mt.parent_type = 'VERTEX'
    mt.parent_vertices = [v.index] * 3

# add an armature
bpy.ops.object.armature_add(location=(0, 0, 0))
for i in range(len(me.vertices) - 1):
    ob = context.object.copy()
    ob.data = ob.data.copy()
    scene.collection.objects.link(ob)
    ob.select_set(True)

bpy.ops.object.join()
arm = context.object

for pb, mt in zip(arm.pose.bones, mtcol.objects):
    con = pb.constraints.new('COPY_LOCATION')
    con.target = mt


# set constrained pose as rest.
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.armature_apply()


Can a punishment be implemented for using the word &quot;vertice&quot; in a Q/A?