import maya.cmds as cmds
from pathlib import Path
#______________________________README____________________________________________
'''
- imagePlaneToggle is a python based tool for managing
imagePlanes and cameras in Maya for use in camera matching
and CG integration.

- ensure Maya project is set correctly

- one image plane per camera

- use the slider to toggle through the current image planes you have imported

'''
#________________________________________________________________________________


class ImagePlaneManager():
    
    def __init__(self):
        
        self.projectDir = Path(cmds.workspace(q = 1, rd = 1))
        self.workingDir = self.projectDir / 'sourceimages'
        self.storeImagePlanesList = []
    
    def show(self):
        self.createMyUI()
    
    def createMyUI(self):
        #Build UI      
        self.window = cmds.window(title= "Image_Plane_Manger", widthHeight = (500, 500), nde = 1, s =1)
        
        cmds.columnLayout(adjustableColumn = True, columnAlign = "center", rowSpacing = 25)
        
        cmds.button(l = 'Create_Camera', c = self.createCam)
        
        self.cameraSelect = cmds.optionMenu(l = 'Manage_Camera', bsp = self.updateCamDisplay, cc = self.lookThroughCamera)
        
        cmds.button(l = 'Add_Image_Plane_To_Selected_Camera', c = self.createImagePlanForCamera)

        cmds.button(l = 'Export_Camera_As_FBX', c = self.exportCamera)
        cmds.button(l = 'Update_Image_Planes', c = self.updateImagePlanes)
        self.displayImagePlanes = cmds.optionMenu(l = 'Image_Planes')

        self.ctrl = cmds.intSlider(min = 0, max = 1, v = 0, s = 1, dc = self.sliderDragCommand)
                   
        cmds.showWindow(self.window)
                
    def createCam(self, *args):
        #Function that creates a new camera in the scene
        newCam = cmds.camera(n = 'renderCam1') 
    
    def getCamera(self):
        #Function that returnes the current camera in the camera options menu
        selectedCamera = cmds.optionMenu(self.cameraSelect, q = 1, v = 1)
        
        return selectedCamera   
        
    def createImagePlanForCamera(self, *args):
        #Function that creates an image plane for the selected camera
        
        selectedCamera = self.getCamera()
        planeName = selectedCamera +'ImagePlane'

        try:
            if cmds.objExists(planeName +'1') != True:
                cmds.imagePlane(c = selectedCamera, n = planexportCameraeName)
                if self.storeImagePlanesList != None:
                    cmds.setAttr(imagePlaneAttrib, imagePath, type = 'string')
            
        except:
            cmds.warning('ImagePlaneManager.Error: This camera already has an image plane')
    
    def lookThroughCamera(self, *args):
        selectedCamera = self.getCamera()
        cmds.lookThru(selectedCamera)
        
        
           
    def updateCamDisplay(self, *args):
        #Function that updates the camera menu to contain all scene cameras
        
        cameras = cmds.listCameras(p = 1)
        currentMenu = cmds.optionMenu(self.cameraSelect, q = 1, ils = 1)
        if currentMenu != None:
            for menuItem in currentMenu:
                cmds.deleteUI(menuItem, mi = 1)
        for camera in cameras:
            cmds.menuItem(p = self.cameraSelect, l = camera)

    
    def updateImagePlanes(self, *args):
        #Add image planes to the image plane menu
        
        updateImages = cmds.fileDialog2(dir = self.workingDir, ds = 2, okc = 'Choose_Images', cap = 'Plane_Management', fm = 4)
        try:    
            for image in updateImages:
                if image not in self.storeImagePlanesList:
                    self.storeImagePlanesList.append(image)
                    cmds.menuItem(p = self.displayImagePlanes, l = image)  
            cmds.intSlider(self.ctrl, e = 1, max = len(self.storeImagePlanesList) - 1)                      
        except:
            cmds.warning('ImagePlaneManager.Error: No images selected')
            
    def sliderDragCommand(self, *args):
        #Function that changes the displayed image plane when the slider is dragged
        
        sliderValue = cmds.intSlider(self.ctrl, q = 1, v = 1)
        selectedCamera = self.getCamera()
        imagePlaneAttrib = (selectedCamera + 'ImagePlane1.imageName')
        if cmds.objExists(selectedCamera + 'ImagePlane1'):
            imagePath = self.storeImagePlanesList[sliderValue]
            cmds.setAttr(imagePlaneAttrib, imagePath, type = 'string')
    
    def exportCamera(self, *args):
        #Function for exporting the selected camera 
        selectedCamera = self.getCamera()
        destination = self.projectDir / 'scenes' / selectedCamera
        cmds.select(selectedCamera)
        cmds.file(destination, force = 1, type = "FBX export", pr = 1, es = 1)
        cmds.select(cl = 1)
              
        
toggle = ImagePlaneManager()
toggle.show()
print(toggle.projectDir)
print(toggle.workingDir)