# Boolean operation with script produces error which requires keyboard interrupt

Cylinder 2 and Cylinder 5 are in the active screen. Manual Boolean Difference works. Following examples from tutorials and stackexchange, I've patched together the following script, which appears to mimic the manual operation:

import bpy
from bpy.types import Operator
from bpy.props import BoolProperty, StringProperty

#   need to spend more time on bpy: types, props,data,ops, context
xx = 15
while (xx < 16):
yy = 15
while (yy < 16):
#  set the 'cutter' in location
bpy.data.objects['Cylinder.002'].location=(xx,yy,0)

#  make sure that the base object is selected
bpy.data.objects['Cylinder.005'].select = False
bpy.data.objects['Cylinder.005'].select = True

#  set the modifier
bpy.context.object.modifiers['Boolean'].operation = 'DIFFERENCE'
bpy.context.object.modifiers['Boolean'].object = bpy.data.objects['Cylinder.002']

#  execute
bpy.ops.object.modifier_apply(apply_as='DATA', modifier='Boolean')
yy += 3
xx += 3


(not worrying about my inability to properly paste a code block for the moment)

Run Script produces the following screenshot after interrupting execution:

Aside from help with my immediate problem, would you suggest a particular tutorial on scripting? Thanks.

• (an aside, not an answer) Often in Python inline comments need to be on the same level of indentation as the code. in Blender's texteditor it doesn't really matter but it's useful to follow the convention/expected behavior. – zeffii Jun 16 '16 at 13:43
• You variable yy in the inner while loop never gets incremented within the loop, which means the while loop will never stop - until you use a keyboard interrupt. You might want to indent the yy += 3 so that it's within the loop. Same is true for the outer while loop and xx. Generally, I'd avoid using while loops for repetitive tasks that have a clear beginning and end, as in your case. – TLousky Jun 16 '16 at 14:23
• Tip on pasting code blocks from blender, select all, hit tab, then copy, the indented code will be formatted properly when pasted here. back in blender shift-tab (or ctrl-Z) to undo. – batFINGER Jun 16 '16 at 14:33

From TLousky:

(paraphrased) The variable yy is at the wrong indent level, so it never gets incremented. Thus your inner loop runs until you interrupt it.

Use a print statement within each loop to print out your counters and find out when it's failing.

I suspect that it's failing on the first loop because although bpy.data.objects['Cylinder.005'].select = True selects the object, it neither guarantees that no other objects are also selected, nor does it set the object to active.

Use bpy.context.scene.objects.active = bpy.data.objects["Cube"] to activate the object you want to work on (FYI it might not display correctly in the viewport, even when it's working correctly). Only one object can ever be 'active' at a time, so you can also look in bpy.context.scene.objects.active to find out what (if anything) is already active.

Also use bpy.context.selected_objects check to see if any other objects are selected, to make sure none of them are interfering with your boolean. (I believe there's a method that just deselects everything, so you don't have to write your own loop to set False on every object in the scene).

• Thanks to all of you who answered. I learned something from each of you. 1) How to paste a code block. 2) Be very precise with indentation (don't mix spaces and tabs). 3) One shouldn't try to re-apply a boolean to the same location: you will get messages telling you about ugly meshes. In other words, once the script was run, even though apparently unsuccessfully, the mesh may have already been cut. 4)The comments concerning active vs. selected may have saved me from my next error. – Hertfordkc Jun 16 '16 at 15:12