blender的python脚本内存泄漏

blender的python脚本内存泄漏,python,memory-leaks,python-3.x,blender,Python,Memory Leaks,Python 3.x,Blender,我对python相当陌生(我通常使用c++)。我编写了一个python脚本,使用光线跟踪算法将Blender对象转换为二进制掩码文件 我最终导致了巨大的内存泄漏。对于M=512(如下所示),脚本在运行时会增加RAM的使用,最终结果是使用了高达5.7GB的内存。在脚本运行之后,RAM的使用也会保持不变,直到我关闭Blender 我不确定open()命令是否将打开的文件存储在RAM中(我猜是的),但这只占了256MB的RAM使用量,因为这就是生成的文件大小,并且也没有解释为什么在fo.close()

我对python相当陌生(我通常使用c++)。我编写了一个python脚本,使用光线跟踪算法将Blender对象转换为二进制掩码文件

我最终导致了巨大的内存泄漏。对于M=512(如下所示),脚本在运行时会增加RAM的使用,最终结果是使用了高达5.7GB的内存。在脚本运行之后,RAM的使用也会保持不变,直到我关闭Blender

我不确定
open()
命令是否将打开的文件存储在RAM中(我猜是的),但这只占了256MB的RAM使用量,因为这就是生成的文件大小,并且也没有解释为什么在
fo.close()
之后RAM使用量仍然存在

我确信我遗漏了一些非常简单的东西,但由于不太熟悉python,我很难弄清楚它是什么。我已尝试通过放置行来清除
pointInsideMesh()
函数中使用的所有变量

axis  = None
mat1  = None
mat   = None
orig  = None
count = None
location = None
normal   = None
index    = None
outside1 = None
outside2 = None
就在
return
语句之前,但这并没有堵塞内存泄漏。这是我的密码:

import mathutils, numpy, bpy





def pointInsideMesh(point,ob):

    axes = [ mathutils.Vector((1,0,0)) ]
    outside1 = False
    for axis in axes:

        mat1 = mathutils.Matrix(ob.matrix_world)
        mat=mat1.invert()

        orig = mat1*point


        count = 0
        while True:
            location,normal,index = ob.ray_cast(orig,axis*10000.0)
            if index == -1: break
            count+= 1

            orig = location + axis*0.00001



        if (count%2 == 0):
            outside1 = True
            break




    axes = [ mathutils.Vector((0,1,0)) ]
    outside2 = False
    for axis in axes:

        mat1 = mathutils.Matrix(ob.matrix_world)
        mat=mat1.invert()

        orig = mat1*point


        count = 0
        while True:
            location,normal,index = ob.ray_cast(orig,axis*10000.0)
            if index == -1: break
            count+= 1

            orig = location + axis*0.00001



        if (count%2 == 0):
            outside2 = True
            break

    outside = outside1 or outside2
    return not outside




ob = bpy.context.active_object
M = 512
fileOut = 'D:\images\\maskFile.txt'

fo = open(fileOut , "w")
for n in range(0,M):
    for m in range(0,M):
        for l in range(0,M):
            if pointInsideMesh( mathutils.Vector(((l+1)/M-0.5,(m+1)/M-0.5,(n+1)/M-0.5)), ob ):
                fo.write("1")
            else:
                fo.write("0")
            if l < M-1:
                fo.write(" ")
        fo.write("\n") 
    fo.write("\n")          
fo.close()
导入数学,numpy,bpy
def pointInsideMesh(点,ob):
轴=[数学向量((1,0,0))]
外部1=错误
对于轴中的轴:
mat1=mathutils.Matrix(ob.Matrix\u world)
mat=mat1.invert()
原点=mat1*点
计数=0
尽管如此:
位置,正常,索引=ob.射线投影(原点,轴*10000.0)
如果索引==-1:中断
计数+=1
原点=位置+轴*0.00001
如果(计数%2==0):
外部1=真
打破
轴=[数学向量((0,1,0))]
外部2=错误
对于轴中的轴:
mat1=mathutils.Matrix(ob.Matrix\u world)
mat=mat1.invert()
原点=mat1*点
计数=0
尽管如此:
位置,正常,索引=ob.射线投影(原点,轴*10000.0)
如果索引==-1:中断
计数+=1
原点=位置+轴*0.00001
如果(计数%2==0):
外部2=真
打破
外部=外部1或外部2
不要在外面回来
ob=bpy.context.active\u对象
M=512
文件输出='D:\images\\maskFile.txt'
fo=打开(文件输出,“w”)
对于范围(0,M)内的n:
对于范围内的m(0,m):
对于范围(0,M)内的l:
如果pointInsideMesh(数学向量(((l+1)/M-0.5,(M+1)/M-0.5,(n+1)/M-0.5)),ob:
fo.写(“1”)
其他:
fo.写入(“0”)
如果l
任何帮助都将不胜感激:)

更新:

import mathutils, numpy, bpy

def pointInsideMesh(point,ob):
    return False

ob = bpy.context.active_object
M = 512
fileOut = 'D:\images\\maskFile.txt'

fo = open(fileOut , "w")
for n in range(0,M):
    for m in range(0,M):
        for l in range(0,M):
            if pointInsideMesh( mathutils.Vector(((l+1)/M-0.5,(m+1)/M-0.5,(n+1)/M-0.5)), ob ):
                fo.write("1")
            else:
                fo.write("0")
            if l < M-1:
                fo.write(" ")
        fo.write("\n") 
    fo.write("\n")          
fo.close()
导入数学,numpy,bpy
def pointInsideMesh(点,ob):
返回错误
ob=bpy.context.active\u对象
M=512
文件输出='D:\images\\maskFile.txt'
fo=打开(文件输出,“w”)
对于范围(0,M)内的n:
对于范围内的m(0,m):
对于范围(0,M)内的l:
如果pointInsideMesh(数学向量(((l+1)/M-0.5,(M+1)/M-0.5,(n+1)/M-0.5)),ob:
fo.写(“1”)
其他:
fo.写入(“0”)
如果l
重现问题,但

import mathutils, numpy, bpy

def pointInsideMesh():
    return False

ob = bpy.context.active_object
M = 512
fileOut = 'D:\images\\maskFile.txt'

fo = open(fileOut , "w")
for n in range(0,M):
    for m in range(0,M):
        for l in range(0,M):
            if pointInsideMesh():
                fo.write("1")
            else:
                fo.write("0")
            if l < M-1:
                fo.write(" ")
        fo.write("\n") 
    fo.write("\n")          
fo.close()
导入数学,numpy,bpy
def pointInsideMesh():
返回错误
ob=bpy.context.active\u对象
M=512
文件输出='D:\images\\maskFile.txt'
fo=打开(文件输出,“w”)
对于范围(0,M)内的n:
对于范围内的m(0,m):
对于范围(0,M)内的l:
如果pointInsideMesh():
fo.写(“1”)
其他:
fo.写入(“0”)
如果l

没有,唯一的区别是在这两个示例中的第一个示例中,测试点(和对象)的位置被传递给函数,而在第二个示例中,没有传递参数。在C++中,即使通过值,函数中创建的参数副本也会在函数返回时从内存中清除。但是在python中,参数的传递方式不同,这一定是问题的根源,因为我不太明白它是如何工作的。

我能看到的唯一会导致内存泄漏的是
ob
对象

您对该对象执行了
512^3次
操作。如果
ob.ray\u cast
ob
对象上存储了一些数据,那么我可以看出这是一个问题。试着替换

ob.ray_cast(orig,axis*10000.0)

使用3个静态值,查看内存问题是否依然存在。搅拌机的C端可能有内存泄漏。

谢谢您的评论。我第一次尝试了这个方法,但没有成功,但后来我做了更重要的改变,试图找出泄漏发生的地方。这可以在我原来问题的更新中看到。再次感谢:)