1

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

  • 1
    Question is a little broad, possibly the title answer is always yes. Recommend looking at https://blender.stackexchange.com/questions/7358/python-performance-with-blender-operators?r=SearchResults&s=2|61.6507 A number of the operators used above could be replaced with lower level API calls, many of such have been covered previously. – batFINGER Aug 20 '20 at 13:29
  • Sorry, I edited the question. – user3024701 Aug 20 '20 at 16:12
  • 1
    Appears to be a later incarnation here https://github.com/JanOtt/ShapeKey-Helpers Often see links to XY Problem Perhaps X here, ie the intended result, is "applying subdiv to a mesh with shapekeys, while preserving the latter" and Y is any question asking to "blah" that script. Suggest making question title "Applying modifiers whilst preserving shapekeys" : ... IIRC there are a number already. Now the script above is an example of how to get to that result but is slow or crashes on a large mesh etc etc. in question body Still broad. – batFINGER Aug 20 '20 at 17:05
  • 1
    The addon doesnt work for some reason, asking on the forum. Edited the title. It's long but it't the shortest version that is at least informative that I could come up with. And it's not a question, which is not ideal. – user3024701 Aug 20 '20 at 18:00

0 Answers0