Adding a Material to selected object (new to Python/ Blender scripting)

Hello everyone,

I just started looking into Python and Blender scripting. Needless to say, I have very little understanding of the Python syntax, commands and Blender API.
I’m trying out some simple commands and just now I was trying to assign a Material to an Object (Cube) using the following line:

bpy.context.object.material_slots.add()

However, this returns the following error:

TypeError: bpy_prop_collection.add(): not supported for this collection.

Can anyone please explain to me why this is? And also, considering the add() function doesn’t take any arguments, how could I assign a specific material (either by name or index)?

Thanks in advance,
Patrick

http://blenderartists.org/forum/showthread.php?t=193908

dowaload PDF and scripts
there is a script showing how to add mat to object

salutations

Hey Ricky,

Thanks! I’m sure that’ll come in handy! I was just about to post that I found my solution, but seems I got more than I asked for. =P Thanks again!

-Patrick

yikes. how did i reach here. hey Senshi. seems im well in your footsteps. yikes. =)