cloth edit script.

I know nothing of scripting. However I used to scripts from the net, one adds empties to each vertex, the other bones, what I need to know is how to tell blender to add copy location constaint to each bone for each empty?? it took me many hours to go through every bone to give it the right target.

This is the animation I did with the scripts.

Here are the 2 scripts

import bpy
import bmesh
context = bpy.context
scene = context.scene
obj = context.edit_object
mesh = obj.data
bm = bmesh.from_edit_mesh(mesh)
empties = []

selected verts

for v in [v for v in bm.verts if v.select]:
empty = bpy.data.objects.new(“Empty”, None)
empty.parent = obj
empty.parent_type = ‘VERTEX’
empty.parent_vertices = [v.index] * 3
scene.objects.link(empty)
empty.matrix_parent_inverse.identity()
empties.append((v.index, empty))

import bpy
from mathutils import Vector
from pprint import pprint
def AddBonesAtVertices(length = 1, use_normals = False):
scn = bpy.context.scene
if (None ==scn.objects.active):
return
obj = scn.objects.active
if (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§
if not use_normals:
n = Vector((p[0], p[1], p[2] + length))
pprint§
normals.append(n)
data.append([p, n])
amt = bpy.data.armatures.new(obj.name + “_vBones”)
rig = bpy.data.objects.new(obj.name + ‘_vRig’, amt)
scn.objects.link(rig)
scn.objects.active = rig
scn.update()
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.2)

I have solved the problem except the empties have a location of 0,0,0 i need them to have the location of the vertex, does anyone know how to fix this, since this script was copy and paste.

#select object and go into edit mode and run script.

import bpy
import bmesh

context = bpy.context
scene = context.scene
obj = context.edit_object
mesh = obj.data
bm = bmesh.from_edit_mesh(mesh)
empties = []

selected verts

for v in [v for v in bm.verts if v.select]:
empty = bpy.data.objects.new(“Empty”, None)
empty.parent = obj
empty.parent_type = ‘VERTEX’
empty.parent_vertices = [v.index] * 3
scene.objects.link(empty)
empty.matrix_parent_inverse.identity()
empties.append((v.index, empty))

bpy.ops.object.editmode_toggle()

context = bpy.context
scene = context.scene

Empty = [o for o in scene.objects if o.type == ‘EMPTY’]

for l in Empty:
# add bone
bpy.ops.object.armature_add(location=l.location)
bpy.ops.object.posemode_toggle()
bpy.ops.pose.constraint_add(type=‘COPY_LOCATION’)
bpy.context.object.pose.bones[“Bone”].constraints[“Copy Location”].target = l
bpy.ops.object.posemode_toggle()

SOLVED

You then bake the armature to create an action. With the bake option of Only selected, Visual keying
Duplicate the mesh and remove cloth sim and duplicate armature and remove all constraints
Apply the duplicated armature to the duplicate mesh with auto weight paint.
Now you can join multiple bakes with baked actions, along with shape keys to fix cloth problems!!

#select object and go into edit mode and run script.

import bpy
import bmesh


context = bpy.context
scene = context.scene
obj = context.edit_object
mesh = obj.data
bm = bmesh.from_edit_mesh(mesh)
empties = []
# selected verts
for v in [v for v in bm.verts if v.select]:
    empty = bpy.data.objects.new("Empty", None)
    empty.parent = obj
    empty.parent_type = 'VERTEX'
    empty.parent_vertices = [v.index] * 3
    scene.objects.link(empty)
    
bpy.ops.object.editmode_toggle()




context = bpy.context
scene = context.scene


Empty = [o for o in scene.objects if o.type == 'EMPTY']


for l in Empty:
    # add bone
    bpy.ops.object.armature_add(location=l.location)
    bpy.ops.object.posemode_toggle()
    bpy.ops.pose.constraint_add(type='COPY_LOCATION')
    bpy.context.object.pose.bones["Bone"].constraints["Copy Location"].target = l
    bpy.ops.object.posemode_toggle()
    
bpy.ops.object.select_grouped(type='TYPE')
bpy.ops.object.join()
bpy.ops.object.posemode_toggle()
bpy.ops.pose.select_all(action='TOGGLE')
bpy.ops.pose.armature_apply()
bpy.ops.object.posemode_toggle()