Skip to content

Instantly share code, notes, and snippets.

@fire
Last active November 21, 2023 09:54
Show Gist options
  • Save fire/495fb6a35168500df53c002695a1f5fe to your computer and use it in GitHub Desktop.
Save fire/495fb6a35168500df53c002695a1f5fe to your computer and use it in GitHub Desktop.
# MIT LICENSE
# Authored by iFire#6518 and alexfreyre#1663
# This code ONLY apply to a mesh and simulations with ONLY the same vertex number
import bpy
#Converts a MeshCache or Cloth modifiers to ShapeKeys
frame = bpy.context.scene.frame_start
for frame in range(bpy.context.scene.frame_end + 1):
bpy.context.scene.frame_current = frame
# For alembic files converted to MDD and loaded as MeshCache.
bpy.ops.object.modifier_apply_as_shapekey(keep_modifier=True, modifier="MeshCache")
bpy.ops.object.modifier_apply_as_shapekey(keep_modifier=True, modifier="MeshSequenceCache")
#for cloth simulations inside blender using a Cloth modifier
#bpy.ops.object.modifier_apply_as_shapekey(keep_modifier=True, modifier="Cloth")
# loop through shapekeys and add as keyframe per frame
# https://blender.stackexchange.com/q/149045/87258
frame = bpy.context.scene.frame_start
for frame in range(bpy.context.scene.frame_end + 1):
bpy.context.scene.frame_current = frame
for shapekey in bpy.data.shape_keys:
for i, keyblock in enumerate(shapekey.key_blocks):
if keyblock.name != "Basis":
curr = i - 1
if curr != frame:
keyblock.value = 0
keyblock.keyframe_insert("value", frame=frame)
else:
keyblock.value = 1
keyblock.keyframe_insert("value", frame=frame)
# bpy.ops.object.modifier_remove(modifier="MeshCache")
# bpy.ops.object.modifier_remove(modifier="Cloth")
@sjkillen
Copy link

sjkillen commented Nov 26, 2022

This looks like what I need, thanks.

@MShakerin
Copy link

MShakerin commented Dec 22, 2022

This gives error for Mesh Sequence Cache!!
I changed it like this
bpy.ops.object.modifier_apply_as_shapekey(keep_modifier=True, modifier="MeshSequenceCache")

@fire
Copy link
Author

fire commented Dec 22, 2022

Will check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment