Serious memory leak in BPyMesh

I needed a way to export meshes with bone deformations and I came across the BPyMesh script that is included with Blender and can be imported. I didn’t immediately see how to use the Mesh class to return the deformed mesh but the BPyMesh script makes it quite clear. The less said about the NMesh monstrosity the better. It returns the deformed mesh and although that thing doesn’t leak memory, it sucks up a lot and boy is it slow. I know that some people wanted NMesh to stay around but does it offer any extra functionality? If not and it’s for script compatibility, can the functions be mapped internally? I hate to think if there are any people still using it and it’s seriously affecting their export script performance.

Anyway, the leak in BPyMesh happens when calling the getMeshFromObject function. The default is set to apply vgroups by default. In this function, there are three lines of code:

if tempob==None:
____tempob= Blender.Object.New(‘Mesh’)

I don’t know why it leaks so much given that it’s just a link so maybe it’s a deeper issue in Blender. The variable tempob is never used again so these lines can be removed and they are what’s causing the problem because it doesn’t leak after removing them.

I tested it on a smallish scene and every export increased Blender’s Ram usage by 10MB. Like I say, this was on a scene with low poly objects (maybe 3000 verts total) so it would be a big problem for higher scenes.

Incidentally, is the preferred method for getting deformed data to do this:

mesh = Blender.Mesh.New()
try: mesh.getFromObject(obj)
except: # raise some error

This works fast and it doesn’t leak memory. Like I say, why can’t NMesh.getRawFromObject just be mapped to doing that?

Its not a memory leak, rather a quirk of Blender…

every new mesh wont be de-allocated until Blender reloads the file.
getMeshFromObject can accept a mesh in its optional args. so you might be best of doing what I did for the OBJ exporter (see

me = Mesh.Get(’~dummy~’)
me = Mesh.New(’~dummy~’)

Then every time you run the script only 1 mesh will be created… if you want to be realy safe also check that ~dummy~ has 0 users

When your done do me.verts = None, that will clear the mesh and reclaim most of the memory…

I could go into more details but take a look at OBJ export.

Of course. :rolleyes: I forgot I wasn’t freeing up the mesh. I need to stop posting so early in the morning.

What threw me off was that the tempob variable is not accessible but it is just a link so I guess that Blender won’t free up the memory when the mesh has an active link.