1
$\begingroup$

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
bpy.ops.mesh.primitive_cube_add(radius=2, location=(0, 0, 0))

#Small Cube
bpy.ops.mesh.primitive_cube_add(radius=1, location=(0, -1.5, 0))

bpy.ops.object.modifier_add(type='BOOLEAN')
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?

$\endgroup$
  • $\begingroup$ You shouldn't be doing this with operators (bpy.ops) anyway... $\endgroup$ – JakeD Mar 27 '17 at 23:46
  • $\begingroup$ 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. $\endgroup$ – Austin Ellis Mar 27 '17 at 23:59
  • $\begingroup$ 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... $\endgroup$ – JakeD Mar 28 '17 at 1:59
1
$\begingroup$

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
bpy.ops.mesh.primitive_cube_add(radius=1, location=(0, -1.5, 0))
small_cube = context.object

#Large Cube
bpy.ops.mesh.primitive_cube_add(radius=2, location=(0, 0, 0))
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)

context.scene.objects.unlink(small_cube)
bpy.data.objects.remove(small_cube)
$\endgroup$
  • $\begingroup$ Wow that makes a lot more sense. The only problem I am getting now is I just copy and pasted that into blender but I came up with an error for the last line of code that says "Python script fail, look in the console for now..." I apologize for asking a second question but after toiling with it for a bit it was obvious I had no idea what I was doing. Mind helping one last time? $\endgroup$ – Austin Ellis Mar 28 '17 at 0:21
  • $\begingroup$ What version of blender are you using? Made an edit to unlink small_cube from scene before removing. Newer versions do this for you by default. $\endgroup$ – batFINGER Mar 28 '17 at 0:24
  • $\begingroup$ I am using version 2.76. $\endgroup$ – Austin Ellis Mar 28 '17 at 0:27
  • $\begingroup$ Just got it with the edit. Thank you very much $\endgroup$ – Austin Ellis Mar 28 '17 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.