0
$\begingroup$

I'm a newbie in blender and I'm trying to play around with the boolean difference modifier.

I have two concentric disks and I want to perform the boolean differece between them. I'm using the python API.

Here is an example:

import bpy
import bmesh

c1mesh = bpy.data.meshes.new("Disk1")
c1object = bpy.data.objects.new("Disk1", c1mesh)
bpy.context.scene.objects.link(c1object)
bm = bmesh.new()
bm.from_mesh(c1mesh)
bmesh.ops.create_circle(bm, cap_ends=True, diameter=2, segments=128)
bm.to_mesh(c1mesh)
bm.free()

c2mesh = bpy.data.meshes.new("Disk2")
c2object = bpy.data.objects.new("Disk2", c2mesh)
bpy.context.scene.objects.link(c2object)
bm = bmesh.new()
bm.from_mesh(c2mesh)
bmesh.ops.create_circle(bm, cap_ends=True, diameter=1, segments=128)  
bm.to_mesh(c2mesh)
bm.free()


bool = c1object.modifiers.new(type='BOOLEAN', name = "diff1")
bool.operation = 'DIFFERENCE'
bool.object = c2object
c2object.hide = True

This has no effect (besides hiding the second disk). When I try to apply the modifier from the gui I get no result (besides some weird lines over the disk 1).

I guess that the problem is just my poor initial knowledge of blender (and general 3D modelling).

Thanks in advance.

$\endgroup$

1 Answer 1

3
$\begingroup$

Hope this helps:

import bpy

# initialize the variables to work with, in this case two selected objects, in yours c1mesh and c2mesh
first_object = bpy.context.selected_objects[0]
second_object = bpy.context.selected_objects[1]

# add the modifier and change settings
first_object.modifiers.new(name = "Boolean", type = 'BOOLEAN')
first_object.modifiers.get("Boolean").operation = "DIFFERENCE"
first_object.modifiers.get("Boolean").object = second_object

# just cosmetic hiding of second object so you can instantly see the result
second_object.hide_viewport = True
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .