Set Material of Some Polygons After Boolean Operation

Hello,

I’m trying to write a Python script to perform some Boolean difference operations that put some cylindrical holes in an object. I’m able to perform the Boolean difference without any issues. However, I’m having trouble applying a material to the polygons that make up the hole in the parent object after the Boolean difference is performed.

Here are the steps to do what I want manually using the GUI:

  1. Perform the Boolean difference operation.
  2. With the parent object selected, enter Edit Mode…the hole polygons are already selected.
  3. On the Material tab, click the “+” icon to add a new material slot.
  4. Below that, select the material I want to assign to the slot. In this case, the material is called “hole-material”.
  5. Click the “Assign” button.
  6. Exit Edit Mode and go back to Object Mode.

I think I can do most of the above with Python, but I can’t seem to figure out how to assign the “hole-material” to the newly-created material slot. Here is my code thus far:

bpy.context.scene.objects.active = bpy.data.objects[“object-with-holes-in-it”]
bpy.ops.object.editmode_toggle()
bpy.ops.object.material_slot_add()
bpy.context.object.active_material_index = 1

Assign “hole-material” to the active material index

???

bpy.ops.object.editmode_toggle()

Would anyone be able to give me some ideas on how to do this via Python? I really appreciate whatever help you can provide.

Here are the steps to do what I want manually using the GUI:

  1. Perform the Boolean difference operation.
  2. With the parent object selected, enter Edit Mode…the hole polygons are already selected.
  3. On the Material tab, click the “+” icon to add a new material slot.
  4. Below that, select the material I want to assign to the slot. In this case, the material is called “hole-material”.
  5. Click the “Assign” button.
  6. Exit Edit Mode and go back to Object Mode.
    The automatic way to add the materials if you were using the boolean modifier would be.
    Add a new material slot to the object with the modifier and add the material being used by the cylinder. When you then use the boolean modifier the inside of the cut hole would then have the material of the cylinder applied.

thanks you for reply

Richard,

Many thanks for your help with this. You answered the question I should have been asking! I took your advice and it worked perfectly, without needing to use Python at all for the material assignment.