# -*- coding: utf-8 -*-
# AUTHOR Dries Vervoort (Luxion)
# VERSION 0.0.1
# Batch render Toy Train models to images.
import os

def getGeometryImportOptions():
  importOptions = lux.getImportOptions()
  importOptions['center_geometry'] = False
  importOptions['snap_to_ground'] = True
  importOptions['up_vector'] = 2
  importOptions['adjust_camera_look_at'] = False
  importOptions['adjust_environment'] = False
  importOptions['update_mode'] = False
  importOptions['group_by_shader'] = False
  importOptions['group_by'] = 2
  importOptions['new_import'] = False
  importOptions['tessellation_quality'] = 0.15
  importOptions['accurate_tessellation'] = True
  return importOptions

def getCameras(allCameras):
  if allCameras:
    cameraToRender = lux.getCameras()[0:-1]
  else:
    cameraToRender = [lux.getCamera()]
  return cameraToRender

def renderImage(cameraToRender, outputFolder, file, style, outputExtension, renderOptions):
  for camera in cameraToRender:
    lux.setCamera(camera)
    outputPath = outputFolder + os.path.sep + file
    outputPath = os.path.splitext(outputPath)[0] + "_" + style + "_" + str(camera) + "." + str(outputExtension)
    print("Rendering {}".format(outputPath))
    if not lux.renderImage(path = outputPath, width = 800, height = 800, opts = renderOptions):
      print("Rendering was cancelled by user.")
      break

def main():
  sceneInfo = lux.getSceneInfo()
  imageFormatCombo = ["jpeg", "png", "tiff", "exr"]
  values = [("ifold", lux.DIALOG_FOLDER, "Folder to import STEP models from:", None),
            ("oext", lux.DIALOG_ITEM, "Output image format:", "png", imageFormatCombo),
            ("ofold", lux.DIALOG_FOLDER, "Output folder:", None),
            ("toon", lux.DIALOG_CHECK, "Include Toon pass", True),
            ("camera", lux.DIALOG_CHECK, "Render all cameras", True)]
  opts = lux.getInputDialog(title = "Batch Render Model Collection",
                            desc = "Renders a collection of models in a folder to images.",
                            values = values,
                            id = "batchrenderSTEPmodels.py.luxion")
  if not opts: return

  importExtension = "step"

  importFolder = opts["ifold"]
  outputExtension = opts["oext"][1]
  outputFolder = opts["ofold"]
  toonPass = opts["toon"]
  allCameras = opts["camera"]

  importOptions = getGeometryImportOptions()
  renderOptions = lux.getRenderOptions()
  renderOptions.setMaxSamplesRendering(8)
  cameraToRender = getCameras(allCameras)

  modelList = []
  for file in os.listdir(importFolder):
    if file.endswith(importExtension):
      modelList.append(file)
      print(modelList)

      scenePath = sceneInfo.get('file')
      lux.openFile(scenePath)

      importPath = importFolder + os.path.sep + file

      print("Importing {}".format(importPath))
      if not lux.importFile(path = importPath, opts = importOptions):
        raise Exception("Error in import! User cancelled?")

      lux.setMaterialTemplate(name = "Toy Train - Realistic")
      style = "Realistic"
      renderImage(cameraToRender, outputFolder, file, style, outputExtension, renderOptions)

      if toonPass:
        lux.setMaterialTemplate(name = "Toy Train - Toon")
        style = "Toon"
        renderImage(cameraToRender, outputFolder, file, style, outputExtension, renderOptions)
        
main()