View Single Post
  #3  
Old 10-14-2014, 01:15 PM
BSUGeek BSUGeek is offline
Member
 
Join Date: Oct 2014
Posts: 23
Quote:
Originally Posted by Frank Verberne View Post
Not sure what seem to be the problem, but I noticed that you want to write declaration to the question_data file. However, I don't see any variable called declaration. Furthermore, the write command is capable of writing multiple lines at the same time, so it would be more efficient to create one string, and writing that one string with a write statement.

Could you clarify what exactly seems to be the problem? Have you tried writing simple information to a .txt file instead of an .xml? I know they are basically the same, but I don't know of the write command handles .xml files well. Furthermore, the program could get stuck in a loop with the while command. Replace the while loop with an if statement. If that does not solve your problem, please specify your problem.
Here's my full code. Right now when it runs it's making separate xml files for every second the avatar is in view. What i'm trying to do is everytime the avatar walks into view I want to write the coordinates to separate xml files but only once each time he's in view. and If a new avatar comes into view I want to it make another xml file. if that makes any sense...

Code:
import viz
import vizact
import vizshape
import vizproximity
import viztask

viz.setMultiSample(4)
viz.fov(60)
viz.go()

viz.MainView.move([0,0,-1])

#Create skylight
viz.MainView.getHeadLight().disable()
sky_light = viz.addLight(euler=(0,90,0))
sky_light.position(0,0,-1,0)
sky_light.color(viz.WHITE)
sky_light.ambient([0.9,0.9,1])

#Add the gallery model
gallery = viz.addChild('gallery.osgb')
#floor = viz.add('ground.osgb')

#Mark the time that the program was started.
start_time = viz.tick()

avatar = viz.addAvatar('vcc_male2.cfg',pos=[-2,0,0])
avatar.state(1)
#Create static drop shadow to avatar
shadow_texture = viz.addTexture('shadow.png')
shadow = vizshape.addQuad(parent=avatar,axis=vizshape.AXIS_Y)
shadow.texture(shadow_texture)
shadow.zoffset()

#Move avatar around the room with a sequence of walk, turn, and wait actions
#Create action to wait 10-15 seconds
RandomWait = vizact.waittime(vizact.randfloat(5,10))

#A list of painting locations
avatarMove = [[-3.7,6.5,270],[0,8,0],[3.7,6.5,90],[3.7,2.6,90],[3.7,1,130]]
actions = []
for loc in avatarMove:
    #Add an action to walk to the next painting, turn towards it, and wait a few seconds
    actions.append(vizact.method.playsound('footsteps.wav',viz.LOOP))
    actions.append(vizact.walkTo([loc[0],0,loc[1]],turnSpeed=250.0))
    actions.append(vizact.method.playsound('footsteps.wav',viz.STOP))
    actions.append(vizact.turn(loc[2],250.0))
    actions.append(RandomWait)

#Repeat the sequence of actions forever
avatar.addAction(vizact.sequence(actions,viz.FOREVER))

#Open a file to collect the data on this subject.
#The 'a' here means that we will be appending our new
#data to the existing data in this file.

#Checks If Avatar is visible
def dostuff():
	if (viz.MainWindow.isCulled(avatar, eye = viz.LEFT_EYE)==0):
		elapsed_time = viz.tick() - start_time
		x_pos = avatar.getPosition()[0]
		y_pos = avatar.getPosition()[1]
		z_pos = avatar.getPosition()[2]
				
		#Make a string out of the data. 
		declaration = '<inform sender = "VR" receiver = "Anamoly Alert System" Format = "xml" reply_with = "Anamoly Detected!" in_response_to = "Anamoly Alert" >\n'
		dataTag1 = '<position>'
		XPosition = 'x position:  ' + str(round(x_pos))
		YPosition = '  y position:  ' + str(y_pos)
		ZPosition = '  z position:  ' + str(z_pos)
		dataTag2 = '</position>\n'
		dataTime1 = '<time>'
		time = str(round(elapsed_time))
		dataTime2 = '</time>\n'
		enddeclaration = '</inform>'
			
		print 'I See You'
		num = viz.tick()
		question_data = open('VRProject'+str(round(num))+'.xml','a')
				
		#Write the data to our file.
		question_data.write(declaration)
		question_data.write(dataTag1) 
		question_data.write(XPosition) 
		question_data.write(YPosition) 
		question_data.write(ZPosition) 
		question_data.write(dataTag2) 
		question_data.write(dataTime1) 
		question_data.write(time) 
		question_data.write(dataTime2)
		question_data.write(enddeclaration)
						
		#Flush the internal buffer.
		question_data.close()
		#while(viz.MainWindow.isCulled(avatar, eye = viz.LEFT_EYE)==0):

vizact.ontimer(1,dostuff)
Reply With Quote