import bpy
import bmesh
#
# experiment to convert armature deformations into shape keys.
#
# click the Run Script button over and over to add another shape key to the active object and advance the frame.
#
def getSelectedObjects():
rval = []
for obj in bpy.context.scene.objects:
if obj.select:
rval.append(obj)
return rval
def setSubtract(bigger, smaller):
rval = []
for x in bigger:
if not x in smaller:
rval.append(x)
return rval
def applyBoneParentMatrix(obj):
m2 = obj.matrix_world
obj.parent = None
obj.matrix_world =m2
def applyOneArmature(tgt, skn, ghost):
for obj in bpy.context.scene.objects:
obj.select = (obj==tgt)
scn = bpy.context.scene
bpy.ops.object.duplicate()
o2 =scn.objects.active
bpy.ops.object.modifier_apply(modifier='Armature')
newVerts = [v.co for v in o2.data.vertices]
print ( newVerts)
if ghost.data.shape_keys is None:
ghost.shape_key_add("Basis")
if True or None is ghost.data.shape_keys.key_blocks.get(skn):
print("adding shape key %s"%skn)
ghost.shape_key_add(skn)
else:
print("shape key %s exists"%skn)
bm =bmesh.new()
bm.from_mesh(ghost.data)
bm.verts.ensure_lookup_table()
sl = bm.verts.layers.shape.get(skn)
for i in range(len(bm.verts)):
bm.verts[i][sl] = newVerts[i]
bm.to_mesh(ghost.data)
sk = ghost.data.shape_keys
sk.key_blocks[skn].value = 1
sk.keyframe_insert(data_path='key_blocks["%s"].value'%skn, frame=scn.frame_current)
sk.key_blocks[skn].value = 0
sk.keyframe_insert(data_path='key_blocks["%s"].value'%skn, frame=scn.frame_current-1)
sk.keyframe_insert(data_path='key_blocks["%s"].value'%skn, frame=scn.frame_current+1)
scn.objects.unlink(o2)
scn.objects.active = tgt
def applyArmatures(tgt, ghost):
applyOneArmature(tgt, "frame %d"%scn.frame_current , ghost)
def applyArmatures1(tgt):
applyOneArmature(tgt, "frame %d"%scn.frame_current )
objs = getSelectedObjects()
#print(objs)
old = bpy.context.scene.objects[:]
bpy.ops.object.duplicate()
newObjs = setSubtract(bpy.context.scene.objects, old)
for obj in newObjs:
applyBoneParentMatrix(obj)
obj.select = False
for o in objs:
o.select = True
print (newObjs)
scn = bpy.context.scene
ghost = scn.objects.get("ghost")
if (ghost is None):
o2 = scn.objects.active
bpy.ops.object.duplicate()
ghost = scn.objects.active
ghost.name = "ghost"
scn.objects.active = o2
applyArmatures(scn.objects.active, ghost)
scn.frame_current = scn.frame_current +1
|
Blender python API quick-start
Syntax highlighting by Pygments.