So after a two month absense from working on this, I'm back at it, and realised I don't know what I'm doing! My code seems to be a mess with extraneous, or at least poorly written, code and the experiment doesn't work how I'd like. Here's my code:
Code:
import viz
import viztask
import random
viz.go()
viz.MainView.move(0,1.4,-13)
#add pathway to bring objects from
viz.res.addPath('C:\Users\Matyi\Desktop\2009 Experiments')
#field of view at 45 degrees
viz.fov(45)
#keep nine positions in an array
pos = [[-2.667, 0.0, -1.524],[1.51892, 0.0, -2.53492],[-0.82042, 0.0, 2.89052],[2.286, 0.0, -0.04572],[-1.17856, 0.0, -1.30302],[1.8415, 0.0, 2.06248],[-0.94996, 0.0, -2.85242],[-3.15722, 0.0, 0.82042],[-0.20828, 0.0, 0.84328]]
#add table, keep it in position, scale
table = viz.add('bluetable.wrl')
table.setPosition(0,0,0)
table.setScale(.5,.5,.5)
table.disable(viz.PICKING)
#add objects and scale
bottle = viz.add('bottle.wrl')
bottle.setScale(.5,.5,.5)
bottle.center(0,0,0)
candle = viz.add('candle.wrl')
candle.setScale(.5,.5,.5)
candle.center(0,0,0)
clamp = viz.add('clamp.wrl')
clamp.setScale(.5,.5,.5)
clamp.center(0,.5,0)
clamp.rotate(0,150,90)
lock = viz.add('lock.wrl')
lock.setScale(.5,.5,.5)
lock.center(0,0,.0)
wood = viz.add('wood.wrl')
wood.setScale(.5,.5,.5)
wood.center(0,0,0)
#make a list with the objects in it
objects = [bottle, candle, clamp, lock, wood]
#disable mouse navigation
viz.mouse(viz.OFF)
def prep():
viz.MainScene.visible(viz.OFF)
#vizact.onkeydown(' ', sceneOn,1)
rand_positions = []
def sceneOn(num):
global rand_positions
positions = [0,1,2,3,4,5,6,7,8]
if num == 1:
#set objects to 5 different random positions
rand_positions = random.sample(positions,5)
objects[0].setPosition(pos[rand_positions[0]])
objects[1].setPosition(pos[rand_positions[1]])
objects[2].setPosition(pos[rand_positions[2]])
objects[3].setPosition(pos[rand_positions[3]])
objects[4].setPosition(pos[rand_positions[4]])
#vizact.ontimer2(3,0,sceneOff)
elif num == 2:
#choose one object to move randomly and leave others in their places
#first get available positions
diff_list = [item for item in positions if not item in rand_positions]
rand_position = random.sample(diff_list,1)[0]
#choose one of the objects
rand_obj = random.randint(0,4)
#set the randomly chosen object to the new random position
objects[rand_obj].setPosition(pos[rand_position])
print 'object ' + str(rand_obj) + ' moved to position ' + str(rand_position)
viz.MainScene.visible(viz.ON)
def sceneOff():
viz.MainScene.visible(viz.OFF)
#vizact.ontimer2(10,0,sceneOn,2)
#define the file to save subject responses
response_data = open('exp_data','a')
#use this; viztask is better
def experiment():
while True:
viz.MainScene.visible(viz.OFF)
yield viztask.waitKeyDown(' ')
yield sceneOn(1)
viztask.waitTime(3)
yield sceneOff()
viztask.waitTime(10)
yield sceneOn(2)
d = viz.Data()
yield viztask.waitMouseDown(viz.MOUSEBUTTON_LEFT, d)
print str(objects), 'was selected at', d.time
viztask.schedule( experiment() )
#viz.callback(viz.MOUSEDOWN_EVENT,mouseclick)
#go to blank scene
#vizact.whilemousedown(viz.MOUSEBUTTON_LEFT,prep)
#input subject number at start
subject = viz.input('What is the participant number?')
If I could get some help here telling me what's unnecessary, what's good and that I should keep expanding on, that would be amazing. In particular, my problems include:
- subject number can be blank and it still accepts this, I need that to change
- data file doesn't exist; do I create a spreadsheet and it stores there, or automatically makes one with the correct code?
- pressing space now just moves an object with no timing at all or scene change
- clicking anywhere at all makes the scene blank and gives me lines saying [vizard object yada yada yada moved positions] with nothing relevant, or the object name
Thanks for your help, my complete lack of understanding with vizard is getting somewhat discouraging.