What I want
I have two sets of objects and want to intersect every object of one set with every object of the other set by means of the boolean modifier. The output can be reduced to the vertices of intersection for every object pair which I write into an ascii file.
The problem
When executing the script, the execution time for the first iterations is low, however, it takes forever with further iterations. The problem seems to be well known (see here and here; and related to bad programming/use of blender api) when using many bpy.ops.
which causes updating the whole scene every time.
But in my understanding this updating should not make things worse when the number of objects is low for every update. That's why I delete every object and mesh except for the two objects to intersect with each other.
I guess the problem is that not everything of the object is deleted even though I delete all objects and meshes after each iteration and the remaining part will then be updated with an increasing number of "something". This may be confirmed by the continously increasing memory Blender needs.
The question
What kind of items does remain to cause the increase of computation time (and memory) although I delete all objects and meshes (len(list(bpy.data.meshes))
and len(list(bpy.data.objects))
is zero) after every iteration?
Theoretically it should be working if restarting blender after each iteration, but this will cause a large overhead for startup. Maybe there is something like reset scene?
For the purpose of deletion I use the following code:
def deleteObj(obj):
bpy.ops.object.select_all(action='DESELECT')
obj.select = True
bpy.ops.object.delete(use_global = True)
and
meshes = list(bpy.data.meshes)
for me in meshes:
me.user_clear()
bpy.data.meshes.remove(me)
For the intersection I use:
def getIntersection (obj1, obj2):
bpy.ops.object.select_all(action='DESELECT')
obj1.select = True
bpy.context.scene.objects.active = obj1
mod = obj1.modifiers.new('Boolean', type='BOOLEAN')
mod.object = obj2
mod.operation = 'INTERSECT'
bpy.ops.object.modifier_apply(apply_as='DATA', modifier = mod.name)
Edit Added minimised code:
# read-in functions
# set parameters for objects creation
# open file for output
for i in R1:
obj1 = createObj(Parameters1, i)
for j in R2:
obj2 = createObj(Parameters2, j)
mesh = getIntersection(obj1, obj2)
deleteObj(obj2)
Verts = list(mesh.vertices)
if len(Verts) > 0:
writeVerticesToFiles(file, mesh)
bpy.data.meshes.remove(mesh)
deleteObj(obj1)
meshes = list(bpy.data.meshes)
for me in meshes:
me.user_clear()
bpy.data.meshes.remove(me)