3

I have tried the following but the mat is not assigned to any of the faces.

import bpy
import bmesh

Creating a new Mat

mat = bpy.data.materials.new(name="newMat") mat.diffuse_color = (0.5,0,0.5,1)

Generating a Box

bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=True, location=(0, 0, 0))

Resizing and Translating the box

bpy.ops.mesh.select_all(action='SELECT') bpy.ops.transform.resize(value=(0.5, 1, 1)) bpy.ops.transform.translate(value=(0.5, 0, 0)) bpy.ops.mesh.select_all(action='DESELECT')

Instantiating the bmesh of the Box

obj = bpy.context.edit_object me = obj.data bm = bmesh.from_edit_mesh(me)

Selecting just one of the faces

bm.faces[0].select = True

toggle to edit mode

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

make sure face select mode is enabled

bpy.context.tool_settings.mesh_select_mode = [False, False, True]

use second material slot

ob = bpy.context.active_object ob.active_material_index = 1

assign the material

bpy.ops.object.material_slot_assign()

toggle to object mode

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

What am I doing wrong, or how else could I achieve this?

  • 1
    @MartyFouts hi, thanks for shareing. Actually I tried that answer, I should have mentioned in the question that most of my code have been taken from the accepted answer, but in my case the mat is not being assigned. That is why I posted my version – Ignacio Alorre Oct 18 '21 at 05:53
  • 1
    No problem. In the future, you should mention the specific answers you have tried and why they didn't work for you. The answer from the question should work, but the code you shared is missing some of the steps. Someone has already posted another way of doing it. If you want to see your code fixed, let me know and I'll post an answer with working bmesh-based code. – Marty Fouts Oct 18 '21 at 14:52

2 Answers2

7

If you'd like to assign material(s) to certain faces in Edit Mode, just assign the index of the material slot to BMFace.material_index on a Bmesh representation of your mesh:

enter image description here

import bpy, bmesh

def create_material(mat_name, diffuse_color=(1,1,1,1)): mat = bpy.data.materials.new(name=mat_name) mat.diffuse_color = diffuse_color return mat

Generate 2 demo materials

mat_red = create_material("Red", (1,0,0,1)) mat_green = create_material("Green", (0,1,0,1))

Generate a Box

bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=True, location=(0, 0, 0))

Resize and Translate the box ...

Get a reference to the object in context

obj = bpy.context.object

Append both Materials to the created object

obj.data.materials.append(mat_red) obj.data.materials.append(mat_green)

Bmesh representation

me = obj.data bm = bmesh.from_edit_mesh(me) bm.faces.ensure_lookup_table()

Assign the green Material to the second polygon

bm.faces[1].material_index = 1

Assign the green Material to the 4th polygon

bm.faces[3].material_index = 1

Optional

#bmesh.update_edit_mesh(me) #bpy.ops.object.mode_set(mode='OBJECT')

You might want to create the mesh using Bmesh in this case as well: how to use BMesh to add verts, faces, and edges to existing geometry

brockmann
  • 12,613
  • 4
  • 50
  • 93
6

You can assign the materials using MeshPolygon.material_index in Object Mode without Bmesh.

enter image description here

import bpy

def create_material(mat_name, diffuse_color=(1,1,1,1)): mat = bpy.data.materials.new(name=mat_name) mat.diffuse_color = diffuse_color return mat

Generate 2 demo materials

mat_red = create_material("Red", (1,0,0,1)) mat_green = create_material("Green", (0,1,0,1))

Generating a Box

bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, 0))

Resizing and Translating the box ...

Get a reference to the object in context

obj = bpy.context.object

Append both Materials to the created object

obj.data.materials.append(mat_red) obj.data.materials.append(mat_green)

Assign the green Material to the second polygon

obj.data.polygons[1].material_index = 1

Assign the green Material to the 4th polygon

obj.data.polygons[3].material_index = 1

Related:

brockmann
  • 12,613
  • 4
  • 50
  • 93