I didn't write the script, I can read it, but can't do much else.
This script is used for applying subdiv to a mesh with shapekeys, while preserving the latter. However it seems to be leaking a lot of meshes and/or other objects/data.
A file with DAZ character base mesh with some morphs grows up to 10GB in memory size and 5GB in file size. Now I have same character with a lot of morphs and there is no way to use this script without crashing. I need to subdivide and export the model with all the shapekeys.
In theory this script doesn't have to use so much memory should cleanup after itself in the process. How to make it preform without bloating the memory?
import bpy
def reset_shape_keys ():
for name, shape_key in get_active_block().items():
shape_key.value = 0
def get_active_block ():
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
select(object)
shape_key_count = len(get_active_block().items())
select_last_shape_key()
for i in range(0, shape_key_count):
bpy.ops.object.shape_key_remove(all=False)
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(apply_as='DATA', modifier=key)
select(selection)
def super_apply_modifiers ():
original = bpy.context.object
bpy.ops.object.duplicate_move()
backup = bpy.context.object
backup.name = 'backup'
remove_shape_keys(original)
apply_modifiers(original)
for key, shape_key in get_active_block().items():
select(backup)
bpy.ops.object.duplicate_move()
meshed_shape_key = bpy.context.object
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)a