|
From: Bill W. <ww...@la...> - 2007-05-30 18:53:23
|
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
|