asign a material to a custom object automatically

hi I want to add a custom object automatically with the material…

I know how to make a custom object… for example this code make a custom arrow and a custom menú:
http://www.pasteall.org/51129/python

but I want to assign a specific material to this that is in the bpy.data.materials…

I tried with different options and following the instruccion in Cookbook the http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Materials_and_textures

but when I add a button or menu access to the arrow that don’t have the material… :frowning:

some help please!

thanks Diego

if it has no materials yet, just append a material datablock

mat = bpy.data.materials[‘Material1’]
Object.data.materials.append(mat)

Otherwise get the material’s index (granted the desired material is in a material slot already) and assign it to all Polygon.material_index

me = ob.data
index = me.materials.find("Material1")
if index == -1:
    index = len(me.materials)
    me.materials.append(mat)

for poly in me.polygons:
    poly.material_index = index

hi
someone happen in the forum but code is “invisible” :smiley:

first one is really usefull really thanks… as I did not see the code you can assign a new material to the arrow, but not one existing.

mat = bpy.data.materials.new ("material1") 
         mat.use_shadeless = True 
         mat.use_transparency = True 
         mat.alpha = 0.0 


         object.data.materials.append (mat)

also try the same code that you give me but with brackets … so not working
mat = bpy.data.materials(‘Material1’) this is a … rookie mistake

thanks again … You gave me light

best

Diego

If you access an array of objects, use []

If you call a function or method, use ()

You can use bpy.data.materials.get(“Material1”) if you want, but only if you don’t know for sure it exists. Instead of raising an exception, it will return None or another default value (second argument of get()).