# AUTHOR NikitaKrutov
# VERSION 1.0.0
# Create Stereo Cameras


import lux
import math


def get_user_input():
    """
    Display a GUI to get camera names.
    """
    dialog_fields = [
        ("eye_distance", lux.DIALOG_INTEGER, "Eye Distance:", 65, (1, 10000)),
        ("camera_name", lux.DIALOG_ITEM, "Stereo Camera Name:", lux.getCamera(), lux.getCameras()),
        ("render", lux.DIALOG_ITEM, "Render images?", "No", ["Yes", "No"])
    ]

    user_input = lux.getInputDialog(title="Camera Name and Eye Distance", 
                                    desc="Enter the camera name that shold be stereo'ed and the eye distance:",
                                    values=dialog_fields,
                                    id="camera_names")
    if user_input:
        return (user_input["camera_name"][1]), (user_input["eye_distance"]), (user_input["render"][1])
    else:
        return None


    #rotation_matrix([
    #    [cos_a, -sin_a, 0],
    #    [sin_a,  cos_a, 0],
    #    [0,      0,     1]
    #])    
    

def camera_stereo(camera_name, eye_distance, render):

    file_name = lux.getSceneInfo()['name'].replace(".bip", "")  #get the file name without the .bip part
    path = lux.getSceneInfo()['file'].replace(lux.getSceneInfo()['name'], "") #get file path without the file name
    width = lux.getSceneInfo()['render_width']
    height = lux.getSceneInfo()['render_height']

    
    
    lux.setCamera(camera_name)    
    a = math.radians(lux.getSphericalCamera()[0])
    position = lux.getCameraPosition()

    print(position)
    print(a)

    lux.newCamera(f"000{camera_name}000_Stereo_L") #create new camera
    lux.newCamera(f"000{camera_name}000_Stereo_R") #create new camera
    
    lux.setCamera(f"000{camera_name}000_Stereo_L")

    e = [-math.sin(a)*(-eye_distance/2000), math.cos(a)*(-eye_distance/2000)]
    lux.setCameraPosition([position[0] + e[0], position[1], position[2] + e[1]])
    lux.saveCamera()

    output_file_path = os.path.join(rf"{path}{file_name}_Stereo", f"{file_name}_L.png")   #set path
    if render == "Yes": lux.renderImage(output_file_path, width, height)    #render frame from new camera

    lux.setCamera(f"000{camera_name}000_Stereo_R")

    e = [-math.sin(a)*(eye_distance/2000), math.cos(a)*(eye_distance/2000)]
    lux.setCameraPosition([position[0] + e[0], position[1], position[2] + e[1]])
    lux.saveCamera()

    output_file_path = os.path.join(rf"{path}{file_name}_Stereo", f"{file_name}_R.png")   #set path
    if render == "Yes": lux.renderImage(output_file_path, width, height)    #render frame from new camera
     
    lux.setCamera(camera_name)
    
    
def main():
    # Get user input for frame range    
    camera_name, eye_distance, render = get_user_input()
    if camera_name is None:
        return  # Cancel if no input
    
    camera_stereo(camera_name, eye_distance, render)
       

if __name__ == "__main__":
    main()




