How do I import XYZ AND a fourth parameter that adds color to my objects? I am a biologist, and I have generated spheres in Blender at designated XYZ coordinates, and now I want to add color to each of those spheres. The spheres represent cells in a living organism, and I want to add color to indicate the differences between those cells. 

I generated a CSV file that has 60 rows and four columns: X, Y, Z, and Q. The XYZ coordinates were imported into blender to generate 60 different spheres at each of the XYZ coordinates using a Python script. However, now I would like to add color to each of those spheres in a manner that is dependent on the Q value.


Shown here are the 60 different spheres located at their respective XYZ coordinates in Blender. The spheres are all colored gray.


This is an image of the lookup table that I want to apply. In other words, spheres with low Q values should be dark blue while spheres with large Q values should be red.

The operator I've used to generate the spheres in Blender is: 

bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, radius=0.5, calc_uvs=True, enter_editmode=False, align=("WORLD"), location=(' + xyz + '), rotation=(0,0,0))
Create a material that applies your color ramp depending on the vertex color. That way we dont need to create a separate material for each sphere and can easily manipulate the color ramp in one material and all spheres will change accordingly.



Create the spheres and set the color of all there vertices to your Q-Value

import bpy
import random

mat = bpy.data.materials['Material'] # The name of the Material you created
# create sphere 
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, radius=1, calc_uvs=True, enter_editmode=False, align='WORLD', location=(0, 0, 0), rotation=(0, 0, 0))

obj = bpy.context.active_object
# apply material
obj.data.materials.append(mat)

mesh = obj.data

# create vertex color map
mesh.vertex_colors.new()

color_layer = mesh.vertex_colors["Col"]

# color all vertecys 
q = random.random() # your Value here
color = [q,q,q,1] 
i = 0
for poly in mesh.polygons:
    for idx in poly.loop_indices:
        rgb = color
        color_layer.data[i].color = rgb
        i += 1


Set your viewport mode to shaded, to actually see the color that's applied by the shader.



If you want to be realy fancy you could export your data as an OBJ file (they are very similar to CSV) with vertex colors. That way you wouldn't even depend on Blender.

GIF of whole process

Run script


Create Shader