How to colour vertices of a beveled curve mesh without converting to mesh? For scientific visualisation, I'm using a lot of splines which are bevelled with an object (i.e. a hexagon). I control the level of details depending on the context.
Without losing the benefits of curves, I want to assign colours to the curve points so that the generated mesh will show these colours (with interpolation).
Here's an image of what I would like to achieve. The image below is an example of the object I want to map colours to. Some splines have hundreds of points. Each point has a colour so the mesh has to show the &quot;exact&quot; colour at the proper location.  
I browsed stack exchange for an answer and I also did try to answer my own question before posting it here, see below.
1. the naive way
I tried looping over the curve points but there's no colour property. Meh.
2. curve mesh data
So, this is about looping over generated mesh vertices and then calculating how close they are to a point in order to assign the proper vertex colour.
When I have a meshed curve in my viewport, I do a loop
for mesh in bpy.data.meshes:
    print(mesh)

And I get 0 as a result. But my mesh is there, I can see it in Blender.
3. weights to colours with Cycles
So far what I did was assigning the colour value to the weight property of curve points, like this simplified code:
    polyline.points.add(len(points) - 1)
    for i in range(len(points)):
        vertex = points[i]
        # x, y, z and weight
        polyline.points[i].co = (x, y, z, w)
        # radius of the point (only meaningful if a tube-like volume is drawn around it)
        polyline.points[i].radius = r

I thought there would be a way to then transfer the weight to vertex colouring of the curve mesh but I found none since I can't access the mesh data.
So I randomly tried to find a way to use weights with Cycles, maybe there is something that I can plug to the colour input of a standard material ? Didn't find any.
4. using BMesh ?
Bmesh doesn't support curves so it's a dead end here as well.
5. uvw mapping
As Chebhou wrote, I could use UVW mapping but I would like to see this in python. However, I'm asking for a way to match colours at every curve point. It seems that uvw mapping is non linear so how do I know to what coordinate in space (on the mesh) a color will be ?
UPDATE : there is a limit to how many gradient elements you can have in a color ramp, 32 colours max. I have splines with way more points than that. I need to find an alternative way of using UVW mapping.
Check use UV for mapping in the curve tab (this act as unwrapping the curve mesh ):



use the U input from the texture coordinates node to control the color using ColorRamp node along the curve :



render result 





UPDATE 
It seems that the UV space is divided equally between the bezier points , and using this info we can use the ColorRamp to assign a color to each point using knowing its order in the spline :


the number of elements in the color ramp should be the same as the number of bezier points
distribute the color ramp elements evenly
the color of each bezier point is the color of its corresponding element in the color ramp


this is a curve with 3 splines each spline has each own material :



using the previous idea gives the following result( color-ramp interpolation is set to constant to show thee relation between elements and bezier points ) :



Scripting : the following code has been used to create the last image materials

assuming you have


a dictionary of points' colors
material slot for each spline
-each material has a ramp node ( the material should be as the previously used one ^)




import bpy

curve = bpy.context.object

colors = {  0 : [(0,0,1,1), (0,1,1,1), (1,0,0,1), (1,0,1,1), (1,1,0,1), (1,0,1,1), (1,1,0,1)],   
            1 : [(0,1,0.3,1), (0,0,1,1), (1,0,0,1), (1,0,1,1), (1,1,0,1), (1,0,1,1), (1,1,0,1)],
            2 : [(1,0.4,1,1), (0.8,0,1,1), (0.5,0.5,0,1), (0.6,0,0.2,1), (1,1,0,1), (1,0,1,1), (1,1,0,1)]
         }

if curve.type == 'CURVE' :
    i = 0
    if len(curve.data.splines) &lt;= len(colors) :
        for spline in curve.data.splines :
            spline.material_index = i
            color_ramp = curve.material_slots[spline.material_index].material.node_tree.nodes['ColorRamp'].color_ramp
            l = len(spline.bezier_points) -1

            for e in range(1,l) :   # if i&gt;0  create new color elements
                color_ramp.elements.new(e/l)
                print(e/l)
                color_ramp.elements[e].color = colors[i][e]

            color_ramp.elements[0].color = colors[i][0]
            color_ramp.elements[-1].color = colors[i][0]

            i+=1