Last active
May 9, 2023 10:41
-
-
Save terokorp/c2e0bdadf4cffcc856098b93b84d38a9 to your computer and use it in GitHub Desktop.
This file contains 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
# https://blender.stackexchange.com/questions/56795/script-outdated-shape-keys-and-applying-subdivision-surface-modifier | |
import bpy | |
def reset_shape_keys (): | |
for name, shape_key in get_active_block().items(): | |
shape_key.value = 0 | |
def get_active_block (): | |
if bpy.context.object.active_shape_key is None or bpy.context.object.active_shape_key.id_data is None: | |
bpy.context.object.active_shape_key_index = 0 | |
block_id = bpy.context.object.active_shape_key.id_data.name | |
return bpy.data.shape_keys[block_id].key_blocks | |
def select (selection): | |
bpy.ops.object.select_all(action='DESELECT') | |
selection.select_set(True) | |
bpy.context.view_layer.objects.active = selection | |
def select_last_shape_key (): | |
shape_key_count = len(get_active_block().items()) | |
bpy.context.object.active_shape_key_index = shape_key_count - 1 | |
def remove_shape_keys (object): | |
selection = bpy.context.object | |
object.shape_key_clear() | |
select(selection) | |
def apply_modifiers (object): | |
selection = bpy.context.object | |
select(object) | |
for key, modifier in object.modifiers.items(): | |
if key != 'Armature': | |
bpy.ops.object.modifier_apply(modifier=key) | |
select(selection) | |
def super_apply_modifiers (): | |
original = bpy.context.object | |
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={ | |
"linked":False, "mode":'TRANSLATION'}, | |
TRANSFORM_OT_translate={ | |
"value":(0, 0, 0), | |
"constraint_axis":(False, False, False), | |
"orient_matrix":[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], | |
"orient_matrix_type":'GLOBAL', | |
"mirror":False, | |
"use_proportional_edit":False, | |
"proportional_edit_falloff":'SMOOTH', | |
"proportional_size":1, | |
"snap":False, "snap_target":'CLOSEST', | |
"snap_point":(0, 0, 0), | |
"snap_align":False, | |
"snap_normal":(0, 0, 0), | |
"gpencil_strokes":False, | |
"texture_space":False, | |
"remove_on_cancel":False, | |
"release_confirm":False, | |
"use_accurate":False | |
}) | |
backup = bpy.context.object | |
backup.name = original.name + '_backup' | |
remove_shape_keys(original) | |
apply_modifiers(original) | |
try: | |
for key, shape_key in get_active_block().items(): | |
select(backup) | |
if(key == "Basis"): | |
continue | |
bpy.ops.object.duplicate_move( | |
OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, | |
TRANSFORM_OT_translate={ | |
"value":(0, 0, 0), | |
"constraint_axis":(False, False, False), | |
"orient_matrix":[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], | |
"orient_matrix_type":'GLOBAL', | |
"mirror":False, | |
"use_proportional_edit":False, | |
"proportional_edit_falloff":'SMOOTH', | |
"proportional_size":1, | |
"snap":False, | |
"snap_target":'CLOSEST', | |
"snap_point":(0, 0, 0), | |
"snap_align":False, | |
"snap_normal":(0, 0, 0), | |
"gpencil_strokes":False, | |
"texture_space":False, | |
"remove_on_cancel":False, | |
"release_confirm":False, | |
"use_accurate":False | |
}) | |
print("key: " + key); | |
meshed_shape_key = bpy.context.object | |
meshed_shape_key.name = backup.name + '_' + key | |
select(meshed_shape_key) | |
reset_shape_keys() | |
get_active_block()[key].value = 1 | |
bpy.ops.object.convert(target='MESH') | |
select(original) | |
meshed_shape_key.select_set(True) | |
bpy.ops.object.join_shapes() | |
select_last_shape_key() | |
bpy.context.object.active_shape_key.name = key | |
select(meshed_shape_key) | |
bpy.ops.object.delete(use_global=False) | |
backup.hide_set(True) | |
except Exception as err: | |
original_name = original.name | |
original.name = original.name + "_broken" | |
backup.name = original_name | |
select(original) | |
# bpy.ops.object.delete(use_global=False) | |
super_apply_modifiers() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment