Quake style BPython console in blender WIP. Py Included.

Heres a blender console that runs in the blender script window and dosent hang th erest of blender.

Its works on a basic level- a little buggy but wont do any thing bad to your blender file.

Some problems I cant find a solution are-

Since the blender redraw calls the command line draw/text ently/cmd exec() functions, all the variables go out of scope each time a command is executed.
I have tried to make uses set vars global but they need to be set outside the function previously for this to work.

so…
>> x = 1; print x
works

but …
>> x = 1
>> print x

raises an error, because x looses scope.

Anyway have a play, its kinda fun.


#!BPY
import Blender
from Blender import *

import sys
import os

filename = 'cmd'

# tOUCH THE FILE
file = open(filename, 'w')
file.close()

def run(py):
	# OPEN A FILE TO PUT THE STDOUT IN
	sys.stdout.flush() # Get rid of whatever came before
	file = open(filename, 'w')
	std=sys.stdout
	sys.stdout=file

	try:
		exec(py)
	except:
		print 'error, some problem with your command,
could anybody who knows how to catch the output from a failed try: satement
please email me - [email protected]'
	
	# Close file\
	sys.stdout=std
	file.close()

	# Open the file to read the lines to the buffer.
	file = open(filename, 'r')
	for line in file.readlines():
		cmdBuffer.append(line)
	cmdBuffer.append(' ')
	sys.stdout=std
	file.close()
	
	# DIRTY HACH

global x

cmdBuffer = [' '] # A history of commands
shift = 0 # Bool used to work out if were using caps.
enter = 0
cursor = -1 # How far back from the first letter are we? - in cmd tha tis.
prompt = '>> '
def cmd():
	global cmd
	global shift
	global cursor
	#------------------------------------------------------------------------------#
	#                             event handling code                              #
	#------------------------------------------------------------------------------#
	def handle_event(evt, val):
		def insCh(ch): # Instert a char
			global cmdBuffer
			global cursor
			# Later account for a cursor variable
			cmdBuffer[-1] = cmdBuffer[-1][:cursor] + ch + cmdBuffer[-1][cursor:]

		if (evt == Draw.ESCKEY and not val):
		
			Draw.Exit()
		global cmdBuffer
		global shift
		global enter
		global cursor
		
		if (evt == Draw.AKEY and val and shift): insCh('A')
		elif (evt == Draw.AKEY and val and not shift): insCh('a')
		elif (evt == Draw.BKEY and val and shift): insCh('B')
		elif (evt == Draw.BKEY and val and not shift): insCh('b')
		elif (evt == Draw.CKEY and val and shift): insCh('C')
		elif (evt == Draw.CKEY and val and not shift): insCh('c')
		elif (evt == Draw.DKEY and val and shift): insCh('D')
		elif (evt == Draw.DKEY and val and not shift): insCh('d')
		elif (evt == Draw.EKEY and val and shift): insCh('E')
		elif (evt == Draw.EKEY and val and not shift): insCh('e')
		elif (evt == Draw.FKEY and val and shift): insCh('F')
		elif (evt == Draw.FKEY and val and not shift): insCh('f')
		elif (evt == Draw.GKEY and val and shift): insCh('G')
		elif (evt == Draw.GKEY and val and not shift): insCh('g')
		elif (evt == Draw.HKEY and val and shift): insCh('H')
		elif (evt == Draw.HKEY and val and not shift): insCh('h')
		elif (evt == Draw.IKEY and val and shift): insCh('I')
		elif (evt == Draw.IKEY and val and not shift): insCh('i')
		elif (evt == Draw.JKEY and val and shift): insCh('J')
		elif (evt == Draw.JKEY and val and not shift): insCh('j')
		elif (evt == Draw.KKEY and val and shift): insCh('K')
		elif (evt == Draw.KKEY and val and not shift): insCh('j')
		elif (evt == Draw.LKEY and val and shift): insCh('L')
		elif (evt == Draw.LKEY and val and not shift): insCh('l')
		elif (evt == Draw.MKEY and val and shift): insCh('M')
		elif (evt == Draw.MKEY and val and not shift): insCh('m')
		elif (evt == Draw.NKEY and val and shift): insCh('N')
		elif (evt == Draw.NKEY and val and not shift): insCh('n')
		elif (evt == Draw.OKEY and val and shift): insCh('O')
		elif (evt == Draw.OKEY and val and not shift): insCh('o')
		elif (evt == Draw.PKEY and val and shift): insCh('P')
		elif (evt == Draw.PKEY and val and not shift): insCh('p')
		elif (evt == Draw.QKEY and val and shift): insCh('Q')
		elif (evt == Draw.QKEY and val and not shift): insCh('q')
		elif (evt == Draw.RKEY and val and shift): insCh('R')
		elif (evt == Draw.RKEY and val and not shift): insCh('r')
		elif (evt == Draw.SKEY and val and shift): insCh('S')
		elif (evt == Draw.SKEY and val and not shift): insCh('s')
		elif (evt == Draw.TKEY and val and shift): insCh('T')
		elif (evt == Draw.TKEY and val and not shift): insCh('t')
		elif (evt == Draw.UKEY and val and shift): insCh('U')
		elif (evt == Draw.UKEY and val and not shift): insCh('u')
		elif (evt == Draw.VKEY and val and shift): insCh('V')
		elif (evt == Draw.VKEY and val and not shift): insCh('v')
		elif (evt == Draw.WKEY and val and shift): insCh('W')
		elif (evt == Draw.WKEY and val and not shift): insCh('w')
		elif (evt == Draw.XKEY and val and shift): insCh('X')
		elif (evt == Draw.XKEY and val and not shift): insCh('x')
		elif (evt == Draw.YKEY and val and shift): insCh('Y')
		elif (evt == Draw.YKEY and val and not shift): insCh('y')
		elif (evt == Draw.ZKEY and val and shift): insCh('Z')
		elif (evt == Draw.ZKEY and val and not shift): insCh('z')
		

		elif (evt == Draw.ONEKEY and val and shift): insCh('!')
		elif (evt == Draw.ONEKEY and val and not shift): insCh('1')
		elif (evt == Draw.TWOKEY and val and shift): insCh('@')
		elif (evt == Draw.TWOKEY and val and not shift): insCh('2')
		elif (evt == Draw.THREEKEY and val and shift): insCh('#')
		elif (evt == Draw.THREEKEY and val and not shift): insCh('3')			
		elif (evt == Draw.FOURKEY and val and shift): insCh('$')
		elif (evt == Draw.FOURKEY and val and not shift): insCh('4')	
		elif (evt == Draw.FIVEKEY and val and shift): insCh('%')
		elif (evt == Draw.FIVEKEY and val and not shift): insCh('5')	
		elif (evt == Draw.SIXKEY and val and shift): insCh('^')
		elif (evt == Draw.SIXKEY and val and not shift): insCh('6')	
		elif (evt == Draw.SEVENKEY and val and shift): insCh('&')
		elif (evt == Draw.SEVENKEY and val and not shift): insCh('7')	
		elif (evt == Draw.EIGHTKEY and val and shift): insCh('*')
		elif (evt == Draw.EIGHTKEY and val and not shift): insCh('8')	
		elif (evt == Draw.NINEKEY and val and shift): insCh('(')
		elif (evt == Draw.NINEKEY and val and not shift): insCh('9')				
		elif (evt == Draw.ZEROKEY and val and shift): insCh(')')
		elif (evt == Draw.ZEROKEY and val and not shift): insCh('0')
			
		elif (evt == Draw.COMMAKEY and val and shift): insCh('<')
		elif (evt == Draw.COMMAKEY and val and not shift): insCh(",")
		
		elif (evt == Draw.PERIODKEY and val and shift): insCh('>')
		elif (evt == Draw.PERIODKEY and val and not shift): insCh(".")
		
		elif (evt == Draw.QUOTEKEY and val and shift): insCh('"')
		elif (evt == Draw.QUOTEKEY and val and not shift): insCh("'")
		
		elif (evt == Draw.LEFTBRACKETKEY and val and not shift): insCh('[')
		elif (evt == Draw.LEFTBRACKETKEY and val and shift): insCh('{')
		elif (evt == Draw.RIGHTBRACKETKEY and val and not shift): insCh(']')			
		elif (evt == Draw.RIGHTBRACKETKEY and val and shift): insCh('}')			


		elif (evt == Draw.MINUSKEY and val and shift): insCh("_")		
		elif (evt == Draw.MINUSKEY and val and not shift): insCh("-")	

		elif (evt == Draw.EQUALKEY and val and shift): insCh("+")		
		elif (evt == Draw.EQUALKEY and val and not shift): insCh("=")		
		
		# SPACE
		elif (evt == Draw.SPACEKEY and val): insCh(' ')

		elif (evt == Draw.LEFTARROWKEY and val): cursor +=1
		elif (evt == Draw.RIGHTARROWKEY and val): cursor -=1

		elif (evt == Draw.BACKSPACEKEY and val): cmdBuffer[-1] = cmdBuffer[-1][:cursor-1] + cmdBuffer[-1][cursor:]
		# FIXDEL
		#elif (evt == Draw.DELKEY and val): cmd = cmd[:cursor] + cmd[cursor-2:]

		# HANDLE CAPITALS/ SHIFT KEY
		elif (evt == Draw.LEFTSHIFTKEY and not val): shift = 0		
		elif (evt == Draw.RIGHTSHIFTKEY and not val): shift = 0		

		elif (evt == Draw.LEFTSHIFTKEY and val): shift = 1
		elif (evt == Draw.RIGHTSHIFTKEY and val): shift = 1




		elif (evt == Draw.RETKEY and not val): enter = 0		

		elif (evt == Draw.RETKEY and val): enter = 1
		
		Draw.Redraw()

	

	# This recieves the event index, call a function from here depending on the event.
	def handle_button_event(evt):
		print evt

	
	def draw_gui():
		global cmdBuffer
		global enter
		lineHeight = 20
		# find the width of the widest button
		fontsize = "normal"
		# Setup
		

		# Catch the STDOUT and Comment it, then add it to the 

		# Draw the set of cammands to the buffer
		Ln = len(cmdBuffer)
		while Ln != 0:
			
			BGL.glRasterPos2i(0, lineHeight*Ln)
			Draw.Text(prompt + cmdBuffer[-Ln])
			Ln -= 1
		

		if enter:
			run(cmdBuffer[-1])
		
	#------------------------------------------------------------------------------#
	#                    register the event handling code, GUI                     #
	#------------------------------------------------------------------------------#
	Draw.Register(draw_gui, handle_event, handle_button_event) 

cmd()