from visual import * ##from time import clock # If you want to access scene.range before any user adjustments # have been made (zoom or spin), you need to wait for the scene # to be rendered and autoscaled. In that case you need to import # the clock() function to make it possible to wait for 0.1 s (say). # But if you wait on a mouse click for the user to complete zoom # or spin, you don't need to pause the 0.1 s. box(pos=(-1.5,0,0), color=color.red) box(pos=(1.5,0,0), color=color.blue) ##t = clock() ##while clock() < t+0.1: ## continue # Wait while user adjusts the view. scene.mouse.getclick() # Determine the outer bounds of the objects displayed. ### saverange has to be a new object too (not just a reference) ### Actually, since mousing never changes scene.range, this one wouldn't ### bite you until you change it programatically. saverange = vector(scene.range) # Determine how far we are from the center of the scene. savedistance = mag(scene.mouse.camera-scene.center) # Keep a copy of current scene.forward. saveforward = vector(scene.forward) ### Mousing changes scene.forward and can change scene.up too! saveup = vector(scene.up) ### Wait for more mousing, but on the next click, zoom out to 50% programatically: scene.mouse.getclick() zoom = 0.5 scene.range = scene.range/zoom ### Changing scene.range *does* shift the camera (but not vice versa). ### The new relationship between camera and scene is not under our ### control, and seemes to supercede the one saved above! We have to adjust the ### saved distance or the restored view will be off. savedistance *= mag(scene.range)/mag(saverange) # Wait while user changes the view, then restore the saved view. scene.mouse.getclick() # Determine the new distance from the center. distance = mag(scene.mouse.camera-scene.center) # Adjust the range based on the new distance. scene.range = (savedistance/distance)*saverange # Reset the viewing direction. scene.up = saveup scene.forward = saveforward