# Path: psychoblend/util.py
# def escape_name(name):
#     name = name.replace("\\", "\\\\")
#     name = name.replace(" ", "\\ ")
#     name = name.replace("$", "\\$")
#     name = name.replace("[", "\\[")
#     name = name.replace("]", "\\]")
#     name = name.replace("{", "\\{")
#     name = name.replace("}", "\\}")
#     return name
# 
# def mat2str(m):
#     """ Converts a matrix into a single-line string of values.
#     """
#     s = ""
#     for j in range(4):
#         for i in range(4):
#             s += (" %f" % m[i][j])
#     return s[1:]
# 
# class ExportCancelled(Exception):
#     """ Indicates that the render was cancelled in the middle of exporting
#         the scene file.
#     """
#     pass


import bpy
from math import degrees, tan, atan
from mathutils import Vector, Matrix
from .util import escape_name, mat2str, ExportCancelled


class DistantDiskLamp:
    def __init__(self, ob, name):
        self.ob = ob
        self.name = name
        self.time_col = []
        self.time_dir = []
        self.time_rad = []

    def take_sample(self, render_engine, scene, time):
        render_engine.update_stats("", "Psychopath: Collecting '{}' at time {}".format(self.ob.name, time))
        self.time_dir += [tuple(self.ob.matrix_world.to_3x3() * Vector((0, 0, -1)))]

        if self.ob.data.psychopath.color_type == 'Rec709':
            self.time_col += [('Rec709', self.ob.data.color * self.ob.data.energy)]
        elif self.ob.data.psychopath.color_type == 'Blackbody':
            self.time_col += [('Blackbody', self.ob.data.psychopath.color_blackbody_temp, self.ob.data.energy)]
        elif self.ob.data.psychopath.color_type == 'ColorTemperature':
            self.time_col += [('ColorTemperature', self.ob.data.psychopath.color_blackbody_temp, self.ob.data.energy)]

        self.time_rad += [self.ob.data.shadow_soft_size]

    def export(self, render_engine, w):
        render_engine.update_stats("", "Psychopath: Exporting %s" % self.ob.name)
        w.write("DistantDiskLight $%s {\n" % self.name)
        w.indent()
        for direc in self.time_dir:
            w.write("Direction [%f %f %f]\n" % (direc[0], direc[1], direc[2]))
        for col in self.time_col:

# Path: psychoblend/util.py
# def escape_name(name):
#     name = name.replace("\\", "\\\\")
#     name = name.replace(" ", "\\ ")
#     name = name.replace("$", "\\$")
#     name = name.replace("[", "\\[")
#     name = name.replace("]", "\\]")
#     name = name.replace("{", "\\{")
#     name = name.replace("}", "\\}")
#     return name
# 
# def mat2str(m):
#     """ Converts a matrix into a single-line string of values.
#     """
#     s = ""
#     for j in range(4):
#         for i in range(4):
#             s += (" %f" % m[i][j])
#     return s[1:]
# 
# class ExportCancelled(Exception):
#     """ Indicates that the render was cancelled in the middle of exporting
#         the scene file.
#     """
#     pass


# Path: psychoblend/world.py
import bpy
from math import degrees, tan, atan
from mathutils import Vector, Matrix
from .util import escape_name, mat2str, ExportCancelled



class DistantDiskLamp:
    def __init__(self, ob, name):
        self.ob = ob
        self.name = name
        self.time_col = []
        self.time_dir = []
        self.time_rad = []

    def take_sample(self, render_engine, scene, time):
        render_engine.update_stats("", "Psychopath: Collecting '{}' at time {}".format(self.ob.name, time))
        self.time_dir += [tuple(self.ob.matrix_world.to_3x3() * Vector((0, 0, -1)))]

        if self.ob.data.psychopath.color_type == 'Rec709':
            self.time_col += [('Rec709', self.ob.data.color * self.ob.data.energy)]
        elif self.ob.data.psychopath.color_type == 'Blackbody':
            self.time_col += [('Blackbody', self.ob.data.psychopath.color_blackbody_temp, self.ob.data.energy)]
        elif self.ob.data.psychopath.color_type == 'ColorTemperature':
            self.time_col += [('ColorTemperature', self.ob.data.psychopath.color_blackbody_temp, self.ob.data.energy)]

        self.time_rad += [self.ob.data.shadow_soft_size]

    def export(self, render_engine, w):
        render_engine.update_stats("", "Psychopath: Exporting %s" % self.ob.name)
        w.write("DistantDiskLight $%s {\n" % self.name)
        w.indent()
        for direc in self.time_dir:
            w.write("Direction [%f %f %f]\n" % (direc[0], direc[1], direc[2]))
        for col in self.time_col:

            if col[0] == 'Rec709':