Script - Render in background

This is a script made for render an image or an animation in background in the gnome-terminal.
I don’t know if a script like that have already be made but I was needed something like that.
This allow you to :
- continue work in Blender when you render.
- rendering more fast (it should rendering more fast but I didn’t do any test…)
- don’t think and search the commands and arguments to put in the terminal for launch a render in background (it why I made that script)

Blender can be closed, since the gnome-terminal is still open the render will continue.
So for this version of this script, gnome-terminal must be installed.
The script work on my Ubuntu Intrepid Ibex 64 bits, i don’t know at all if it will work on other OS.
If you know how to make it work with any terminal and any os, dont hesitate to do the changes.

#!BPY 
""" 
Name: 'Render in Background' 
Blender: 248 
Group: 'Render' 
Tooltip: 'Make render in background (work with gnome-terminal)' 
""" 
 
__author__ = "Orfvz" 
__url__ = ['http://www.orfvz.com', 'http://blenderclan.tuxfamily.org', 'blenderartists.org'] 
__version__ = "0.1" 
 
__bpydoc__ = """\ 
 
This script open a the gnome-terminal for render an image or an animation in background. 
This allow you to : 
    - continue work in Blender when you render. 
    - rendering more fast (it should rendering more fast, i didn't do any test...) 
    - don't think and search the commands and arguments to put in the terminal for launch a render in background (it why i made that script) 
 
Blender can be closed, since the gnome-terminal is still open the render will continue. 
 
So for this version of this script, gnome-terminal must be installed. 
Can you help me make it work on any terminal ? 
 
The script work on my Ubuntu Intrepid Ibex 64 bits, i don't know at all if it will work on other os. 
Can you help me make it work on any os ? 
 
If your can improve it, dont hesitate. 
 
To do : 
 - Universal terminal 
 - Make it work on all OS 
 - Auto-center gui 
 
 
 
""" 
# Created with The_Nerd's GUI creator V2.500000 
# 
# ***** BEGIN GPL LICENSE BLOCK ***** 
# 
# -------------------------------------------------------------------------- 
#  
# RENDER BACKGROUND v 0.1 by Orfvz (http://www.orfvz.com, [email protected]) 
# -------------------------------------------------------------------------- 
# 
# ***** 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 ***** 
# -------------------------------------------------------------------------- 
 
# To Do 
#--------------------------------------------- 
# - Universal terminal 
# - Make it work on all OS 
# - Auto-center gui 
 
###################################################### 
# Importing modules 
###################################################### 
 
import Blender, os 
from Blender import * 
from Blender.BGL import * 
 
 
###################################################### 
# GUI - Made with The_Nerd's GUI creator V2.500000 
###################################################### 
 
EVENT_render_b = 10 
EVENT_anim_b = 11 
Object_render_b = Draw.Create(0.0) 
Object_anim_b = Draw.Create(0.0) 
 
 
def draw(): 
    global EVENT_render_b 
    global EVENT_anim_b 
    global Object_render_b 
    global Object_anim_b 
 
    BGL.glClearColor(0.500000, 0.500000, 0.500000, 0.0) 
    BGL.glClear(GL_COLOR_BUFFER_BIT) 
 
    BGL.glColor3f(0.000000, 0.000000, 0.000000) 
    Object_render_b = Draw.Button("RENDER", EVENT_render_b, 8, 8, 76, 36, "Render the current frame in background.") 
    Object_anim_b = Draw.Button("ANIM", EVENT_anim_b, 96, 8, 76, 36, "Render the animation in background.") 
 
 
def event(event, value): 
    _Input_Events_Callback(event, value) 
 
def b_event(event): 
    global EVENT_render_b 
    global EVENT_anim_b 
    global Object_render_b 
    global Object_anim_b 
 
    if event == 0: pass 
    elif event == EVENT_render_b: 
        render_b_Callback() 
    elif event == EVENT_anim_b: 
        anim_b_Callback() 
    Draw.Draw() 
 
Draw.Register(draw, event, b_event) 
 
##---Begin user code--- 
 
def _Input_Events_Callback(Event, Value): 
    if Event == Draw.ESCKEY and not Value: Draw.Exit() 
 
# Render an image 
def render_b_Callback(): 
    lance_commande(0) 
 
# Render an animation 
def anim_b_Callback(): 
    lance_commande(1) 
 
# Send the command to the gnome terminal 
def lance_commande(anim): 
    # Create a .blend file with your current work in your Temporary directory 
    B_BLEND = str(Get('tempdir'))+'/backgroundrender.blend' 
    Save(B_BLEND, 1) 
     
    # Create the command for send to the gnome-terminal. 
    # First case for the command for an animation, second for a simple frame. 
    # The command finish by a 'rm' for delete the .blend file created before. 
    if (anim): 
        START_FRAME = str(Get('staframe')) 
        END_FRAME = str(Get('endframe')) 
        commande = 'setsid gnome-terminal -e "blender -b '+B_BLEND+' -s '+START_FRAME+' -e '+END_FRAME+' -a" && rm '+B_BLEND+' &' 
        # I have tried replace gnome-terminal by $TERM. It opens xterm on my computer, but xterm open a new instance of Blender...so the command cant work 
        # commande = 'setsid $TERM -e "blender -b '+B_BLEND+' -s '+START_FRAME+' -e '+END_FRAME+' -a" && rm '+B_BLEND+' &' 
    else : 
        CURENT_IMAGE= str(Get('curframe')) 
        commande = 'setsid gnome-terminal -e "blender -b '+B_BLEND+' -f '+CURENT_IMAGE+'" && rm '+B_BLEND+' &' 
        # commande = 'setsid $TERM -e "blender -b '+B_BLEND+' -f '+CURENT_IMAGE+'" && rm '+B_BLEND+' &' 
 
    # Send the command to the os 
    os.system(commande)