1
$\begingroup$

I'm trying to use a boolean modifier to difference two objects. This works, but what it creates is incomplete vs what happens in blender itself.

Here's relevant code:

def difference(self, source_object, modifier_object):
    ## select the object
    bpy.ops.object.select_all(action = 'DESELECT')
    bpy.context.scene.objects.active = source_object

    ## add the boolean modifier to the selected object and apply it.
    bpy.ops.object.modifier_add(type='BOOLEAN')
    bpy.context.object.modifiers["Boolean"].operation = 'DIFFERENCE'
    bpy.context.object.modifiers["Boolean"].object = modifier_object
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")
    modifier_object.hide = True ## hide the second object

If I call this with a cube and a sphere, the sphere is "cut" from the cube as expected, but there's a hole in the cube instead of a complete surface. What am I doing wrong? I've looked at a few different answers, for example using object.modifier.new -- but none have worked so far.
Cut, instead of remove

$\endgroup$
  • $\begingroup$ Usually this kind of result is caused by a bad geometry in the modifier object. How do you generate the sphere? Where does your modifier_object come from? $\endgroup$ – Pyros Jul 24 '17 at 13:37
  • $\begingroup$ It's not exactly this -- but here's the gist in python: bpy.ops.mesh.primitive_uv_sphere_add(size=2, location=(2,2,2)) sphere_ref = bpy.context.active_object modifier_object = sphere_ref Full script is here: github.com/ipeerbhai/Blender101/blob/master/Tutorials/… $\endgroup$ – Imran Jul 24 '17 at 23:23
0
$\begingroup$

Testing your code, it look to works fine with python 2.73, but i was able to reproduce your result with 2.78.

So, first of all, blender boolean are very bad, sometime they fail for no "good" reason caused by overlapping geometry, edges, holes,... In your case the sphere has a ring exactly aligned with the cube face.

A general solution would be so move slightly the sphere, let say positionning it in 2.001 would be enough.

Another solution for Blender 2.78, the boolean now have two solvers, bmesh and Carve. Using carve seems to work better

bpy.context.object.modifiers["Boolean"].solver = 'CARVE'
$\endgroup$
  • $\begingroup$ Thanks for the suggestion! I just found that blender crashes when I try 'CARVE' -- I think the entire issue might be a blender bug. $\endgroup$ – Imran Jul 26 '17 at 2:16
  • $\begingroup$ I just tried on a different computer -- the suggestion worked without a crash. $\endgroup$ – Imran Jul 26 '17 at 2:23
  • $\begingroup$ It can depend of the blender version you are using. Try to install the last version on the first computer. Is your problem solved? $\endgroup$ – Pyros Jul 26 '17 at 7:05
  • $\begingroup$ They're both running the latest, but the OS is different between them. One is Windows 8.1, the other is windows 10. I'll consider this solved as, "blender bug", and figure out how to file a bug report. In any case -- I just want to thank everyone. I would have barked up the wrong tree forever without the help! $\endgroup$ – Imran Jul 28 '17 at 12:43
  • $\begingroup$ You can accept my answer to flag this question as solved if you don't need more help on this topic right now ;) $\endgroup$ – Pyros Jul 31 '17 at 7:37

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.