#! bpy
"""
Name: 'berlin-clock.py'
Blender: 27x
Group: 'Example'
Tooltip: 'Construct a berlin-clock'
"""
import bpy
from timing import timing
import random

color_a = random.uniform(0.3, 1)
color_b = random.uniform(0.3, 1)
color_c = random.uniform(0.3, 1)
color_d = random.uniform(0.3, 1)
color_e = random.uniform(0.3, 1)
color_f = random.uniform(0.3, 1)

clock = [["seconds"],
         ["hour_01", "hour_02", "hour_03", "hour_04"],
         ["hour_05", "hour_06", "hour_07", "hour_08"],
         ["minute_01", "minute_02", "minute_03", "minute_04"] +
         ["minute_05", "minute_06", "minute_07", "minute_08"] +
         ["minute_09", "minute_10", "minute_11"],
         ["minute_12", "minute_13", "minute_14", "minute_15"]]

MATERIAL_001 = bpy.data.materials.new('Mat001')

def create(object):
    "Create and compose a berlin-clock"

    scn = bpy.context.scene
    rows = len(object)
    distances = [9.5, 3.75, 3.75, 1.6, 3.75]
    for row in range(rows):
        cols = len(object[row])
        for col in range(cols):
            distance = distances[row]
            x = row * 3
            y = col * distance + distance
            bpy.ops.mesh.primitive_cube_add(location=(x, y, 0))

            if row == 3:
                bpy.ops.transform.resize(value=(0.5, 0.5, 0.5))

            obj = bpy.context.object
            obj.name = clock[row][col]

    res = timing()
    for name in res.keys():
        scn.objects.active = scn.objects[name]
        obj = bpy.context.object
        if res[name] is True:
            setColor(obj, MATERIAL_001, (1, 0, 1))
            
def setColor(obj, material, color):
    material.diffuse_color = color
    material.specular_hardness = 200
    obj.data.materials.append(material)

                
if __name__ == '__main__':
    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete()
    create(clock)
    timing()