#!BPY

"""
Name: 'Render Cameras'
Blender: 239
Group: 'Render'
Tooltip: 'Render views from cameras to current output directory'
"""

__author__ = "Mitch Hughes (lobo_nz)"
__url__ = ("blender", "Author's homepage, http://blender.formworks.co.nz")
__version__ = "0.1"

__bpydoc__ = """\
This script render views from all cameras to the current output directory.

Usage:
    
Add the script to your blender/scripts directory
Execute this script from the "Scripts->Render" menu and choose "Render Cameras",
click the "Render Cameras" to render views from all cameras.
"""

# $Id: render_cameras.py,v 0.1 2005/11/26 12:26:10$
#
# --------------------------------------------------------------------------
# Render Cameras by Mitch Hughes (AKA lobo_nz)
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------

import sys
import Blender
from Blender import *
import string

################### Get some variables ##############
sc=Scene.GetCurrent()
context = sc.getRenderingContext()

#get start and end frames to re-instate later
#We have to use renderAnim() as there dosn't seem to be a way to
#save a single render to disk using the python API :/ so we animate a single frame
orig_startFrame = context.startFrame()
orig_endFrame = context.endFrame()
#Also we reinstate the renderpath as we mess that up too
orig_renderPath = context.getRenderPath()
#####################################################

def getCameras():
	allCameras = []
	allObjects = Blender.Object.Get()
	thisList = []
	for thisObj in allObjects:
		#print type(thisObj.getData())
		if type(thisObj.getData()) == Types.CameraType:
			thisList.append(thisObj)
	return thisList

def render():
	context.startFrame(Blender.Get('curframe'))
	context.endFrame(Blender.Get('curframe'))
	
	for cam in (getCameras()):
	
		print "Rendering view from " + cam.name
		
		#By addin the camera name onto the the render path
		#and not having a / on the end of it we get the camera
		#name prepended to the frame number which is nice :)
		context.setRenderPath(orig_renderPath + cam.name + "_")
		
		sc.setCurrentCamera(cam)
		context.renderAnim()
	
	context.startFrame(orig_startFrame)
	context.endFrame(orig_endFrame)
	context.setRenderPath(orig_renderPath)

	Blender.Scene.Render.CloseRenderWindow()
	print "Done"
	
def gui():
	Blender.BGL.glClearColor(0.6, 0.6, 0.6, 0.0)
	Blender.BGL.glClear(Blender.BGL.GL_COLOR_BUFFER_BIT)
	Blender.Draw.Button("Render Cameras", 1, 10, 10, 180, 20)
	Blender.Draw.Button("Exit", 2, 200, 10, 60, 20)

def event(evt, mode):
   if evt == Blender.Draw.ESCKEY: Blender.Draw.Exit()

def bevent(evt):
	if evt == 1:
 		render()
	if evt == 2:
		Blender.Draw.Exit()

Blender.Draw.Register(gui,event,bevent) 
