2
$\begingroup$

I have a csv file with xy coordinates and an additional column that I want use as color information and set as a color ramp. Below is an example of the data I have:

enter image description here

Edit: if the color ramp was to go from red (highest number) to blue (lowest number) then I want the spheres with counts equal to 20 to be red and the sphere with count equal to 1 to be blue.

This is really close to what I want to do: How do I import XYZ AND a fourth parameter that adds color to my objects?, but I'm not familiar enough with python to get all the way to the end.

This is what I have so far, any help would be appreciated!

with open("example.csv") as f:
    reader = csv.reader(f)
    for i, row in enumerate(reader):
        if i==0: continue
        x, y = row[0:2]
    
        bpy.ops.mesh.primitive_uv_sphere_add( location = ( float(x), float(y), 0 ) )
    
$\endgroup$
2
  • $\begingroup$ I'm a little confused. Do you want a colorramp node with colors set at various positions or do you want to set vertex colors on vertices at specific locations or something else altogether? $\endgroup$ Jan 28 at 18:24
  • $\begingroup$ My goal is for the numbers in the color column to be mapped to a color ramp gradient. For example, if the color ramp was to go from red (highest number) to blue (lowest number) then I want the spheres with counts equal to 20 to be red and the sphere with count equal to 1 to be blue. $\endgroup$
    – Jasmine
    Jan 28 at 19:07

1 Answer 1

6
$\begingroup$

There are several ways you could do it. Here's one:

First create a material all the spheres will use. The nodes should look like:

  • The attribute node reads a custom property on the object named color, which should be a number between 0 and 1.
  • The color ramp is your desired ramp of course.
  • Make note of the name because you'll need it in the script. I called it Ramp Material.

Then a slight modification of your script will work

import bpy, csv
with open("example.csv") as f:
    reader = csv.reader(f)
    for i, row in enumerate(reader):
        if i==0: continue
        x, y = row[0:2]
        bpy.ops.mesh.primitive_uv_sphere_add( location = ( float(x), float(y), 0 ) )

        # This sets the material to use on the sphere you just created
        material = bpy.data.materials["Ramp Material"]
        bpy.context.object.data.materials.append(material)

        # This creates the color custom property (assumes range 0-20)
        bpy.context.object["color"] = float(row[2]) / 20
$\endgroup$

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.