#!bpy
"""
Name: 'Flying Letters'
Blender: 2.7x
Group: 'Animation'
Tooltip: 'Helperscripts to animate letters'
"""
import bpy
import random

def textToSingleLetters(text="Hallo", x_start=0, y_start=0):
    """ A given text line is divided in single letters (objects). """
    counter = 1
    kerning = .1
    pos_next_x = 0
    for letter in text:
        name = "row_%s_%s" % (y_start, counter)
        myFontCurve = bpy.data.curves.new(type="FONT", name=name)
        myFontOb = bpy.data.objects.new(name, myFontCurve)
        myFontOb.location = (x_start + pos_next_x, y_start, 0)       
        myFontOb.data.body = letter
        bpy.context.scene.objects.link(myFontOb)
        bpy.context.scene.update()
        counter += 1
        pos_next_x = pos_next_x + myFontOb.dimensions.x + kerning
 
def defineEndPosition():
    """ Register endposition of all text objects. 

        This is the Position of each letter after creation.
    """
    data = {}
    all = [item.name for item in bpy.data.objects]
    for name in all:
        if name[0:3] == "row":
            obj = bpy.data.objects[name]
            data[name] = [(obj.location.x,
                           obj.location.y,
                           obj.location.z)]
    return data

def calcStartPosition(d=None):
    """ Calculate random start position of all text objects. """

    all = [item.name for item in bpy.data.objects]
    for name in all:
        if name[0:3] == "row":                
            obj = bpy.data.objects[name]
            randX = round(random.uniform(-4, 4),3)
            randY = round(random.uniform(10, 30),3)
            randZ = round(random.uniform(10, 30),3)
            d[name].append((randX, randY, randZ))
    return d

def calcStartEndFrames(d=None, startFrame=10, endFrame=190):
    """ Calculate random startframe for each object. """
    pass


def setkeyframes(d=None):
    """ One line describing the task of this function  """
    pass

if __name__ == '__main__':
    bpy.ops.object.select_by_type(type='FONT')
    bpy.ops.object.delete()
    textToSingleLetters("Python", y_start=-1, x_start=-2)
    textToSingleLetters("Blender", y_start=0, x_start=-2)
    textToSingleLetters("Blend4Web", y_start=1, x_start=-2)

    d = defineEndPosition()
    d = calcStartPosition(d)
    print(d)