import math
import bpy

def initialize_scene(shape_parameters):
    
    # Set up scene
    objs = bpy.data.objects
    bpy.data.scenes['Scene'].objects.unlink(objs["Cube"])
    objs.remove(objs["Cube"])    
            
def setup_scene(shape_parameters): 
    
    # Set up world
    bpy.context.scene.world.use_sky_paper = True
    bpy.context.scene.world.horizon_color = (0.99, 0.8, 0.8)
    #bpy.context.scene.world.light_settings.use_environment_light = True
    
    # Add back wall
    bpy.ops.mesh.primitive_plane_add(radius=20, location=(-10.0, 0.0, 0.0))
    bpy.ops.transform.rotate(value=math.pi/2.0, axis=(0.0,1.0,0.0))
    bpy.data.objects["Plane"].name = "back_wall"
    mat = bpy.data.materials.get("backwall-material")
    if mat is None:
        mat = bpy.data.materials.new("backwall-material")
    mat.diffuse_color = (0.25, 0.25, 0.25)
    mat.specular_color = (0.25, 0.25, 0.25)
    mat.diffuse_intensity = 1.0
    mat.specular_intensity = 1.0
    bpy.context.scene.objects.active = bpy.data.objects["back_wall"]
    if bpy.context.active_object.data.materials:
        # assign to 1st material slot
        bpy.context.active_object.data.materials[0] = mat
    else:
        # no slots
        bpy.context.active_object.data.materials.append(mat)     
    
    # Set up cameras
    bpy.data.objects["Camera"].location = (8.5, 0.0, -2)
    fov = 65.0
    bpy.data.objects["Camera"].data.angle = fov*(math.pi/180.0)
    bpy.data.objects["Camera"].rotation_mode = 'XYZ'
    bpy.data.objects["Camera"].rotation_euler[0] = 1.5
    bpy.data.objects["Camera"].rotation_euler[1] = 0.0
    bpy.data.objects["Camera"].rotation_euler[2] = 1.5708
    
    # Lamps
    bpy.data.scenes['Scene'].objects.unlink(bpy.data.objects["Lamp"])
    bpy.data.objects.remove(bpy.data.objects["Lamp"])
    
    bpy.ops.mesh.primitive_plane_add(radius=5, location=(10.0, -10.0, 5.0))
    bpy.ops.transform.rotate(value=math.pi/2.0, axis=(1.0,2.0,0.0))
    bpy.data.objects["Plane"].name = "key_lamp" 
    mat = bpy.data.materials.get("key-material")
    if mat is None:
        mat = bpy.data.materials.new("key-material")
    mat.use_nodes = True
    mat.node_tree.nodes.new('ShaderNodeEmission')
    inp = mat.node_tree.nodes["Material Output"].inputs["Surface"]
    outp = mat.node_tree.nodes["Emission"].outputs["Emission"]
    mat.node_tree.nodes['Emission'].inputs[1].default_value = 30.0
    mat.node_tree.links.new(inp, outp)
    
    bpy.context.scene.objects.active = bpy.data.objects["key_lamp"]
    if bpy.context.active_object.data.materials:
        # assign to 1st material slot
        bpy.context.active_object.data.materials[0] = mat
    else:
        # no slots
        bpy.context.active_object.data.materials.append(mat) 
        
    bpy.ops.mesh.primitive_plane_add(radius=5, location=(10.0, 10.0, -5.0))
    #bpy.ops.transform.rotate(value=-math.pi/2.0, axis=(1.0,2.0,0.0))
    #bpy.ops.transform.rotate(value=-math.pi/4.0, axis=(0.0,0.0,1.0))
    bpy.data.objects["Plane"].name = "fill_lamp" 
    bpy.data.objects["fill_lamp"].location = (10.0, 10.0, -5.0)
    bpy.data.objects["fill_lamp"].rotation_euler[0] = 0.6454
    bpy.data.objects["fill_lamp"].rotation_euler[1] = 0.7169
    bpy.data.objects["fill_lamp"].rotation_euler[2] = 0.0000
    
    mat = bpy.data.materials.get("fill-material")
    if mat is None:
        mat = bpy.data.materials.new("fill-material")
    mat.emit = 12.0
    bpy.context.scene.objects.active = bpy.data.objects["fill_lamp"]
    if bpy.context.active_object.data.materials:
        # assign to 1st material slot
        bpy.context.active_object.data.materials[0] = mat
    else:
        # no slots
        bpy.context.active_object.data.materials.append(mat)     
        