Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a .txt and loading .cob files based on that input?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
HI,

I've never written a Python script before. I've decided I want to write one for a TS scene I'm making. What I want to do is to load some objects (.cob files) into the scene based upon the data read in from a .txt file. The .txt file will be a series of either "1" or "0" to indicate whether or not the object should be loaded into the scene. I've been researching how to do this and here's what I've come up with, can I get somebody to verfiy my code and/or offer suggestions as to how to improve?

I think the script will look something like this:

def ontimestarted():
shipfile = open('c:\program files\SSI\shipgroup.txt','r')
ship1 = shipfile.readline()
if ship1 == 1
queenmary = doc.LoadObject("queenM.cob")
queenmary.Position = (50,50,50)
ship2 = shipfile.readline()
if ship2 == 1
Titanic = doc.LoadObject("Titanic.cob")
Titanic.Position = (100,100,100)
# etc for each needed model
shipfile.close()
doc.PlayAnim()

Thanks in advance for any help that can be given...
 
How about this ?


Create the shipgroup.txt file like this:

--%--cut here----
1,queenmary,queenM.cob,50,50,50
1,Titanic,Titanic.cob,100,100,100
0,oldShip,oldFile.cob,12,12,12
--%--cut here----

Each line of this file is:
load(yes/no), ship name, filename, x, y, z



Then, ship.py:

--%--cut here----
shipslist = open('shipgroup.txt','rb').read().split('\r\n')
myships = {}
for ship in shipslist:
try:
(load,shipname,filename,x,y,z) = ship.split(',')
if load:
myships[shipname] = doc.LoadObject(filename)
myships[shipname].Position = (x,y,z)
except:
pass
doc.PlayAnim()
--%--cut here----

myships is a dictionnary. You can use it to access the different boats you have loaded.

If you want to access a ship, you can do:
print myships['queenmary']

For example:
print myships['queenmary'].Position

To get the list of ships which are loaded, just do:
print myships.keys()


The 'try' statement will ensure the program does not break even if the configuration file is bad, or if .cob files are missing.

You can put as many ships as you want in the configuration file without touching the Python code.
Nice, isn't it ?


If you need detailed explanation of this script, just ask ! :)
 
Hi sebsauvage,

Thanks for your reply, if it's not too much trouble I would like a more detailed explanation of this script. As I mentioned I've never worked with Python before and I could use more info about your "ship.py:" code. I understand why you setup the shipgroup.txt file as you did. I've just started to try and learn some Python within the last few days so I could possibly write this little routine. Could you also possibly explain what a ".py" file is? (sorry to sound dumb).
Basically it's an attempt to write a "bridge" between the application that will play the .avi file I'm making in Truespace and the .avi file itself. The main application is a computer game and it is written in another language. I will modify the "shipgroup.txt" "1's" and "0's" as appropriate in my main application--then hopefully have the Python script in my animation read that .txt and "adjust" which ships to display in the .avi animation accordingly...so my animation is not simply static but more realistic depending on game conditions. Thanks again!
 
I whould strongly suggest reading a few Python tutorials.

There are plenty of very good Python tutorials here:
(seek the "Learning Python" section).


I don't think it's worth explaining the whole script until you understand some Python basics (especially lists/dictionnaries/tuples data types, which are used here).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top