#!bpy
"""
Name: 'mat1.py'
Blender: 2.71
Group: 'Sample'
Tooltip: 'Put one image on each site of a cube'
"""
import bpy
import os
#####################################################
def delete_old_stuff():

    # escape edit mode
    if bpy.ops.object.mode_set.poll():
        bpy.ops.object.mode_set(mode='OBJECT')

    # delete all mesh objects
    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete()

    # delete all materials
    for i in bpy.data.materials.values():
        bpy.data.materials.remove(i)

    # delete all textures
    for i in bpy.data.textures.values():
        bpy.data.textures.remove(i)

    # delete all images 
    for i in bpy.data.images.values():
        # delete image path, this is only possible without a user
        i.user_clear()
        # delete all, except »Render Result«
        if i.name != "Render Result":
            bpy.data.images.remove(i)

    
#####################################################
def make_materials():
        
    materials = []
    images = ["blume.jpg", "boot.jpg", "jueterbog.jpg", "kopf.jpg",
              "moster.jpg", "telefon.jpg", "warnung.jpg"]
 
    for image in images:
        
        # new material
        matname = "mat" + image[:-4]
        if not matname in bpy.data.materials:
            material = bpy.data.materials.new(matname)
            material.diffuse_color = (0, .5, .4)
            
        # new texture
        texname = "tex" + image[0:-4]
        texUV = bpy.data.textures.new(texname, type="IMAGE")
        image_path = os.path.expanduser("exercises/blender/pymove/uv_mapping_img/" + image)
        print ("image_path: " + image_path)
        texUV.image = bpy.data.images.load(image_path)
 
        # bind material and texture
        material.texture_slots.add()
        material.active_texture = texUV
        material.texture_slots[0].texture_coords = "ORCO"
        material.texture_slots[0].mapping = "CUBE"
        
        materials.append(material)
 
    return materials

#####################################################
def same_image_to_all_sides(obj, material):   
    # it looks like we are using the last texture in the array
    obj.data.materials.append(material)
    
#####################################################
def different_image_to_each_side (obj, materials):   
    # it looks like we are using the last texture in the array
    for material in materials:
        obj.data.materials.append(material)
    for face in obj.data.polygons:
        face.material_index = face.index
            
################################################
if __name__ == "__main__":
    # housekeeping
    delete_old_stuff()
    # otherwise we can't see jack squat
    bpy.types.WorldLighting.use_environment_light = True
    
    materials = make_materials()
    
    bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0))
    obj = bpy.context.scene.objects.active
    obj.name = 'uvmapped_cube_1'
    
    same_image_to_all_sides(obj, materials[0])
    
    bpy.ops.mesh.primitive_cube_add(location=(6, 0, 0))
    obj = bpy.context.scene.objects.active
    obj.name = 'uvmapped_cube_2'
    
    different_image_to_each_side (obj, materials)
