How to set colour of this line

Hi,

I’m generating a curve using the code below:


        curvedata = bpy.data.curves.new(name=name+"_curve", type='CURVE')  
        curvedata.dimensions = '3D'  
        obj = bpy.data.objects.new(name, curvedata)    
        bpy.context.scene.objects.link(obj)  
        # add the points for the line
        polyline = curvedata.splines.new('POLY')  
        polyline.points.add(len(cList)-1)  

How can I give the line a defined color?
I tried:

obj.color = (0.0, 0.0, 1.0, 1.0)

But that doesn’t work.
Any idea?

I found out that I have to create a material with the color and set the material of the line.
I tried this:


material = bpy.data.materials.new(name+"_material")
material.diffuse_color = (0.0,1.0,0.0)
polyline.materials.append(material)

But that doesn’t work.
What am I doing wrong?

The .materials property is part of the curve object. Note that you need to add some depth to see a color:

import bpy

name = "Poly Line"
cList = ((0,0,0,0), (0.1, 1.75, 1.4, 0), (1.75, 3, 0.2, 0), (1.4, 0, -2.5, 0))

curvedata = bpy.data.curves.new(name=name+"_curve", type='CURVE')  
curvedata.dimensions = '3D'  

obj = bpy.data.objects.new(name, curvedata)    
bpy.context.scene.objects.link(obj)  

# add the points for the line
polyline = curvedata.splines.new('POLY')  
polyline.points.add(len(cList)-1)
for p, co in zip(polyline.points, cList):
    p.co = co

material = bpy.data.materials.new(name+"_material")
material.diffuse_color = (0.0,1.0,0.0)
curvedata.materials.append(material)
curvedata.bevel_depth = 0.2

Thanks. That works.
I assume I can create one material and append it to several curves. Right?
But when I’m doing so, Blender crashes.

You can.

See here for reasons for crash:
Help! My script crashes Blender

You’re probably using a freed datablock