BSP entities in Python

A forum to store posts deemed exceptionally wise and useful
Post Reply
BongoMan

BSP entities in Python

Post by BongoMan »

Hey!
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()

afecelis
Admin
Posts: 3075
Joined: Sun Feb 22, 2004 10:44 pm
Location: Colombia
Contact:

Post by afecelis »

damn! I wish I knew Python!
Image
bal
Posts: 829
Joined: Fri Jun 18, 2004 5:19 pm
Location: Geluwe, Belgium

Post by bal »

Damn! I wish Irrlicht had a Python script-reader so this was usefull!

Or we could just "translate" it :).
General Tools List
General FAQ
System: AMD Barton 2600+, 512MB, 9600XT 256MB, WinXP + FC3
afecelis
Admin
Posts: 3075
Joined: Sun Feb 22, 2004 10:44 pm
Location: Colombia
Contact:

Post by afecelis »

damn! that was funny!

:lol:
Image
BongoMan

Post by BongoMan »

Its very simple:

open the file.
Move to byte no 8
Read two 32 bit ints
The first is the start point of the entity string
The second is the length of the string
Read the string

In the string the entities is separated by }\n{
Every entity can have an unknown number of parameters, but I think there is always atleast two, Class_name and Origin. The parameters are separated by lines (\n) and the key and the value is separated by " ".

Just read the string out and take alook at it and it become pretty obvious.
ErMurazor
Posts: 37
Joined: Sat Dec 13, 2003 2:00 pm
Location: Sweden
Contact:

Post by ErMurazor »

Nice to see that more people is using Python :)
Project YAMMO and
Venom for Irrlicht (Python Irrlicht language binding)
http://www.stenhard.net
BongoMan

Post by BongoMan »

Yeah, keep the venom comming. It rocks. You get so much power at your fingertips without having to touch C/C++.

Are you planning to use Q3 bsps for YAMMO by the way?
BongoMan

Post by BongoMan »

Figured Id better post this link here too: Its an unofficial spec of the Q3 bsp format:
http://graphics.stanford.edu/~kekoa/q3/

So, if you want to get the lightvol data you start reading on 8 +(16*8) insted of 8 like in the example above, but you still get the two ints etc.
blackbirdXXX
Posts: 20
Joined: Tue Sep 21, 2004 9:08 pm

Post by blackbirdXXX »

Welcome in the circle of python developers :D
Venom is very cool.
Only a function reference would be cool.
Post Reply