1
$\begingroup$

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

Scripting Screenshot

Sys Console

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

$\endgroup$
  • 1
    $\begingroup$ (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. $\endgroup$ – zeffii Jun 16 '16 at 13:43
  • 1
    $\begingroup$ 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. $\endgroup$ – TLousky Jun 16 '16 at 14:23
  • $\begingroup$ 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. $\endgroup$ – batFINGER Jun 16 '16 at 14:33
0
$\begingroup$

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).

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Hertfordkc Jun 16 '16 at 15:12

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.