Skip to content

Instantly share code, notes, and snippets.

@todashuta
Last active April 8, 2025 13:27
Show Gist options
  • Select an option

  • Save todashuta/d91bad7d9401799083911be9c6f64b6d to your computer and use it in GitHub Desktop.

Select an option

Save todashuta/d91bad7d9401799083911be9c6f64b6d to your computer and use it in GitHub Desktop.
ソフトボディ等の頂点の動きをボーンアニメーション化するやつ
import bpy
context = bpy.context
base_ob = bpy.data.objects['Suzanne1'] # ソフトボディを設定したメッシュ
target_ob = bpy.data.objects['Suzanne2'] # base_obをリンク複製してモディファイアはすべて消したもの
target_ob.vertex_groups.clear()
vg_names = []
num_vertices = len(target_ob.data.vertices)
for i, v in enumerate(target_ob.data.vertices):
name = "v-"+str(i).zfill(len(str(num_vertices)))
#print(name)
vg_names.append(name)
group = target_ob.vertex_groups.new(name=name)
group.add([i], 1.0, 'REPLACE')
#print(vg_names)
mw = target_ob.matrix_world.copy()
depsgraph = context.evaluated_depsgraph_get()
target_ob_eval = target_ob.evaluated_get(depsgraph)
target_mesh_eval = target_ob_eval.data
vertices = [target_ob_eval.matrix_world @ v.co for v in target_mesh_eval.vertices]
#print(vertices)
bpy.ops.object.armature_add()
context.active_object.matrix_world = mw # ???
context.active_object.data.display_type = 'STICK'
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.armature.select_all(action='SELECT')
bpy.ops.armature.delete()
for name, v in zip(vg_names, vertices):
context.scene.cursor.location = v
bpy.ops.armature.bone_primitive_add(name=name)
bpy.ops.object.mode_set(mode='POSE')
for i in range(context.scene.frame_start, context.scene.frame_end+1):
context.scene.frame_current = i
print(i)
depsgraph = context.evaluated_depsgraph_get()
base_ob_eval = base_ob.evaluated_get(depsgraph)
base_mesh_eval = base_ob_eval.data
vertices = [base_ob_eval.matrix_world @ v.co for v in base_mesh_eval.vertices]
for name, v in zip(vg_names, vertices):
ob = context.object
pb = ob.pose.bones[name]
mw = ob.convert_space(
pose_bone=pb,
matrix=pb.matrix,
from_space='POSE',
to_space='WORLD')
mw.translation = v
pb.matrix = ob.convert_space(
pose_bone=pb,
matrix=mw,
from_space='WORLD',
to_space='POSE')
pb.keyframe_insert('location')
bpy.ops.object.mode_set(mode='OBJECT')
mod = target_ob.modifiers.new('', 'ARMATURE')
mod.object = context.object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment