|
From: Bruce S. <Bru...@nc...> - 2009-06-19 18:04:55
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Good point. Another possibility is to check to make sure pick is not
None:<br>
<br>
if pick and pick.frame:<br>
pick = pick.frame<br>
<br>
Bruce Sherwood<br>
<br>
Haibo Zhao wrote:
<blockquote
cite="mid:76b...@ma..."
type="cite">Hi Bruce,<br>
<br>
One more small change might be needed:<br>
<br>
if hasattr(pick,"frame") and pick.frame:<br>
pick = pick.frame<br>
<br>
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.<br>
<br>
Thank you for your help<br>
<br>
Regards,<br>
Haibo<br>
<br>
<div class="gmail_quote">On Fri, Jun 19, 2009 at 1:25 PM, Bruce
Sherwood <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:Bru...@nc...">Bru...@nc...</a>></span>
wrote:<br>
<blockquote class="gmail_quote"
style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">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:
<div class="im"><br>
<br>
from visual import *<br>
scene.range = 5 # fixed size, no autoscaling<br>
ball = sphere(pos=(-3,0,0), color=color.cyan)<br>
cube = box(pos=(+3,0,0), size=(2,2,2), color=color.red)<br>
<br>
f = frame()<br>
cylinder(frame=f, pos=(0,0,0), radius=0.1, length=1,<br>
color=color.cyan)<br>
sphere(frame=f, pos=(1,0,0), radius=0.2, color=color.red)<br>
f.axis = (0,1,0) # change orientation of both objects<br>
f.pos = (-1,0,0) # change position of both objects<br>
<br>
pick = None # no object picked out of the scene yet<br>
while 1:<br>
if scene.mouse.events:<br>
m1 = scene.mouse.getevent() # get event<br>
if m1.drag:<br>
drag_pos = m1.pickpos # where on the ball<br>
pick = m1.pick # pick now true (not None)<br>
</div>
if pick.frame:<br>
pick = pick.frame
<div class="im"><br>
elif m1.drop: # released at end of drag<br>
pick = None # end dragging (None is false)<br>
if pick:<br>
# project onto xy plane, even if scene rotated:<br>
new_pos = scene.mouse.project(normal=(0,0,1))<br>
if new_pos != drag_pos: # if mouse has moved<br>
# offset for where the ball was clicked:<br>
pick.pos += new_pos - drag_pos<br>
drag_pos = new_pos # update drag position<br>
<br>
</div>
<font color="#888888">Bruce Sherwood</font>
<div>
<div class="h5"><br>
<br>
Haibo Zhao wrote:<br>
<blockquote class="gmail_quote"
style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi guys,<br>
<br>
Say I have a sphere and a cylinder, and I grouped them together in a<br>
frame. And then I tried to drag this frame using the code on this page<br>
(With small changes). So basically, I want all the objects in the<br>
frame move together when I drag the frame.<br>
<br>
<a moz-do-not-send="true"
href="http://vpython.org/contents/docs/visual/mouse_drag.html"
target="_blank">http://vpython.org/contents/docs/visual/mouse_drag.html</a><br>
<br>
It seems like frame is not "pickable"?<br>
<br>
Any input will be appreciated.<br>
<br>
Thanks,<br>
Haibo<br>
<br>
<br>
Below is the code:<br>
<br>
from visual import *<br>
scene.range = 5 # fixed size, no autoscaling<br>
ball = sphere(pos=(-3,0,0), color=color.cyan)<br>
cube = box(pos=(+3,0,0), size=(2,2,2), color=color.red)<br>
<br>
f = frame()<br>
cylinder(frame=f, pos=(0,0,0), radius=0.1, length=1,<br>
color=color.cyan)<br>
sphere(frame=f, pos=(1,0,0), radius=0.2, color=color.red)<br>
f.axis = (0,1,0) # change orientation of both objects<br>
f.pos = (-1,0,0) # change position of both objects<br>
<br>
pick = None # no object picked out of the scene yet<br>
while 1:<br>
if scene.mouse.events:<br>
m1 = scene.mouse.getevent() # get event<br>
if m1.drag:<br>
drag_pos = m1.pickpos # where on the ball<br>
pick = m1.pick # pick now true (not None)<br>
elif m1.drop: # released at end of drag<br>
pick = None # end dragging (None is false)<br>
if pick:<br>
# project onto xy plane, even if scene rotated:<br>
new_pos = scene.mouse.project(normal=(0,<br>
0,1))<br>
if new_pos != drag_pos: # if mouse has moved<br>
# offset for where the ball was clicked:<br>
pick.pos += new_pos - drag_pos<br>
drag_pos = new_pos # update drag position<br>
</blockquote>
</div>
</div>
</blockquote>
</div>
<br>
</blockquote>
</body>
</html>
|