|
From: Dunning, R. <dun...@lo...> - 2007-05-30 21:57:34
|
> Hi Theo, > > As Bruce and Rodney have said, it's not a bug, it's a > feature! But not one of my favorites. Just a guess: probably it was thought that programmers would find it easier to loop over the objects in a scene (or frame) without having to explicitly check for visibility. Perhaps the assumption was that any loop over objects would very likely only need to target the visible objects in the scene. About my "fix," look at this line: scene.my_objects = spheres This line simply attaches the name "scene.my_objects" to the same list referenced by "spheres." If you later on append an object to scene.my_objects, it gets appended to the spheres list as well (it's the *same* list)--very likely this isn't what you want. A classic Python gotcha! -- Rodney Dunning Assistant Professor of Physics Longwood University http://www.longwood.edu/staff/dunningrb > -----Original Message----- > From: vis...@li... > [mailto:vis...@li...] On > Behalf Of Bill Ward > Sent: Wednesday, May 30, 2007 2:53 PM > To: Vis...@li... > Subject: Re: [Visualpython-users] Visibility Bug? > > Hi Theo, > > As Bruce and Rodney have said, it's not a bug, it's a > feature! But not one of my favorites. I understand the new > beta will obey "frame.visible = 0" to do what you want, but > sometimes you may want to be selective, say you want to hide > only certain types of objects, so you want to turn maintain > your own list anyway. > > My solution has been to write an new "Frame" subclass of > "frame", and give it methods that are convenient. Here's > that bit of code in case you find it > instructive: > > class Frame(frame): > """Subclass visual's frame to add some convenient functions. > Specifically, > make the .visible attribute do what you expect (show or > hide all the > objects in a frame). """ > > def show(self, state=None): > """ Set visibility of all objects in the frame to > 'state'. If state is absent, > toggle the visibility. Note that the .objects > attribute does not report > hidden objects, so first a parallel list has to be created. > """ > #On the first call, create the all_objects list > (might be cleaner to use our own __init__()? ) > > if not hasattr(self,'all_objects'): #store all > objects (not just > visible) > self.all_objects = self.objects[:] > if state == None: > self.visible = not self.visible > else: > self.visible = state > for obj in self.all_objects: > show(obj,self.visible) > > def animate(self, *args): > """ Animate all the visible objects in the frame that > have their own > animate() functions defined. Pass through everything > in *args. > """ > if self.visible: > for o in self.objects: #loop visible objects only > if o and hasattr(o,'animate'): > o.animate(*args) > > To use it: > > f = Frame() > box(frame=f) > box(y=2,frame=f) > f.show(False) #make frame, a couple of objects, and hide initially > > while 1: > if scene.kb.keys: # keyboard event in main scene? > f.show() #toggle the objects in f on any key press > > I have this class in a module of stuff I import everywhere > and it tidies things up for me a bit. I hope it's a useful example! > > Bill Ward > > P.S. The last "animate" function is a little distraction I > left in, so let me explain what that means briefly. I often > use a new class definition to generate my objects, and then > declare an animate() method inside that. So if my scene has > a Frame "f" I can just call, for example, "f.animate(coswt, > sinwt)", and if all the custom moving objects in the Frame > have a bound "animate" method that expects two arguments of > that form, I'm done. > > Sorry to muddy things up. You can lose that bit of code or > leave it, it should be harmless. > > > > > -----Original Message----- > From: vis...@li... > [mailto:vis...@li...] On > Behalf Of Bruce Sherwood > Sent: Wednesday, May 30, 2007 11:03 AM > Cc: vpython > Subject: Re: [Visualpython-users] Visibility Bug? > > From the Visual reference manual (on the Help manual in VPython): > > ------------------------ > Another frame attribute is objects, which is a list of > currently visible objects contained in the frame (the list > does not include objects that are currently invisible). If > you want to make all the objects in a frame be red, do the > following (assume the frame is named f): > > for obj in f.objects: > obj.color = color.red > > If you use this method to make all the objects invisible, the > f.objects list will be empty. If you need a list containing > all the objects, both visible and invisible, you need to > maintain your own list of objects. > -------------------------- > > In the new beta version there is this new feature: "You can > now make a frame visible or invisible, and all objects in the > frame will be affected. And you can set the vector "scale" > attribute of a frame to change the size of the combined object." > > Bruce Sherwood > > Theo DuBose wrote: > > Hello, > > > > I have noticed what appears to be a bug when dealing > with frames > > and visibility. If I make all the objects in the frame > disappear, with > > > > for obj in a.objects: > > obj.visible = 0 > > > > and then attempt to make the objects reappear with > > > > > > for obj in a.objects: > > obj.visible = 0 > > > > They do not appear. Is this a known, or does anyone have any > > suggestions? Presently I am overcoming this by reinstantiating the > > invisible objects, but i would like to be able to reduce > the amount of > > memory this uses. > > > > Thanks, > > Theo > > > > > ---------------------------------------------------------------------- > > --- This SF.net email is sponsored by DB2 Express Download > DB2 Express > > C - the FREE version of DB2 express and take control of > your XML. No > > limits. Just data. Click to get it now. > > http://sourceforge.net/powerbar/db2/ > > _______________________________________________ > > Visualpython-users mailing list > > Vis...@li... > > https://lists.sourceforge.net/lists/listinfo/visualpython-users > > > > -------------------------------------------------------------- > ----------- > This SF.net email is sponsored by DB2 Express Download DB2 > Express C - the FREE version of DB2 express and take control > of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Visualpython-users mailing list > Vis...@li... > https://lists.sourceforge.net/lists/listinfo/visualpython-users > > > > -------------------------------------------------------------- > ----------- > This SF.net email is sponsored by DB2 Express Download DB2 > Express C - the FREE version of DB2 express and take control > of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Visualpython-users mailing list > Vis...@li... > https://lists.sourceforge.net/lists/listinfo/visualpython-users > |