from visual import* from visual.graph import * print """ Rob Salgado (salgado@physics.syr.edu) - Sept 24, 2003 This program copies objects from one display into another with an improved copyobjects() function. With a new function updatecopyobjects(), I wish to update the objects in the destination display with data from the source display. However, to make this work, I had to understand a subtlety in the copyobjects() function on http://www.vpython.org/webdoc/visual/options.html . First, run and observe the shell output. (Widen the shell output window.) Second, note my updatecopyobjects() function and my revised copyobjects() function. The original version of copyobjects() does not use the reverse() function. Observe what happens when that statement is commented out. *** Essentially, I believe copyobjects() should traverse the source-display's list in reverse as it builds up the destination-display so that the items in the destination list of objects are in correspondence with those of the source list. For an example of what you can do with this, comment out the print statements in updatecopyobjects() [so the animations will run smoothly] and uncomment the updatecopyobjects() statement in the while loop. =========== """ scene=display( title="A", width=350,height=350, x=0, y=0, autoscale=0, visible=1, forward=(-1,-1,-1),center=vector(0,0,0) ) sceneB=display( title="B", width=350,height=350, x=350, y=0, autoscale=0, visible=1, forward=(0,-1,-10),center=vector(0,0,0) ) scene.select() def copyobjects(scene1,scene2): # based on http://www.vpython.org/webdoc/visual/options.html # Copy all Visual objects from scene1 into scene2 scene2.select() scenelist=scene1.objects ### ### reverse() is needed so that the items in the destination list of objects ### are in correspondence with those of the source list scenelist.reverse() for obj in scenelist: newobj = obj.__class__() # create object in scene2 # FOR DEBUGGING print obj, obj.__class__, "\t--copy-->\t", print newobj, newobj.__class__ for member in obj.__members__: if member == 'display': continue # avoid putting into scene1 setattr(newobj,member,getattr(obj,member)) # FOR DEBUGGING print "\n-----list of scene1 objects" print scene1.objects for obj in scene1.objects: print obj.__class__, print "\n-----list of scene2 objects" print scene2.objects for obj in scene2.objects: print obj.__class__, print "\n------------" def updatecopyobjects(scene1,scene2): # Update the Visual objects copied into scene2 from scene1 obj2_list=scene2.objects i=0 for obj in scene1.objects: # FOR DEBUGGING print obj, obj.__class__, "\t--update-->\t", print obj2_list[i], obj2_list[i].__class__ for member in obj.__members__: if member == 'display': continue # avoid putting into scene1 setattr(obj2_list[i],member,getattr(obj,member)) i += 1 ### A variant of the "Simple program example" ### from http://www.vpython.org/vpythonprog.htm floor = box (pos=(0,0,0), length=4, height=0.5, width=4, color=color.blue) ball = sphere (pos=(0,4,0), radius=1, color=color.red) ball.velocity = vector(0,-1,0) dt = 0.01 copyobjects(scene,sceneB) updatecopyobjects(scene,sceneB) # here for proof of concept while 1: rate (100) ball.pos = ball.pos + ball.velocity*dt if ball.y < ball.radius: ball.velocity.y = -ball.velocity.y else: ball.velocity.y = ball.velocity.y - 9.8*dt #You may want to comment out the function's print statements #before you uncomment the next line.: #updatecopyobjects(scene,sceneB)