# Subtracting an object and then deleting the object used to subtract within python script

I am currently trying to create an object within python in order to subtract from another object. I did this using the GUI with no problem but when I try to use the same code within the script it doesn't seem to work.

import bpy

#Large Cube

#Small Cube

bpy.context.object.modifiers["Boolean"].operation = 'DIFFERENCE'
bpy.context.object.modifiers["Boolean"].object =
bpy.data.objects["Cube.001"]
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")
bpy.ops.object.delete(use_global=False)


How can I select the Large Cube in order to use the boolean operation and then delete the Small Cube?

• You shouldn't be doing this with operators (bpy.ops) anyway... – JakeD Mar 27 '17 at 23:46
• Is there a better way to do it? I just want to know how to subtract an object in python and then delete the object used to subtract. – Austin Ellis Mar 27 '17 at 23:59
• batFinger answered very well, and that's exactly what I was referring to. I'm sure there's a few hundred questions here about not using bpy.ops, though... – JakeD Mar 28 '17 at 1:59

Can add modifiers with obj.modifiers.new(name, type). When a primitive is added it is the context object. Changing the order made the large_cube context object, otherwise it can be set with context.scene.objects.active = large_cube.

import bpy

context = bpy.context

#Small Cube
small_cube = context.object

#Large Cube
large_cube = context.object

mod = large_cube.modifiers.new("Boolean", type='BOOLEAN')
mod.operation = 'DIFFERENCE'
mod.object = small_cube

# large cube has context.
bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod.name)