Motivation: Post-process a finite element solver answer file (in my case FEMM).

I'm creating a mesh with Python with the following code (well, my actual code is a bit more complex, but it doesn't matter).

import bpy

mesh = bpy.data.meshes.new("NewMesh")  # add the new mesh
obj = bpy.data.objects.new(mesh.name, mesh)
col = bpy.data.collections.get("Collection")
bpy.context.view_layer.objects.active = obj

verts = [(0, 0, 0),
         (1,  1,  0), 
         (1, -1,  0),
         (-1, -1,  0),
         (-1, 1,  0),

edges = []

faces = [[0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 4, 1]]

face_values = [-1.5, 10, -5, 3]

mesh.from_pydata(verts, edges, faces)

With Python (I have 10000+ faces to colorize), how can I affect a separate color to each face depending on the corresponding value in face_values (based on a color map)?

I'd like to achieve something like that (except that all faces should have a uniform color).

Minh, N & Mot, Tran & Ban, P & Nguyen, Duc. (2016). Experimental investigation on the vibration characteristics of sandwich composite beam and panel under harmonic load using accelerometers and displacement sensors.

Here is an example that I faked using texture painting:

enter image description here

with [-1.5, 10, -5, 3] corresponding to [red, blue, green, white].

  • 1
    $\begingroup$ Have a look into vertex colors: blender.stackexchange.com/a/60730/31447 $\endgroup$
    – brockmann
    May 12, 2020 at 16:51
  • $\begingroup$ @brockmann Thanks for your comment. As far as I understand, with vertex colors, face colors won't be uniform on each face. Then, if I'm not mistaken, this is not what I'm looking for (I must admit that the image in my question does not properly show this requirement). Please correct me if I'm wrong. $\endgroup$
    – cjorssen
    May 13, 2020 at 8:02
  • $\begingroup$ Sure. You can assign the same color to each vertex of a triangle/quad which makes it a "uniform color" per face component. Does that answer your q? $\endgroup$
    – brockmann
    May 13, 2020 at 8:20
  • $\begingroup$ So a given vertex can have multiple colors corresponding to the different faces it belongs to? $\endgroup$
    – cjorssen
    May 13, 2020 at 9:08
  • $\begingroup$ Nope, only one value. However your can blend them of course, see blender.stackexchange.com/a/911/31447 $\endgroup$
    – brockmann
    May 13, 2020 at 9:11

1 Answer 1


for those stuck on this struggle, this is the code to add arbitrary point centered OR cell centered data to a blender mesh. This is valid for Blender 3.3+ that I've tested, maybe more.

this allows you to store the actual data (floats, float vectors, ints, strings), and then use shader nodes to color it later.

assuming you have an object in your scene called 'Object':

#the object    
newobj = bpy.data.objects['Object']
#convert to bmesh
bm = bmesh.new()
#these will be out of date, update so you can index
#you only need to update the one you need
#add point data
pkey = bm.verts.layers.float.new('pointdata')
for j,v in enumerate(bm.verts):
    v[pkey] = np.random.random() #the value for the point
#add cell data
ckey = bm.faces.layers.float.new('celldata')
for j,f in enumerate(bm.faces):
    f[ckey] = np.random.random() #the value for the face
#return data to blender

you can now see these in your mesh attributes when you click on the object and can set up some simple shader nodes to color by point/cell data:

enter image description here

not a glamorous example by any means, but you get the idea


You must log in to answer this question.

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