1

I'm using Blender 2.76 on a Win 7 x64 machine.

I should warn you I'm new to Blender & its Python API although I have used Python extensively before I'm no expert.

I run the following script after start up (ie the default cube is already in the 3D work space).

import bpy

cube1 = bpy.context.active_object
bpy.ops.transform.resize(value=(2.0, 2.0, 0.9))
bpy.ops.object.modifier_add(type = 'BOOLEAN')
bpy.ops.mesh.primitive_cube_add(location=(0.0, 0.0, 0.0))
cube2 = bpy.context.active_object
cube1.modifiers[0].object = cube2
cube1.modifiers[0].operation = 'DIFFERENCE'
bpy.ops.object.modifier_apply(apply_as='DATA',modifier=cube1.modifiers[0].name)

My aim is to create a square hole through cube1 but so far I end up with no change to anything.

enter image description here

I tried swapping cube1 for cube2 & this cut cube2 with cube1.

import bpy

cube1 = bpy.context.active_object
bpy.ops.transform.resize(value=(2.0, 2.0, 0.9))
bpy.ops.mesh.primitive_cube_add(location=(0.0, 0.0, 0.0))
cube2 = bpy.context.active_object
bpy.ops.object.modifier_add(type = 'BOOLEAN')
cube2.modifiers[0].object = cube1
cube2.modifiers[0].operation = 'DIFFERENCE'
bpy.ops.object.modifier_apply(apply_as='DATA',modifier=cube2.modifiers[0].name)

enter image description here

So what am I missing here?

EDIT:

I tried to flip the normals of cube2 (see code below) as per the comment below but this appears to have had no effect.

import bpy, bmesh

cube1 = bpy.context.active_object
bpy.ops.transform.resize(value=(2.0, 2.0, 0.9))
bpy.ops.object.modifier_add(type = 'BOOLEAN')
bpy.ops.mesh.primitive_cube_add(location=(0.0, 0.0, 0.0))
cube2 = bpy.context.active_object

b_cube2 = bmesh.new()
b_cube2.from_mesh(cube2.data)

bpy.ops.object.mode_set(mode='EDIT')
for v in b_cube2.verts:
    v.select = True
bpy.ops.mesh.flip_normals()
bpy.ops.object.mode_set(mode='OBJECT')

b_cube2.to_mesh(cube2.data)
cube1.modifiers[0].object = cube2
cube1.modifiers[0].operation = 'DIFFERENCE'
bpy.ops.object.modifier_apply(apply_as='DATA',modifier=cube1.modifiers[0].name)
DrBwts
  • 113
  • 6
  • The darker, smaller cube in your scene probably has reversed normals. Fix it by selecting all verts in edit mode and pressing Ctrl + N. This confuses the boolean modifier and might result with a different operation than expected. – TLousky Mar 03 '16 at 15:42
  • I just attempted to flip the normals (see EDIT in OP) but this seems to have had no effect. – DrBwts Mar 03 '16 at 16:26

1 Answers1

2

Setting the context object to cube1 with context.scene.objects.active = cube1 or context.view_layer.objects.active in 2.8, before running the apply modifier fixes it. Object operators run on the context.active_object, which was cube2.

import bpy
context = bpy.context 
scene = context.scene
cube1 = context.active_object
bpy.ops.transform.resize(value=(2.0, 2.0, 0.9))
bpy.ops.object.modifier_add(type = 'BOOLEAN')
bpy.ops.mesh.primitive_cube_add(location=(0.0, 0.0, 0.0))
cube2 = context.active_object
mod = cube1.modifiers[-1] # last one added
mod.object = cube2
mod.operation = 'DIFFERENCE'

#scene.objects.active = cube1 2.79 context.view_layer.objects.active = cube1 # 2.8

bpy.ops.object.modifier_apply(modifier=mod.name)

A less operator-centric approach

import bpy
context = bpy.context 
scene = context.scene

cube1 = context.active_object cube2 = cube1.copy()

create a copy of mesh data, for bool mod

cube2.data = cube1.data.copy() cube1.scale = (2.0, 2.0, 0.9) mod = cube1.modifiers.new("SomeName", type='BOOLEAN') mod.operation = 'DIFFERENCE'

uncomment below to have cube2 in scene

#scene.collection.objects.link(cube2) 2.8 #scene.objects.link(cube2) mod.object = cube2 bpy.ops.object.modifier_apply(modifier=mod.name)

The modifier apply operator can also be replaced with, in < 2.7x

scene.update()
cube1.data = cube1.to_mesh(scene, True, 'PREVIEW')
cube1.modifiers.remove(mod)

or for 2.8

How do I get a mesh data-block with modifiers and shape keys applied in Blender 2.8?

batFINGER
  • 84,216
  • 10
  • 108
  • 233