How to color a object using python

Hi, I am new to blender scripting.
I had taken a default cube, i just want to color the cube using script.

import Blender
ob=Blender.Object.Get(‘Cube’)

please complete the coloring part of above code
and i just want to color the whole object not a single face

Thank you,

Forget old Blender (nearly)
Blender 2.54 SVN320050
Example make the default Cube ‘violet’ (ok …)


import bpy
mat = bpy.data.materials.new("PKHG")
mat.diffuse_color = (float(.5),0.0,1.0)
o = bpy.context.selected_objects[0] 
o.active_material = mat

i have objects like cube, cylinder and tube

i want to add colors to thee objects


import Blender
from Blender import *
scn = Blender.Scene.GetCurrent()
me=Mesh.Primitives.Cylinder()
outershealth=scn.objects.new(me)

i just want to color the above cylinder.
please help me.

Sorry, I would know only how to do THAT in the latest version of Blender.

Why this not working with specular ?

import bpy
mat = bpy.data.materials.new(“PKHG”)
mat.diffuse_color = (float(1.5),0.0,1.0)
mat.specular_color = (200.0,1.0,100.0)
o = bpy.context.selected_objects[0]
o.active_material = mat

Works in my Blender (2.54 W 32) … may get a newer version?!

In 2.49b:

from Blender import *

ob = Object.Get("Cube")
me = ob.getData(mesh=1)
material = Material.New("matRose")
material.rgbCol = [0.8,0.6,0.6]  # some rose color
ob.setMaterials([material])
ob.colbits = (1<<0)
me.materials = [material]

Redraw()

Not that I dont like Blender development but while asking “Why this or that python code is not working in 2.5x?” almost 90% of answers seems to be “Because API is unstable or still incomplete or you’re using another version.”

I wish this percentage be less than 5 !!! :stuck_out_tongue:

Regards,

Thanks bro… I got it what i want…