Applying Displace as Shape Keys?

Hello.
I’m trying to apply a Displace modifier set to Object (empty moving from A to B) as Shape Keys but it seems like it’s not working.

image

image

The empty is moving from A to B, the mesh is deforming but then if I click “Apply as Shape Keys” on the Displace Modifier nothing happens and it’s as if I removed the modifier.

Am I trying to do something not possible or is there a way around it?
Thank you

Nothing happens as in there’s no Shape key named Displace in Object Data Properties?

If you hit Apply as Shape Key there should be a new shapekey (with value set to 0) and the modifier gets removed.
Save as Shape Key does not remove the mod.

Hey Billie thanks.
So if I hit Apply as Shape Key, there is in fact a new shapekey and if I scroll between 0 and 1 I can see the modifier being applied to the shape. The problem is, that it does just that. It goes from flat (0, no modifier) to displaced (1, modifier).

What I’m trying to apply is the animation. Currently when the empty moves from A to B I have this nice “wave like” effect. But when I apply the modifier as shape key I loose that animation.

P.S. I tried exporting as Alembic and it is working! I’m now trying to having it play nicely but that’s a problem outside of Blender. So I guess a “workaround” for now might be to just export as Alembic, although the filesize is huge compared to fbx (which is what I was trying to export).

Oh. As I understand Shapekey just stores new vertex positions (one for each vertex). And the interpolation between shapes is strictly linear.
You can probably achieve some fancy wavy animation by having multiple different shapekeys and stacking them together somehow, but that’s a whole other story.

I see, I thought that might be the case but I was hoping there was a way to do this. Well, good to know and thanks for taking the time to explain it :slight_smile: A stack of shapekeys could be an alternative solution and since the mesh is “modular” I could always have an array of them. But anyway, the alembic does the job for now so problem solved :laughing:

Hi Harry,
Why do you want to apply the displace mod?

You can create a shapekey using the last state of your mesh with modifiers applied for each frame and animate those shapekeys using this script below. I wrote it to convert an Alembic animation into shapekeys

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)
1 Like

This is excatly what I was looking for but unfortunately it doesnt seem like the shapekeys actually contain any data after this

The vertex postions are correctly retrieved (i’ve output and checked) but the shape key data doesnt seem to be correctly applied

do you know how to fix it?

Nevermind your code works perfectly, thanks again, i really dont know how you figured this out the documentation on all of this seems really bad

for me the shape key lock button was enabled (the little pin) which made none of the shape keys work
grafik

1 Like

All the magic is on the “evaluated_depsgraph_get()”. I made the script to convert an alembic animation to shapekey animation but then found out I also can bake armature animation or anything into shapekeys. So, like, I have an animated tree generated with the ‘Sapling Three Gen’ addon and it’s really heavy due to the branch and bone amounts, but I was able to bake the armature animation into shapekeys and the code worked perfectly.