WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Help Printing to Different files (https://forum.worldviz.com/showthread.php?t=5192)

BSUGeek 10-14-2014 07:07 AM

Help Printing to Different files
 
My scene is with an avatar out of view and he's walking around the room. When he walks into view I want my code to print his coordinates once to an xml file. When he's in view again I want to print his coordinates once to a separate xml file...etc, etc. :confused:

I'm having trouble getting the code to print to separate xml files while he's in view. any help would be greatly appreciated! below is my snippet of code.

Code:

#Checks If Avatar is visible
def dostuff():
        num = 1
        while(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.
                        dataTag1 = ''
                        XPosition = 'x position:  ' + str(round(x_pos))
                        YPosition = '  y position:  ' + str(y_pos)
                        ZPosition = '  z position:  ' + str(z_pos)
                        dataTag2 = '
\n'
                        dataTime1 = '\n'
                        question_data = open('VRProject'+str(num)+'.xml','w')
                        num = num + 1
                       
                        #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()
       
vizact.ontimer(1,dostuff)


Frank Verberne 10-14-2014 09:37 AM

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.

BSUGeek 10-14-2014 01:15 PM

Quote:

Originally Posted by Frank Verberne (Post 16648)
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... :confused:

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 = '\n'
                dataTag1 = ''
                XPosition = 'x position:  ' + str(round(x_pos))
                YPosition = '  y position:  ' + str(y_pos)
                ZPosition = '  z position:  ' + str(z_pos)
                dataTag2 = '
\n'
                dataTime1 = '\n'
                enddeclaration = '
'
                       
                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)



All times are GMT -7. The time now is 11:26 AM.

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