PDA

View Full Version : Error Changing Scenes


sportykourty
04-05-2015, 09:08 AM
I'm trying to have my program change scenes once I enter a proximity sensor. I keep receiving an error. Any ideas what i'm doing wrong? :confused:


import viz
import vizact
import vizproximity

viz.setMultiSample(4)
viz.go()

############# Movement Control Code###################
#Script to control viewpoint with keyboard arrow keys
MOVE_SPEED = 900
TURN_SPEED = 60

def updatecar():
#move view forward and backward
if viz.key.isDown(viz.KEY_UP):
view.move([MOVE_SPEED*viz.elapsed(),0,0],viz.BODY_ORI)
if viz.key.isDown(viz.KEY_DOWN):
view.move([-MOVE_SPEED*viz.elapsed(),0,0],viz.BODY_ORI)
if viz.key.isDown(viz.KEY_TAB):
view.move([0,MOVE_SPEED*viz.elapsed(),0],viz.BODY_ORI)
if viz.key.isDown(viz.KEY_SHIFT_L):
view.move([0,-MOVE_SPEED*viz.elapsed(),0],viz.BODY_ORI)

#rotate body of view left and right
if viz.key.isDown(viz.KEY_RIGHT):
view.setEuler([TURN_SPEED*viz.elapsed(),0,0],viz.BODY_ORI,viz.REL_PARENT)
elif viz.key.isDown(viz.KEY_LEFT):
view.setEuler([-TURN_SPEED*viz.elapsed(),0,0],viz.BODY_ORI,viz.REL_PARENT)
vizact.ontimer(0,updatecar)

########### Viewpoint Code #################
#Start collision detection
viz.MainView.collision( viz.ON )
view = viz.MainView

#Get rid of gravity.
viz.MainView.gravity(0)

#Set the viewpoint.
viz.MainView.setPosition(88,0,-443)

#Viewpoint
view = viz.MainView

#Actual Viewpoint
view.setPosition([-7034, 200, -10775])
view.setEuler([90, 0, 0])

mars = viz.add('gallery.osgb',scene = viz.Scene1)
viz.addChild('dojo.osgb',scene=viz.Scene2)

manager = vizproximity.Manager()
manager.setDebug(True)
manager.addTarget( vizproximity.Target(viz.MainView) )

# Create sensor using bounding box of node
node = viz.addChild('plant.osgb',pos=(-4034, 0, -10775),scale=(1000,1000,1000))
node.addAction(vizact.spin(0,1,0,45,viz.FOREVER))
sensor = vizproximity.addBoundingBoxSensor(node)
manager.addSensor(sensor)

def changeScene(scene):
viz.MainWindow.setScene(scene)
if scene == viz.Scene2:
viz.MainView.setPosition([0,1.8,0])
viz.MainView.setEuler([90,0,0])

manager.onEnter(None, changeScene)

mape2k
04-08-2015, 06:03 AM
The problems you have originate from the last bits of your code:


def changeScene(scene):
viz.MainWindow.setScene(scene)
if scene == viz.Scene2:
viz.MainView.setPosition([0,1.8,0])
viz.MainView.setEuler([90,0,0])

manager.onEnter(None, changeScene)

In the last line, you tell Vizard to call the function changeScene() whenever the sensor is entered. The argument that gets passed to that function by default is the sensor that was activated. So your variable <scene> will be a proximity sensor => this will create an error when trying to change the scene to a sensor.

To fix this, you need to pass more arguments to the function changeScene, e.g.:


def changeScene(sensor,scene):

viz.MainWindow.setScene(scene)
if scene == viz.Scene2:
viz.MainView.setPosition([0,1.8,0])
viz.MainView.setEuler([90,0,0])

manager.onEnter(None, changeScene, desiredScene)

in which <desiredScene> is the variable to the scene you wish to change to.