Link Material to Object

I’m getting mad :o …

How can I link (or assign) a Material to an Object?
Simply like I do In the Material Editor ?

I really don’t understand, is the material linked to Mesh, or …?

With setMaterials() I don’t have any errors on the console but the Material is not linked to the Object…

I believe the material has been linked to the Object. In the Material buttons, just under the material dropdown box, there are two little buttons OB and ME. By default ME is on. If you tried setMaterials and click the OB button, you’ll see that the Material has been linked to the Object. But in a normal workflow we always assign Materials to the Mesh for some obscure reason…
So the proper code would be this if I’m right:

import Blender

ob = Blender.Object.Get("Cube")
me = ob.getData(mesh=1)
mat = Blender.Material.Get("funky_material")

me.materials = [mat]

Blender.Window.RedrawAll()

Thanks Bagration,
I use your code and it works, but I’m really not a good coder.
What I want to achieve is a script that render alpha image per materials in my scene. I write a script that is working for that. But when there are object without materials they render in each ‘alpha passe’. So I want to add a function that link a basic material to object that don’t have any material.

The function is this :
(but it’s not working… :expressionless: )

def mat():

	global list

	default=Material.New('0 Default')

	for a in list:

		mesh=a.getData(mesh=1)

		mat=a.getMaterials()

		

		if mat==[]:

			mesh.materials=[default]

		else : return

Try replacing this:

mat=a.getMaterials()

with this:

mat = mesh.materials

Also, to check whether a list is empty, it is faster and more “Pythonic” to do this:

if not mat:

instead of:

if mat == []:

I’ve got an AttributeError on a.materials.
The ‘list’ is ; Object.Get(), so a is an Object, so…

Whoops, I edited my previous post.
I’m still learning myself :wink: