1
$\begingroup$

I try remove polygon by material_index. Re-assign polygons errors

    new_polys = []

    for polys in me.polygons:
        if polys.material_index == 1:
            new_polys.append(polys)

    me.polygons = new_polys

AttributeError: bpy_struct: attribute "polygons" from "Mesh" is read-only

Any ideas?

$\endgroup$
3
  • $\begingroup$ Does it have to be in python? I understand if you need to automate it because you have lots of objects or lots of materials. $\endgroup$
    – Gunslinger
    Aug 5, 2023 at 10:49
  • $\begingroup$ yes of course, I need it in python. I have about 4k objects in my scene, it's impossible to do it manually. $\endgroup$
    – minus_dva
    Aug 5, 2023 at 11:05
  • 1
    $\begingroup$ it's not clear what you are trying to do but based on your first sentence phrase "remove polygons by material_index" it sounds like you want a script that removes all faces that have material index = 1. so i'm not sure why you are trying to assign those faces you want to remove back to the mesh. it's non sensical :D $\endgroup$ Aug 6, 2023 at 9:36

1 Answer 1

1
$\begingroup$

To remove all polygons or faces with material_index = 1, Select all objects you want and run the script:

import bpy

material_index = 1

bpy.ops.object.mode_set(mode='OBJECT')

for obj in bpy.context.selected_objects:
    for face in obj.data.polygons:
        face.select = (face.material_index == material_index)

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.delete(type='FACE')
$\endgroup$

You must log in to answer this question.

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