2
$\begingroup$

I'm working on a little project, that requires a large flat mesh object divided into separate faces, each face needs to be a different material as seen here:

sample

Creating and assigning these materials, setting colours so I can tell them apart, and renaming them is a slow a tedious process, I was wondering if there were any shortcuts that create a material and immediate assign it to the selected faces, or something similar that might speed this sort of process up?

$\endgroup$
5
  • $\begingroup$ While I'm not aware of any way to do it exactly like you described within UI there's a way to assign different faces to different vertex colors by performing Ctrl+E > Edge Split and then using the addon to assign different vertex colors which you can use as factor in the material shader. Note that this won't create multiple slots; most likely for that scripting is required. $\endgroup$
    – Mr Zak
    Commented Apr 15, 2017 at 14:08
  • $\begingroup$ Are they all similar, just a different shade? If so, you could script the process. ...well, you can also script the process with different shaders. It just happens to be easier to iterate if they are different colors of the same material. $\endgroup$ Commented Apr 15, 2017 at 14:36
  • 5
    $\begingroup$ Could you tell us why each face needs its own material, and what the end goal of all of this is? It may help the community to recommend a solution. Sometimes, what we think is the shortest path is not the shortest path. $\endgroup$
    – Mentalist
    Commented Apr 15, 2017 at 14:51
  • $\begingroup$ This is for a project in secondlife, when important into SL each material is recognised as a different face, and that face can be used in scripting as well as being able to change textures individually. I will be scripting this so that each face changes it texture depending on who walks on it etc. $\endgroup$
    – Freya
    Commented Apr 15, 2017 at 17:23
  • 2
    $\begingroup$ You can add a shortcut for the "assign material" operator by right clicking the assign button in the materials panel. But Blender cannot know which material you want for which face, so you still need to select the face, select the material and then you can save another click with this shortcut. The actual solution would be using a python script. $\endgroup$
    – Dimali
    Commented Apr 15, 2017 at 21:29

1 Answer 1

1
$\begingroup$

A new material can be created with bpy.data.materials.new(). You assign a material to a face by setting it's material_index, which is the material's index from it's location in object.material_slots, we need to use the length of material_slots to know the index of the material we just added to the list.

import bpy

obj = bpy.context.object
obj.data.materials.clear()

for f in obj.data.polygons:
    m = bpy.data.materials.new('FaceMat' + str(f.index))
    # for 2.79 remove the ,1.0 at the end
    m.diffuse_color = (0.01*f.index,0.02*f.index,0.03*f.index, 1.0)
    obj.data.materials.append(m)
    f.material_index = len(obj.data.materials)-1

This is a simple example that sets the diffuse colour used by BI which is also used by cycles for the viewport colour. To create a cycles material you will want to create nodes as well.

You could also do it in two parts, one loop to create materials and another to assign a material based on the material name. This way you could create 10 materials and distribute them across the object.

obj.data.materials.append(bpy.data.materials[mat_name])
$\endgroup$

You must log in to answer this question.

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