WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 10-23-2007, 03:17 AM
jaclyn.bill jaclyn.bill is offline
Member
 
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.

Jac

Code:
####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
	file.write(save)
	file.flush()
	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
		gball.add(motionseq)
		
		#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
		vizact.removeEvent(recordTimer)
	
viztask.schedule(RunSimulation())


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'
    file.write(out)
    file.flush()
    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
    SaveData(viz.tick(),key)

# Defines a callback for keyboard events
viz.callback(viz.KEYBOARD_EVENT, mykeyboard)
Reply With Quote
  #2  
Old 10-23-2007, 11: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:
Code:
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
	logFile.write(save)
	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
  #3  
Old 10-24-2007, 07:37 AM
jaclyn.bill jaclyn.bill is offline
Member
 
Join Date: Oct 2007
Posts: 42
That's great, thanks for your help.

Jac
Reply With Quote
Reply

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 08:08 PM.


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