# How to map vertex to particle (hair); or color particles with specific colors

I am trying to render CFD data in blender, where the CFD code generates output files that consist of particle positions and scalars. I have successfully loaded the data into blender and used a particle system to place a sphere at each vertex. Next, I want to color each particle using a scalar (temperature, velocity, etc.). So I created a nparticles x 1 image where the colors are the same order as I added the vertices. I tried using the particle system index to map to that image and color the particles. This works except that something in the ordering of the particles is wrong.

This image should have a nice linear coloring from bottom to top based on the particles height, however it has bands of color:

Here is the material nodes for the duplicated object (sphere):

Any ideas on how I can map the vertex order to the particle order?

For creating the image:

def set_image_data(obj, data, cmap='viridis', limits=[0, 1]):
'''
Given an array of data, convert to colors using the color map, create an
image and asign the image to the 'Image Texture' node in the objects
material.
'''
node_tree = obj.data.materials[-1].node_tree

data = data.flatten()
npoints = len(data)

# create the image to color the objects
w, h = npoints, 1
image = bpy.data.images.new('ParticleColor', width=w, height=h)

# map data to colors
rgba = map_colors(data, 'viridis', limits)

# set the pixels
image.pixels = rgba.flatten()

# add image to Image Texture node
image_node = node_tree.nodes.get('Image Texture', None)
if image_node is not None:
image_node.image = image

# update divide node
math_node = node_tree.nodes.get('Math', None)
if math_node is not None:
math_node.inputs[1].default_value = npoints


For creating the particle system:

def make_hair(parent, child, npoints=100):
'''
Make a Hair particle system, inspired from:
'''
parent.modifiers.new("particle sys", type='PARTICLE_SYSTEM')
particle_sys = parent.particle_systems[-1]
settings = particle_sys.settings
settings.type = 'HAIR'

• This is just an example. In the real application I have an array of data (particle temperature) that I need to map to specific particles. So something like particle(50) has a temperature of 600C... – Onlyjus Oct 21 at 17:49
• Maybe 32bit depth will help when creating new image, float=True. Also uncheck "random" emission in particle system. – Serge L Oct 21 at 20:19
• I am using 2.79 (Latest that works on our machines) so I tired float_buffer=True when creating the image. That didn't seem to change anything. – Onlyjus Oct 21 at 21:03