I wrote this little thing to rip the entity data from Q3 bsp files. Its my first python app, it took me 6 hours including researching the fileformat...and I am very proud.
Code: Select all
###############################################################
# Joakim Karrstrom 2004 #
# #
# Q3 BSP entity arser #
# My First Python Proggie :D #
# #
# Reads a Q3 bsp file and extracts the entity data #
# Makes a list of dictionaries. Every dictionary holds #
# The Key/value pairs for one entity. #
# #
###############################################################
from struct import *
from string import *
f_bsp=open('map.bsp','rb') # <- filename
f_bsp.seek(8)
t_listPos = unpack('ii',f_bsp.read(8))
f_bsp.seek(t_listPos[0])
s_rawString = f_bsp.read(t_listPos[1])
f_bsp.close()
s_rawString = strip(s_rawString,'{}')
L_tmpList = split(s_rawString,'}\n{')
L_Entities =[] # <- da list
for entry in L_tmpList:
D_tmpDict = dict()
L_line = split(entry, '\n')
for line in L_line:
if len(str(line)) > 5:
L_words = split(line, '" "')
s_key = replace(L_words[0],'"','')
s_value = replace(L_words[1],'"','')
D_tmpDict[s_key]=s_value
L_Entities.append(D_tmpDict)
# all done
# the stuff below creates a textfile for easy reading
f_txt=open('output.txt', 'w')
for entry in L_Entities:
for key,value in entry.iteritems():
f_txt.writelines(key + ': ' + value + '\n')
f_txt.writelines('\n*************\n')
f_txt.close()