Rendering/render.py

Rendering/render.py
1 # Copyright 2009 Autodesk, Inc. All rights reserved.
2 # Use of this software is subject to the terms of the Autodesk license agreement
3 # provided at the time of installation or download, or which otherwise accompanies
4 # this software in either electronic or hard copy form.
5 #
6 # Topic: FBRenderer, FBVideoGrabber, FBVideoRenderDepth, FBVideoRenderDepth, FBVideoCodecManager, FBVideoCodecMode, FBAudioFmt
7 #
8 
9 from pyfbsdk import FBMessageBox, FBApplication, FBVideoGrabber, FBVideoRenderDepth, FBVideoRenderDepth, FBVideoCodecManager, FBVideoCodecMode, FBAudioFmt_AppendFormat
10 import sys, os
11 from os import environ, listdir
12 
13 # We use a try/except block, because the call to 'listdir' can fail.
14 try:
15  # We take for granted that the folder containing the FBX files is
16  # indicated with the environment variable 'RENDER_SRC_FOLDER'.
17  lRenderSrcFolder = None
18  if environ.has_key( 'RENDER_SRC_FOLDER' ):
19  lRenderSrcFolder = environ['RENDER_SRC_FOLDER']
20  else:
21  # We have failed to find the folder containing the FBX files...
22  # The environment variable is probably not defined.
23  FBMessageBox( "ERROR", "The environment variable 'RENDER_SRC_FOLDER' is not defined... Cannot proceed.", "OK", None, None )
24  FBApplication().FileExit()
25 
26  # And the place where to store the rendered scene is indicated by
27  # the environment variable 'RENDER_DST_FOLDER'.
28  lRenderDstFolder = None
29  if environ.has_key( 'RENDER_DST_FOLDER' ):
30  lRenderDstFolder = environ['RENDER_DST_FOLDER']
31  else:
32  # We have failed to find the folder where to save the renders...
33  # The environment variable is probably not defined.
34  FBMessageBox( "ERROR", "The environment variable 'RENDER_DST_FOLDER' is not defined... Cannot proceed", "OK", None, None )
35  FBApplication().FileExit()
36 
37  # The file format of the render is indicated by 'RENDER_FILE_FORMAT'.
38  lRenderFileFormat = None
39  if environ.has_key( 'RENDER_FILE_FORMAT' ):
40  lRenderFileFormat = environ['RENDER_FILE_FORMAT']
41  else:
42  # We have failed to find the folder where to save the renders...
43  # The environment variable is probably not defined.
44  FBMessageBox( "ERROR", "The environment variable 'RENDER_FILE_FORMAT' is not defined... Cannot proceed", "OK", None, None )
45  FBApplication().FileExit()
46 
47  # If the variable is defined, let's process the files
48  if lRenderSrcFolder and lRenderDstFolder and lRenderFileFormat:
49 
50  # Make sure that the destination folder exists...
51  # Ideally it should be created automatically.
52  listdir( lRenderDstFolder )
53 
54  # Obtain the list of files in the folder, and sort them alphabetically.
55  lFileList = listdir( lRenderSrcFolder )
56 
57  # We sort the list
58  lFileList.sort()
59 
60  # Iterate in the list
61  for lFileName in lFileList:
62 
63  # Ensure that we handle only FBX files.
64  if lFileName.endswith('.fbx'):
65 
66  # Create an application object.
67  lApp = FBApplication()
68 
69  # We need the full path of the FBX file to load it.
70  lSrcFileName = os.path.join(lRenderSrcFolder, lFileName)
71 
72  # We also need the full path of the output file.
73  lDstFileName = os.path.join(lRenderDstFolder, lFileName.replace( 'fbx', lRenderFileFormat ))
74 
75  # Open the file in the application.
76  lApp.FileOpen( lSrcFileName )
77 
78  # Get the default rendering options, which are saved in the FBX file.
79  lOptions = FBVideoGrabber().GetOptions()
80 
81  # Set VideoCodec Option:
82  VideoManager = FBVideoCodecManager()
83  VideoManager.VideoCodecMode = FBVideoCodecMode.FBVideoCodecUncompressed
84 
85  # Set the name of the rendered file.
86  lOptions.OutputFileName = lDstFileName
87 
88  # Render audio.
89  lOptions.RenderAudio = True
90  lOptions.AudioRenderFormat = FBAudioFmt_AppendFormat(0, 2, 16, 44100)
91 
92  # Only windows supports mov.
93  if lRenderFileFormat == '.mov' and os.name != 'nt':
94  lOptions.BitsPerPixel = FBVideoRenderDepth.FBVideoRender32Bits
95 
96  # Do the render. This will always be done in uncompressed mode.
97  lApp.FileRender( lOptions )
98 
99  # Clear the scene.
100  lApp.FileNew()
101 
102 except Exception, e:
103  # Unkown error encountered... Maybe from the 'listdir' call failing...
104  FBMessageBox( "ERROR", "Unknown error encountered. Aborting! " + str(e), "OK", None, None )
105 
106 # Now that we are done, we just exit the application.
107 FBApplication().FileExit()