# Coloring bevel curve with vertex color

I'm working on a visualization problem.

I'm given a curve with a list of vertices. There is a color assigned to each vertex.

My question is "how do I generate the bevel curve by linearly interpolating the vertex colors?" Any ideas? It seems like I need to write some python scripts to achieve this.

Thanks!

• As far as I know bezier curve objects can't hold vertex color information, you will likely need to convert to a mesh object eventually Commented Sep 16, 2018 at 1:23

With a Python script you can populate a Color Ramp in the material according to the color specification. With the curve's UV coordinates you can color the bevelled curve with the correct color.

I start with a manually created Bezier curve with its bevel object set and the material assigned, as well as the node tree for the material. It's also possible to create the whole curve object in Python, just as I think it's possible to set up the node tree.

In the Data tab of the bezier curve under Texture Space activate “Use UV for mapping”.

I supply the coordinates of the curve's points and their color values in a csv file called point_data.csv within Blender. If your files are stored in another format or in an external file, you'll have to modify the code accordingly. Perhaps you might also want to supply the handle positons instead of using auto-handles.

x, y, z, r, g, b, a
-1, -1, 0, 1, 0, 0, 1
-2, 0, 0, 0, 0, 1, 1
-1, 1, 0, 1, 0, 0, 1
2, 0, 0, 1, 0, 0, 0


This is the script that was used to generate the image above. The coordinate grid was added manually to check if the color positions are correct.

import bpy
import csv
from itertools import tee
from mathutils import Vector

material = bpy.data.materials['Material']
ramp = material.node_tree.nodes['ColorRamp'].color_ramp
# Clear the color ramp of all elements but the first
# which can't be removed
for e in reversed(ramp.elements[1:]):
ramp.elements.remove(e)

# Modify an existing curve object instead of creating new one
curve = bpy.data.objects['BezierCurve']
curve.data.splines.remove(curve.data.splines[0])
spline = curve.data.splines.new('BEZIER')

# Read color info from internal csv file.
# Could also load from external file.
point_data = bpy.data.texts['point_data.csv']
lines = (line.body for line in point_data.lines)
line_count = sum(1 for line in line_counter)
vals = {k:float(v) for k,v in line.items()}

ramp_position = i / (line_count-1)

if i==0:
ramp_element = ramp.elements[0]
ramp_element.position = 0
else:
ramp_element = ramp.elements.new(ramp_position)