|
From: Haibo Z. <ha...@gm...> - 2009-06-19 17:58:32
|
Hi Bruce,
One more small change might be needed:
if hasattr(pick,"frame") and pick.frame:
pick = pick.frame
hasattr(pick,"frame") needs to be added, otherwise it will throw an
exception "AttributeError: 'NoneType' object has no attribute 'frame'" when
I drag on the blank area.
Thank you for your help
Regards,
Haibo
On Fri, Jun 19, 2009 at 1:25 PM, Bruce Sherwood <Bru...@nc...>wrote:
> The following slight modification works. When you pick an object, just
> check to see whether it is in a frame, and make the frame picked:
>
> from visual import *
> scene.range = 5 # fixed size, no autoscaling
> ball = sphere(pos=(-3,0,0), color=color.cyan)
> cube = box(pos=(+3,0,0), size=(2,2,2), color=color.red)
>
> f = frame()
> cylinder(frame=f, pos=(0,0,0), radius=0.1, length=1,
> color=color.cyan)
> sphere(frame=f, pos=(1,0,0), radius=0.2, color=color.red)
> f.axis = (0,1,0) # change orientation of both objects
> f.pos = (-1,0,0) # change position of both objects
>
> pick = None # no object picked out of the scene yet
> while 1:
> if scene.mouse.events:
> m1 = scene.mouse.getevent() # get event
> if m1.drag:
> drag_pos = m1.pickpos # where on the ball
> pick = m1.pick # pick now true (not None)
> if pick.frame:
> pick = pick.frame
> elif m1.drop: # released at end of drag
> pick = None # end dragging (None is false)
> if pick:
> # project onto xy plane, even if scene rotated:
> new_pos = scene.mouse.project(normal=(0,0,1))
> if new_pos != drag_pos: # if mouse has moved
> # offset for where the ball was clicked:
> pick.pos += new_pos - drag_pos
> drag_pos = new_pos # update drag position
>
> Bruce Sherwood
>
>
> Haibo Zhao wrote:
>
>> Hi guys,
>>
>> Say I have a sphere and a cylinder, and I grouped them together in a
>> frame. And then I tried to drag this frame using the code on this page
>> (With small changes). So basically, I want all the objects in the
>> frame move together when I drag the frame.
>>
>> http://vpython.org/contents/docs/visual/mouse_drag.html
>>
>> It seems like frame is not "pickable"?
>>
>> Any input will be appreciated.
>>
>> Thanks,
>> Haibo
>>
>>
>> Below is the code:
>>
>> from visual import *
>> scene.range = 5 # fixed size, no autoscaling
>> ball = sphere(pos=(-3,0,0), color=color.cyan)
>> cube = box(pos=(+3,0,0), size=(2,2,2), color=color.red)
>>
>> f = frame()
>> cylinder(frame=f, pos=(0,0,0), radius=0.1, length=1,
>> color=color.cyan)
>> sphere(frame=f, pos=(1,0,0), radius=0.2, color=color.red)
>> f.axis = (0,1,0) # change orientation of both objects
>> f.pos = (-1,0,0) # change position of both objects
>>
>> pick = None # no object picked out of the scene yet
>> while 1:
>> if scene.mouse.events:
>> m1 = scene.mouse.getevent() # get event
>> if m1.drag:
>> drag_pos = m1.pickpos # where on the ball
>> pick = m1.pick # pick now true (not None)
>> elif m1.drop: # released at end of drag
>> pick = None # end dragging (None is false)
>> if pick:
>> # project onto xy plane, even if scene rotated:
>> new_pos = scene.mouse.project(normal=(0,
>> 0,1))
>> if new_pos != drag_pos: # if mouse has moved
>> # offset for where the ball was clicked:
>> pick.pos += new_pos - drag_pos
>> drag_pos = new_pos # update drag position
>>
>
|