4
$\begingroup$

How can I improve this code, so that it works like this: I want a script to automate some steps of Boolean operations intersection to be applied in the following objects:

  1. A plane and a cube,
  2. As a result of the previous operation becomes a Boolean operation with a ball.
  3. With the result of the operation 1 make a Boolean operation with a cone
  4. With the result of the operation 1 make a Boolean operation with a torus Remembering that all these objects have merged area with the plane of the operation 1, which thus becomes possible to do boolean operations in this walkthrough.

The part that is commented is where I could not continue because he was unable to do boolean operations, The code is below:

#Note To run this code in the right way the cube should be selected 
#on the scene and, only then it should run the code.

import bpy  
Object = "Plane"
  def ChangeLayer():
  bpy.context.scene.layers[0] = True
  bpy.ops.object.select_by_layer()
  bpy.ops.object.duplicate_move()
  bpy.ops.object.move_to_layer(layers=(False, False, False, False, False,True, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

if Object== "Plane":
  # Apply normal (Ctrl+N) for no result in error on the boolean
  bpy.ops.object.editmode_toggle()
  bpy.ops.mesh.normals_make_consistent(inside=False)
  bpy.ops.object.editmode_toggle()


  # Here can to apply the boolean to object
  bpy.ops.object.modifier_add(type='BOOLEAN')
  bpy.context.object.modifiers["Boolean"].operation = 'INTERSECT'
  bpy.context.object.modifiers["Boolean"].object =    bpy.data.objects["Plane"]
  bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")
  bpy.ops.object.editmode_toggle()

  # Here apply the material, wif previously setted to objetc, 
  #must to have tree different materials
  bpy.context.object.active_material_index = 0             
  bpy.ops.object.material_slot_assign()           
  bpy.ops.object.editmode_toggle()
  Object = "Cone"
  ChangeLayer
if Object == "Cone":
  bpy.ops.object.editmode_toggle()
  bpy.ops.mesh.normals_make_consistent(inside=False)
  bpy.ops.object.editmode_toggle()

  bpy.ops.object.duplicate_move()
  bpy.ops.object.move_to_layer(layers=(False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

  bpy.ops.object.modifier_add(type='BOOLEAN')
  bpy.context.object.modifiers["Boolean"].operation = 'INTERSECT'
  bpy.context.object.modifiers["Boolean"].object = bpy.data.objects["Cone"]
  bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")
  bpy.ops.object.editmode_toggle()
  #To apply the secont material into to object
  bpy.context.object.active_material_index = 1             
  bpy.ops.object.material_slot_assign()           
  bpy.ops.object.editmode_toggle()
  bpy.context.scene.layers[5] = True
  bpy.ops.object.select_by_layer(match='EXACT', extend=False, layers=5)
 Object = "Sphere"
 #ChangeLayer
 #if Object == "Sphere":
 #    bpy.ops.object.editmode_toggle()
 #    bpy.ops.mesh.normals_make_consistent(inside=False)
 #    bpy.ops.object.editmode_toggle()

 #    bpy.ops.object.modifier_add(type='BOOLEAN')
 #    bpy.context.object.modifiers["Boolean"].operation = 'INTERSECT'
 #    bpy.context.object.modifiers["Boolean"].object =   bpy.data.objects["Sphere"]
 #    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")
 #    bpy.ops.object.editmode_toggle()

 #    #Here to apply the third material to the object
 #    bpy.context.object.active_material_index = 2
 #    bpy.ops.object.material_slot_assign()
 #    bpy.ops.object.editmode_toggle()
 #Object = "Torus"
 #ChangeLayer   
 #if Object == "Torus":
 #    bpy.ops.object.editmode_toggle()
 #    bpy.ops.mesh.normals_make_consistent(inside=False)
 #    bpy.ops.object.editmode_toggle()

 #    bpy.ops.object.modifier_add(type='BOOLEAN')
 #    bpy.context.object.modifiers["Boolean"].operation = 'INTERSECT'
 #    bpy.context.object.modifiers["Boolean"].object = bpy.data.objects["Torus"]
 #    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")
 #    bpy.ops.object.editmode_toggle()
 #    #To apply the material into to object
 #    bpy.context.object.active_material_index = 0             
 #    bpy.ops.object.material_slot_assign()           
 #    bpy.ops.object.editmode_toggle()
$\endgroup$
0
11
$\begingroup$

run this from an empty .blend file

import bpy

bpy.ops.mesh.primitive_plane_add(radius=1.2, view_align=False, enter_editmode=False, location=(0, 0, 0))
bpy.ops.mesh.primitive_cube_add(view_align=False, enter_editmode=False, location=(0, 0, 0))
bpy.ops.mesh.primitive_cone_add(view_align=False, enter_editmode=False, location=(0, 0, 0))
bpy.ops.mesh.primitive_torus_add(rotation=(0, 0, 0), view_align=False, location=(0, 0, 0))


objects = bpy.data.objects

cube = objects['Cube']
plane = objects['Plane']  # must be larger than the cube
torus = objects['Torus']
cone = objects['Cone']

bool_one = cube.modifiers.new(type="BOOLEAN", name="bool 1")
bool_one.object = plane
bool_one.operation = 'DIFFERENCE'
plane.hide = True

bool_two = cube.modifiers.new(type="BOOLEAN", name="bool 2")
bool_two.object = torus
bool_two.operation = 'DIFFERENCE'
torus.hide = True

bool_three = cube.modifiers.new(type="BOOLEAN", name="bool 3")
bool_three.object = cone
bool_three.operation = 'DIFFERENCE'
cone.hide = True

enter image description here

$\endgroup$
1
  • $\begingroup$ Note that this code doesn't actually apply the modifiers. For that, you need bpy.context.view_layer.objects.active = obj to select the first operand of the modifier (cube in this case), then bpy.ops.object.modifier_apply(modifier=modifier_name). Selecting the object before applying the modifier is necessary for it to work. (Blender 2.82a) $\endgroup$
    – Andrew Che
    Oct 16 at 18:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.