Created
August 28, 2024 08:23
-
-
Save gorborukov/56c81d44cbc2f285b1b748c864abff12 to your computer and use it in GitHub Desktop.
Blender convert alembic animation to shape keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from https://blenderartists.org/t/how-to-bake-alembic-mesh-to-keyframes-for-nla/1461379/12 | |
import bpy | |
ob = bpy.context | |
obAc = ob.active_object | |
mesh_data = obAc.data | |
start_frame = bpy.context.scene.frame_start | |
end_frame = bpy.context.scene.frame_end | |
if not obAc.data.shape_keys: | |
obAc.shape_key_add(name="Basis") | |
# Create shape keys for each frame | |
for frame in range(start_frame, end_frame + 1): | |
bpy.context.scene.frame_set(frame) | |
# Evaluate the mesh with modifiers | |
depsgraph = bpy.context.evaluated_depsgraph_get() | |
object_eval = obAc.evaluated_get(depsgraph) | |
mesh_eval = object_eval.data | |
# Create a new shape key and set its points based on the current frame | |
shape_key = obAc.shape_key_add(name=str(frame)) | |
# Collect vertex positions for the current frame | |
vertices = [vertex.co for vertex in mesh_eval.vertices] | |
# Set shape key data | |
shape_key.data.foreach_set('co', [c for v in vertices for c in v]) | |
if obAc.data.shape_keys: | |
shape_keys = obAc.data.shape_keys.key_blocks | |
# Iterate through shape keys and set keyframes | |
for frame in range(start_frame, end_frame + 1): | |
ob.scene.frame_set(frame) | |
for shape_key in shape_keys: | |
if shape_key.name.isdigit(): | |
value = 1.0 if int(shape_key.name) == frame else 0.0 | |
shape_key.value = value | |
shape_key.keyframe_insert(data_path='value', index=-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment