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:
- A plane and a cube,
- As a result of the previous operation becomes a Boolean operation with a ball.
- With the result of the operation 1 make a Boolean operation with a cone
- 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()

bpy.context.view_layer.objects.active = objto select the first operand of the modifier (cubein this case), thenbpy.ops.object.modifier_apply(modifier=modifier_name). Selecting the object before applying the modifier is necessary for it to work. (Blender 2.82a) – Andrew Che Oct 16 '21 at 18:59