2
$\begingroup$

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. 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. 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))
$\endgroup$
  • 2
    $\begingroup$ Are the spheres to be a single color from your color ramp, or should the color change across the sphere? $\endgroup$ – rcgauer Apr 6 at 12:57
  • $\begingroup$ @rcgauer The spheres will be a single color from my color ramp. $\endgroup$ – Espe Apr 6 at 13:55
  • $\begingroup$ this might be of intrest to your project? archive.blender.org/wiki/index.php/Extensions:2.6/Py/Scripts/… $\endgroup$ – J.Doe Apr 12 at 11:21
2
$\begingroup$

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.

enter image description here

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.

enter image description here

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

enter image description here Create Shader

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ I don't see the Vertex Color Node/Window in my version of Blender. I'm using Blender 2.80.75. I used shift+A and searched for the term "vertex", but nothing showed up. I'm going to work on this some more. Thank you for this valuable information. $\endgroup$ – Espe Apr 7 at 10:57
  • $\begingroup$ i added gifs so you can follow easily. Please accept the answer if it helped you out so the question gets closed. $\endgroup$ – J.Doe Apr 7 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.