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:


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,

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


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!


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