#!BPY

#
# RenderWorld SubmitTool (C) 2004 Futureware: Gregor Mueckl, Philipp Guehring
#

# TODO: optimize generation of animation job files
# TODO: find a way to include all required textures in job

# TODO for HTTP upload part:
# - fix: hangs if userid/password is wrong
# - check if file is uploaded succesfully
# - use mimelib to encode the file (instead of using urlencode)



"""
Name: 'RenderWorldSubmitTool'
Blender: 233
Group: 'Export'
Tip: 'Tool for submitting render jobs to RenderWorld'
"""

import Blender
import math
import urllib

default_email = "your@email.com"

bEMail = Blender.Draw.Create(default_email)

user='render'
password='world'
addfileurl='http://renderworld.futureware.at/cgi-bin/newproject'
useragent="RenderWorldBlenderClient/0.1"
filetitle='a nice test picture'
filename='renderworldclient.blend'


class MyUrlOpener(urllib.FancyURLopener):
	def prompt_user_passwd(self, host, realm):
		return (user,password)
	def __init__(self, *args):
		self.version = useragent
		urllib.FancyURLopener.__init__(self, *args)


def doRender(context):
	#context.getRenderPath()
	#filename=("%04d" % context.currentFrame()) + ".blend"
	#print filename
	Blender.Save(filename,True)
	urllib._urlopener = MyUrlOpener()
	
        # read the contents of filename into filebody
	f=open(filename, 'rb')
	filebody=f.read()
	f.close
	
	# urlencode the id, title and file
	params = urllib.urlencode({'email': bEMail.val,
	'id': filename,
	'title':filetitle,
	'submitfile':filebody})
	
	# send the file to RenderWorld
	f=urllib.urlopen(addfileurl, params).read()

	print f

def renderFrame(progressText, progressPosition):
	scene=Blender.Scene.GetCurrent()
	context=scene.getRenderingContext()
	renderPath=context.getRenderPath()
	startFrame=context.startFrame()
	endFrame=context.endFrame()
	context.startFrame(context.currentFrame())
	context.endFrame(context.currentFrame())
	camera=scene.getCurrentCamera()

	baseName=renderPath + "-" + camera.getName() + "-"

	Blender.Window.DrawProgressBar(progressPosition,progressText)
	context.setRenderPath(baseName)
	doRender(context)

	context.setRenderPath(renderPath)
	context.startFrame(startFrame)
	context.endFrame(endFrame)

def renderSingle():
	renderFrame("single",0)
	Blender.Window.DrawProgressBar(1.0,"done")

def renderAnimation():
	scene=Blender.Scene.GetCurrent()
	context=scene.getRenderingContext()
	startFrame=context.startFrame()
	endFrame=context.endFrame()
        doRender(context)
	#origCurrentFrame=context.currentFrame()
	#progressDelta=1/(endFrame-startFrame+1)
	#for i in range(endFrame-startFrame+1):
		#context.currentFrame(startFrame+i)
		#renderFrame("frame " + str(i),i*progressDelta)
	#context.currentFrame(origCurrentFrame)
	Blender.Window.DrawProgressBar(1.0,"done")

def handleEvent(event,value):
	#drawGui()
	pass

def handleButtonEvent(event):
	if event==1:
		Blender.Draw.Exit()
		return
	elif event==10:
		renderSingle()
	elif event==11:
		renderAnimation()

def drawGui():
	global bEMail
	#Blender.Draw.PushButton("render single",10,10,70,120,25,"render a single frame")
	Blender.Draw.PushButton("render animation",11,10,40,120,25,"render the whole animation")
	bEMail=Blender.Draw.String("EMail: ",12,10,110,220,25,bEMail.val,22,"your email address for confirmation")
	Blender.Draw.PushButton("exit",1,10,10,120,25)

Blender.Draw.Register(drawGui,handleEvent,handleButtonEvent)


