From BlenderWiki
< Doc:Tutorials | Extensions | Python | BSoD
[edit] Creating simple Materials and linking them to Objects
As we have learned, Blender objects consist of two parts, the Object part which holds spatial information and an ObData part that holds the geometry. Blender allows Materials to be linked to either the Object or to the ObData. This choice is set in the User Preferences. For now, we will only consider the case of Materials linked to the ObData.
Blender supports up to 16 materials per object. The items in a material list are numbered from 0 to 15.
For our example, we create 3 materials and apply them to the faces of an existing Cube.
For this example, start with a scene containing a Cube, a Camera and Lamp.
[edit] Program: New Material
import Blender as B
# first object in selection list
ob = B.Object.GetSelected()[0]
# get the Mesh version of our cube, not NMesh
me = B.Mesh.Get( ob.data.name )
# create some materials
red = B.Material.New('Red')
red.rgbCol = 1,0,0 # red, green, blue values
green = B.Material.New('Green')
green.rgbCol = 0,1,0 # red, green, blue values
blue = B.Material.New('Blue')
blue.rgbCol = 0,0,1 # red, green, blue values
# add the material list to our ObData
me.materials = [red, green, blue ]
# set the face materials of our cube
# we can set all faces the same color like this
for f in me.faces:
f.mat = 0
# or for fun, we can alternate colors like this
for i in range(len(me.faces)):
me.faces[i].mat = i % 3







![[]](/skins/blender/open.png)
