4
$\begingroup$

Background: We work with scientific measurements of the brain. After all the data processing is done, we have vertices of the brain surface (and surface faces) and for each vertex, there are 1..n measurements. Assume that each measurement is a float value.

I wrote a small add-on; mostly stealing code from the STL import to transform our data into a Blender mesh which works nicely:

enter image description here

Now, I can't solve how I can attach an attribute to each vertex of the mesh that I can then use in the Attribute node of the Material to create, e.g. a heat-map of the measurement.

enter image description here

I'm not even sure if this is possible, but what I'd like to have is that I can access the measurement value for each vertex. In the past, I used a workaround and used the vertex color to encode the measurement, but this is not optimal.

Would someone know a solution? I attach the creation of the script that creates the mesh, where you can assume that measures is a list of floats with the same length as points (the vertices).

def create_and_link_mesh(name: str, faces: typing.List[tuple], points: typing.List[tuple], measures: typing.List):
    import bpy

    mesh = bpy.data.meshes.new(name)
    mesh.from_pydata(points, [], faces)

    mesh.validate(clean_customdata=False, verbose=True)
    mesh.calc_normals()
    mesh.use_auto_smooth = True
    mesh.update()

    obj = bpy.data.objects.new(name, mesh)
    bpy.context.collection.objects.link(obj)
    bpy.context.view_layer.objects.active = obj
    obj.select_set(True)
$\endgroup$

1 Answer 1

5
$\begingroup$

Do this after you create the verts (with from_pydata) but before you call mesh.update()

    # Create custom attribute storing one FLOAT for every POINT (vertex)
    attr = mesh.attributes.new("measure", 'FLOAT', 'POINT')
    # Set value at each vertex
    attr.data.foreach_set("value", measures)

Docs for attributes.new: link

In the Attribute shader node, put the name of your attribute (measure) and the value should come out of the "Fac" socket. You can also check your attribute's values in the spreadsheet.

$\endgroup$
1
  • 1
    $\begingroup$ Absolutely lovely. Especially the "you can also check the spreadsheet window"! I didn't know this existed and it's so helpful. Thank you very much. $\endgroup$
    – halirutan
    Commented Apr 24, 2022 at 13:59

You must log in to answer this question.

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