0
$\begingroup$

I want to be able to "commit" (or "realize") all Shape Key deformations on multiple selected objects using Python.

By this I mean: create a New Shape From Mix, then delete all other shapes, then finally delete the mix result shape, leaving the selected objects altered but with no more shape key data.

Here's what I've got so far:

import bpy
context = bpy.context
scene = context.scene
sel_objs = context.selected_objects

for each selected object...

New Shape From Mix

bpy.ops.object.shape_key_add(from_mix=True)

Get the number of shape key indices.

Iterate through and remove all except the last (leaving the last one as the true shape):

bpy.ops.object.shape_key_remove(all=False)

Then remove the final remaining one as well (for cleanup).

I'm unclear on some of the details when putting this all together. Any help appreciated.

$\endgroup$

1 Answer 1

2
$\begingroup$

This is how I've done this in one project:

import bpy
context = bpy.context
objs = context.selected_objects

for ob in objs:
    ob.hide_viewport = False  # should be visible
    if ob.data.shape_keys:
         ob.shape_key_add(name='CombinedKeys', from_mix=True)
         for shapeKey in ob.data.shape_keys.key_blocks:
             ob.shape_key_remove(shapeKey)

Avoid using ops, because you have to set context override every time, it is quite difficult because you have to guess that values have to be overridden

$\endgroup$
2
  • $\begingroup$ Thank you! It seems you're not specifying the order of key deletion. Is that because the newly added "CombinedKeys" is indexed last, and therefore automatically gets deleted last? $\endgroup$
    – Mentalist
    May 24, 2022 at 10:04
  • $\begingroup$ Yes. you are right. $\endgroup$
    – Crantisz
    May 24, 2022 at 10:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .