WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Thread Tools Rate Thread Display Modes
Old 10-23-2007, 02:17 AM
jaclyn.bill jaclyn.bill is offline
Join Date: Oct 2007
Posts: 42
text output

Dear all,

I'm using the script below in order to move a ball around the screen and save data to two text files. The first records keyboard and time responses, the second records x,y,z of the ball across time.

Whilst the keyboard responses are recording to text fine and the x,y,z data is visible in the output screen of vizard, only the last x,y,z co-ord is saving to the text file. Is there a simple command I'm missing, or is the issue because I'm trying to write two files at once?

I would be grateful for some help on this.

Thank you.


####code for recording the data.
def RecordTimer(pos, currentTime):
	file = open ('time.txt','w')
	currentTime = viz.tick()
	pos = gball.getPosition()
	save = '%d %.4f %.4f %.4f'%(currentTime * 1000,pos[0],pos[1],pos[2])###original
	print save

def RunSimulation():
	#Repeat simulation indefinitely
	while True:
		#Wait for left mouse button to be pressed
		yield viztask.waitMouseDown(viz.MOUSEBUTTON_LEFT)
		#Place ball in starting position
		#Start timer to record ball position
		recordTimer = vizact.onupdate(0,RecordTimer)
		#Wait for ball to finish move action
		yield viztask.runAction(gball.add(motionseq))
		#Stop timer

import vizinfo

# Opens file 'response.txt' in write mode
file = open('response.txt', 'w')
# Define a function that saves data
def SaveData(currenttime, key):
    out = str(currenttime) + '\t' + key + '\n'
    print out
# Define a function that is called every time a keyboard button is pressed
def mykeyboard(key):          
    # Calls the function SaveData and hands it the current time and key

# Defines a callback for keyboard events
viz.callback(viz.KEYBOARD_EVENT, mykeyboard)
Reply With Quote
Old 10-23-2007, 10:46 AM
farshizzo farshizzo is offline
WorldViz Team Member
Join Date: Mar 2003
Posts: 2,849
You are opening the file during every iteration of your timer, this is very inefficient. Also, whenever you open a file in write mode (i.e. 'w') it will overwrite the current contents of the file. What you should do is open the file once, and just write to it inside your timer. Example:
logFile = open ('time.txt','w')
def RecordTimer(pos, currentTime):
	currentTime = viz.tick()
	pos = gball.getPosition()
	save = '%d %.4f %.4f %.4f'%(currentTime * 1000,pos[0],pos[1],pos[2])###original
	print save
Also, if you want to append data to a file, you can open it using the 'a' flag, instead of the 'w' flag.
Reply With Quote
Old 10-24-2007, 06:37 AM
jaclyn.bill jaclyn.bill is offline
Join Date: Oct 2007
Posts: 42
That's great, thanks for your help.

Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -7. The time now is 10:38 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC