the3dadvantage/Modeling-Cloth

Can't get vertex locations with modifiers without memory leaks in ram

Opened this issue · 6 comments

There is a bug in the collision system when the collider object has shape keys. I'm using: me = ob.to_mesh() to get the location of the vertices with modifiers applied, for armatures and stuff. When I do bpy.data.meshes.remove(me), the shape keys from the duplicate mesh are still in bpy.data.shape_keys. They keep piling up until I run out of ram. I can't figure out a way around this. If anyone has a suggestion please let me know.

Thanks!

I think its already solved in previous commit. I notice this issue before and the problem is you forgot to delete proxies on run_handler function. In current code I see the proxy is no longer used so the problem is solved. I just tried it and I found no memory leaks.

I did solve it on the cloth object. Had to ditch the blender normal calculations and do my own. It's still a problem for collision objects. For example an animated character with an armature. I have to create proxies for that. I am deleting the proxies but the shape keys get duplicated also and those don't go away from bpy.data.shape_keys when you delete the mesh so memory still piles up.

Oh, I see. So you already remove those problematic code? How about track those extra shape keys data then delete them? I dunno if that's possible though, maybe can you share the code here?

import bpy
import numpy as np

ob = bpy.context.object
proxy = ob.data.to_mesh(bpy.context.scene, False, 'PREVIEW')
arr = np.zeros(len(ob.data.vertices) * 3)
proxy.vertices.foreach_get('co' arr)
bpy.data.meshes.remove(proxy)

The trouble is, if the object I'm duplicating has shape keys, the shape keys don't get deleted. Try the code above on an object with shape keys. Open the outliner and show orphan data and you'll see shape keys are getting duplicated over and over. I can't find a way to delete those keys.

I think I found the solution, you can delete shape keys using temporary object, here's the code:

import bpy
import numpy as np

ob = bpy.context.object
proxy = ob.to_mesh(bpy.context.scene, False, 'PREVIEW')

arr = np.zeros(len(ob.data.vertices) * 3)
proxy.vertices.foreach_get('co', arr)

temp_ob = bpy.data.objects.new('__TEMP', proxy)
for key in proxy.shape_keys.key_blocks:
    temp_ob.shape_key_remove(key)

bpy.data.meshes.remove(proxy)
bpy.data.objects.remove(temp_ob)

That surely does work for deleting the shape keys.
I was just testing the addon again and for some reason it's not creating extra duplicate keys even without the code. What the heck? There must have been something different about the proxy I was using on the cloth object versus the collisions object. No idea what that difference was though.