"""
CC BY-SA, (http://www.creativecommons.org/licenses/)
September 2012, Lukas Treyer, Information Architecture, ETH Zurich
Course: http://www.ia.arch.ethz.ch/category/teaching/hs2012-complexcity/
"""

import bpy
from mathutils import Color

meshes = []
path = "/Volumes/schmitt/Transfer/info/mesh_test_05.txt"

# file handling
file = open(path, "r")

#[[['x','y','z'],['x','y','z'],['x','y','z']],[['f1','f2','f3','f4'],['f5','f6','f7','f8']],[1234567,2345678]]

for line in file:
    mesh = eval(line)    
    meshes.append(mesh)

for mesh in meshes:
    me = bpy.data.meshes.new('ColorPoly')
    me.from_pydata(mesh[0],[],mesh[1])
    obj = bpy.data.objects.new('ColorPoly',me)
    
    # color it
    col = obj.data.vertex_colors.new()
    j = 0
    for face in mesh[1]:
        for i in face:
            c = mesh[2][i]
            r, g, b = [(c >> (8*i)) & 255 for i in range(3)]
            col.data[j].color = Color((r/255,g/255,b/255))
            j += 1
    
    bpy.context.scene.objects.link(obj)