# Copyright 2009 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# Topic: FBRenderer, FBVideoGrabber, FBVideoRenderDepth, FBVideoRenderDepth, FBVideoCodecManager
#

from pyfbsdk import FBMessageBox, FBApplication, FBVideoGrabber, FBVideoRenderDepth, FBVideoRenderDepth, FBVideoCodecManager

import sys
from sys import platform

# This assumes that we are working on NT or Mac OSX and that we only have
# a minimal install of Python. We would have used the 'os' module and the
# methods 'listdir()' and 'getenv()'

# The path separator indicate which character is used in a fully
# qualified path between directories names. a '/' on Unix platforms
# and a '\' on Windows.
lPathSep = None

if platform == 'win32':
    from nt import environ, listdir
    lPathSep = "\\"
if platform == 'darwin':
    from posix import environ, listdir
    lPathSep = "/"

# There is no point in going forward if the platform is not known.
if not lPathSep:
    FBMessageBox( "ERROR", "Unsupported platform... Cannot proceed.", "OK", None, None )
    FBApplication().FileExit()

# We use a try/except block, because the call to 'listdir' can fail.
try:
    # We take for granted that the folder containing the FBX files is
    # indicated with the environment variable 'RENDER_SRC_FOLDER'.
    lRenderSrcFolder = None
    if environ.has_key( 'RENDER_SRC_FOLDER' ):
        lRenderSrcFolder = environ['RENDER_SRC_FOLDER']
    else:
        # We have failed to find the folder containing the FBX files...
        # The environment variable is probably not defined.
        FBMessageBox( "ERROR", "The environment variable 'RENDER_SRC_FOLDER' is not defined... Cannot proceed.", "OK", None, None )
        FBApplication().FileExit()

    # And the place where to store the rendered scene is indicated by
    # the environment variable 'RENDER_DST_FOLDER'.
    lRenderDstFolder = None
    if environ.has_key( 'RENDER_DST_FOLDER' ):
        lRenderDstFolder = environ['RENDER_DST_FOLDER']
    else:
        # We have failed to find the folder where to save the renders...
        # The environment variable is probably not defined.
        FBMessageBox( "ERROR", "The environment variable 'RENDER_DST_FOLDER' is not defined... Cannot proceed", "OK", None, None )
        FBApplication().FileExit()

    # The file format of the render is indicated by 'RENDER_FILE_FORMAT'.
    lRenderFileFormat = None
    if environ.has_key( 'RENDER_FILE_FORMAT' ):
        lRenderFileFormat = environ['RENDER_FILE_FORMAT']
    else:
        # We have failed to find the folder where to save the renders...
        # The environment variable is probably not defined.
        FBMessageBox( "ERROR", "The environment variable 'RENDER_FILE_FORMAT' is not defined... Cannot proceed", "OK", None, None )
        FBApplication().FileExit()

    # If the variable is defined, let's process the files
    if lRenderSrcFolder and lRenderDstFolder and lRenderFileFormat:

        # Make sure that the destination folder exists...
        # Ideally it should be created automatically.
        listdir( lRenderDstFolder )

        # Obtain the list of files in the folder, and sort them alphabetically.
        lFileList = listdir( lRenderSrcFolder )

        # We sort the list
        lFileList.sort()

        # Iterate in the list
        for lFileName in lFileList:

            # Ensure that we handle only FBX files.
            if lFileName.endswith('.fbx'):

                # Create an application object.
                lApp = FBApplication()

                # We need the full path of the FBX file to load it.
                lSrcFileName = lRenderSrcFolder + lPathSep + lFileName

                # We also need the full path of the output file.
                lDstFileName = lRenderDstFolder + lPathSep + lFileName.replace( 'fbx', lRenderFileFormat )

                # Open the file in the application.
                lApp.FileOpen( lSrcFileName )

                # Get the default rendering options, which are saved in the FBX file.
                lOptions = FBVideoGrabber().GetOptions()

                # Set VideoCodec Option:
                VideoManager = FBVideoCodecManager()
                VideoManager.VideoCodecMode = FBVideoCodecMode.FBVideoCodecUncompressed

                # Set the name of the rendered file.
                lOptions.OutputFileName = lDstFileName

                # On Mac OSX, QuickTime renders need to be in 32 bits.
                if lRenderFileFormat == '.mov' and platform == 'darwin':
                    lOptions.BitsPerPixel = FBVideoRenderDepth.FBVideoRender32Bits

                # Do the render. This will always be done in uncompressed mode.
                lApp.FileRender( lOptions )

                # Clear the scene.
                lApp.FileNew()

except Exception, e:
    # Unkown error encountered... Maybe from the 'listdir' call failing...
    FBMessageBox( "ERROR", "Unknown error encountered. Aborting! " + str(e), "OK", None, None )

# Now that we are done, we just exit the application.
FBApplication().FileExit()