Can you help optimize this Modifiers Boolean script?

The script below works just fine, but I’d like to create more 3D ‘grunge’ like this faster.

[the results are all beautifully clean BTW.]

When I push the range up to 80 on my machine things really begin to slow down.

I would like to do hundreds of booleans!

My first thought is to do 20 booleans or so at a time, repeat and just be patient, but if any of you have a better idea, please do post…


# from bpy import context
import bpy
import random
from math import sin, cos, pi, radians

def removeObjects( scn ):
    for ob in scn.objects:
        if ob.type == 'MESH':
            scn.objects.unlink( ob )
 
scn = bpy.context.scene
removeObjects( scn )

add_cube = bpy.ops.mesh.primitive_cube_add

add_cube(location=(0,0,5))
ob1 = bpy.context.object
ob1.scale = (1, 1, 5)


for i in range(50):
    x = 3 * random.random() - 1.5
    y = 3 * random.random() - 1.5
    z = 10 * random.random()
    rotX = 1.51 * random.random()
    rotY = 1.51 * random.random()
    rotZ = 1.51 * random.random()
    add_cube(location=(x, y, z), rotation=(rotX, rotY, rotZ) )
    ob = bpy.context.object
    ob.scale = (0.5, 0.5, 0.5)
    boo = ob1.modifiers.new('Booh', 'BOOLEAN')
    boo.object = scn.objects.get(ob.name)
    boo.operation = 'DIFFERENCE'
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Booh")
    scn.objects.unlink(ob)

The problem is that (despite your valiantest effort) you aren’t actually applying the modifiers after adding them. So at the end of the day you have a string of 80 modifiers, which is why it bogs down. Some fiddling fixed it:


bpy.context.scene.objects.active = ob1
bpy.ops.object.modifier_add(type='BOOLEAN')
boo = ob1.modifiers[0]
boo.object = ob
boo.operation = 'DIFFERENCE'
bpy.ops.object.modifier_apply(modifier=boo.name)
scn.objects.unlink(ob)

supergra is the dude! Thanks!

With supergra’s fix the completion time for 80 Booleans dropped from something like 3 minutes down to 10 seconds.

Looking the two pieces of code, I think it’s interesting that my dumby code actually ran.

It’s now going to be a lot easier and simpler to build complicated 3D surfaces using Booleans.

If anybody is interested, here’s the updated routine:


# from bpy import context
import bpy
import random
from math import sin, cos, pi, radians

def removeObjects( scn ):
    for ob in scn.objects:
        if ob.type == 'MESH':
            scn.objects.unlink( ob )
 
scn = bpy.context.scene
removeObjects( scn )

add_cube = bpy.ops.mesh.primitive_cube_add

add_cube(location=(0,0,5))
ob1 = bpy.context.object
ob1.scale = (1, 1, 5)


for i in range(80):
    x = 3 * random.random() - 1.5
    y = 3 * random.random() - 1.5
    z = 10 * random.random()
    rotX = 1.51 * random.random()
    rotY = 1.51 * random.random()
    rotZ = 1.51 * random.random()
    add_cube(location=(x, y, z), rotation=(rotX, rotY, rotZ) )
    ob = bpy.context.object
    ob.scale = (0.5, 0.5, 0.5)    
    bpy.context.scene.objects.active = ob1
    bpy.ops.object.modifier_add(type='BOOLEAN')
    boo = ob1.modifiers[0]
    boo.object = ob
    boo.operation = 'DIFFERENCE'
    bpy.ops.object.modifier_apply(modifier=boo.name)
    scn.objects.unlink(ob)