VPython samples

From OriWiki

Jump to: navigation, search

The path of the examples:

C:\Python25\Lib\site-packages\visual\examples

Contents

Vpython & Tkinter control

import visual 
import Tkinter 
import thread
 
sphere = None
 
def vthread():
    global sphere 
    visual.scene.autoscale = 0
 
    sphere = visual.sphere( pos=(0,0,0), color = visual.color.green)
 
def move_sphere():
    (x,y,z) = sphere.pos
    sphere.pos = (x+1,y,z)
 
root = Tkinter.Tk() 
 
canvas = Tkinter.Canvas(root, width=400, height=200, bg='white')
canvas.pack(expand=Tkinter.YES, fill=Tkinter.BOTH)
 
menubar = Tkinter.Menu(root)
 
filemenu = Tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="Move", command=move_sphere)
 
menubar.add_cascade(label="File", menu=filemenu)
 
root.config(menu=menubar)
 
sphere = thread.start_new_thread(vthread,())
 
root.mainloop()

simple oilerian scribble

 
#!/usr/bin/python
import visual

visual.scene.autoscale = 0

v1 = (0,0,0)
ar = visual.arrow( pos = v1, shaftwidth = .1, fixedwidth = 1, color = visual.color.green)
isDrag = False
c = 0 
while True:
    if isDrag:
        p = visual.scene.mouse.pos
        ar.axis = p - v1 
        #visual.sphere(pos = p, radius = .2, color = visual.color.red)
    if not visual.scene.mouse.events:
        continue
   
    e = visual.scene.mouse.getevent()
    if e.drag :
        isDrag = True
    elif e.drop :
        curr = visual.scene.mouse.pos
        visual.cylinder(pos = v1, radius = .1, axis = curr-v1, color = (1-c,0,c))
        c = (c + .1) % 1 
        print c 
        v1 = curr 
        ar.pos = v1
        ar.axis = (0,0,0) 
        isDrag = False
        

Check code

import visual

vector1 = visual.arrow( pos=(0,0,0),axis = (1,0,10), color = visual.color.blue)
vector2 = visual.arrow( pos=(0,0,10),axis = (3,10,10), color = visual.color.yellow)
vector3 = visual.arrow( pos=(10,10,10),axis = (10,0,0), color = visual.color.green)

box = visual.box(length = 15, height = 2, width = 3, color = visual.color.red)
ball = visual.sphere(pos = (0,4,0), color = visual.color.green)
d = 0.01 
while True:
    visual.rate(1000)
    if ball.x > 5 or ball.x < -5:
        d = -d
    ball.x += d

Axis

#!/usr/bin/python

import visual

x_end = (10,0,0)
y_end = (0,10,0)
z_end = (0,0,10)

rad = .1

visual.cylinder(pos=(0,0,0), axis=x_end, radius=rad, color = visual.color.green)
visual.cylinder(pos=(0,0,0), axis=y_end, radius=rad, color = visual.color.red)
visual.cylinder(pos=(0,0,0), axis=z_end, radius=rad, color = visual.color.blue)

visual.label(pos=x_end, text='X')
visual.label(pos=y_end, text='Y')
visual.label(pos=z_end, text='Z')


mouse events

#!/usr/bin/python

import visual

orig = (0,0,0)
x_axis = (10,0,0)
y_axis = (0,10,0)
z_axis = (0,0,10)

rad = .1

visual.cylinder(pos=orig, axis=x_axis, radius=rad, color = visual.color.green)
visual.cylinder(pos=orig, axis=y_axis, radius=rad, color = visual.color.red)
visual.cylinder(pos=orig, axis=z_axis, radius=rad, color = visual.color.blue)

visual.label(pos=x_axis, text='X')
visual.label(pos=y_axis, text='Y')
visual.label(pos=z_axis, text='Z')

v1_sw = .6
v1_axis = (10,10,10)
v1_axisV = visual.vector(10,10,10)
v1 = visual.arrow(pos=orig, axis=v1_axis, shaftwidth=.6, color=visual.color.red)
v1_cyn = visual.cylinder(pos=orig, axis=v1_axisV*.9, radius=v1_sw/2, color = visual.color.red)

isDrag = False

while True:
    if visual.scene.mouse.events:
        e = visual.scene.mouse.getevent()
        if e.drag:
            print "drag",e.pick
            isDrag = True
            selectedLst = None
        elif e.drop:
            print "drop",e.pick
            isDrag = False
        elif e.click:
            print "click",e.pick
    if isDrag:            
        v1.axis = visual.scene.mouse.pos
Personal tools