1

How to assign a material to multiple faces with python?
As shown in the following image,
there is a cube,
I subdivide one big face into 49 small faces,
then I assign the green material to the 49 small faces with mouse.
enter image description here

Question:
I need to assign the green material to the 49 small faces with python API,how to do it?

zwl1619
  • 137
  • 5

1 Answers1

0

First assign a material to the object:

How to assign a new material to an object in the scene from Python?

Then loop through the polygons and make a check for each if it satisfies your criteria for having the material changed - it seems you want to check face area.

from bpy import context as C
for poly in C.object.data.polygons:
    if poly.area < 1:
        print(poly.index)

Then instead of printing polygon index, assign the material index of the desired material:

How to assign a material to a single face of a mesh using Python API

Markus von Broady
  • 36,563
  • 3
  • 30
  • 99
  • If the material is an image, how to do it? – zwl1619 Mar 23 '23 at 08:06
  • @zwl1619 an image is not a material. If you want, you can create a material and add an image texture node to it, and then load an image (unless it's already loaded in Blender) and assign it to the image texture node. Pretty sure there are already answers how to do that. – Markus von Broady Mar 23 '23 at 09:11