VPython and threads?

289 views
Skip to first unread message

Bartłomiej Stodulski

unread,
Dec 16, 2014, 2:47:06 PM12/16/14
to vpytho...@googlegroups.com
Hello, can anyone tell me if there is a way to run threads with Visual Python? Or is there any other solution ? I'm making project that needs to run for example two 3D objects from VP that move in other directions at the same time and do a lot of other stuff separately.

Cheers,

B.S.

Steve Spicklemire

unread,
Dec 16, 2014, 3:37:58 PM12/16/14
to vpytho...@googlegroups.com
Threads are used internally so I don't see any reason why they couldn't be used. Have you encountered a particular problem? Can you post an example the fails?

thanks,
-steve
--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bartłomiej Stodulski

unread,
Dec 16, 2014, 4:30:59 PM12/16/14
to vpytho...@googlegroups.com
from visual import *

def create_and_move(w):
    ball = sphere(pos=(0,0,0))
    ball.velocity = vector(0,0,w)
   
    while True:
        rate(100)
        ball.pos = ball.pos + ball.velocity*0.01

try:
    thread.start_new_thread(create_and_move,-1)
    thread.start_new_thread(create_and_move,1)
except:
    print('Thread Error')
while 1:
    pass


It doesn't work. I just want to create a somekind of a function that creates objects, sets their vectors and changes their position at the same time.

Bruce Sherwood

unread,
Dec 16, 2014, 5:37:27 PM12/16/14
to vpytho...@googlegroups.com
I think that it is not possible to have two threads that contain rate() statements, because only the main thread can process window events, and there's some kind of collision with wxPython, upon which VPython is based. Here however is a scheme that my do what you want, in that you can have multiple objects animated, and a separate process p can be running along in a separate thread.

from __future__ import division, print_function
import thread
import time
from visual import *
scene.range = 10

def create(w):
    b = sphere(pos=(0,0,0), radius=0.5)
    b.velocity = vector(w,0,0)
    return b

balls = [create(1), create(-1)]

def p(a):
    while True:
        time.sleep(0.2*a)
        if a == 1:
            balls[0].color = color.cyan
            balls[1].color = color.red
        else:
            balls[0].color = color.magenta
            balls[1].color = color.yellow

try:
   thread.start_new_thread( p, (1,) )
   thread.start_new_thread( p, (2,) )
except:
   print("Error: unable to start thread")

while True:
    rate(100)
    for ball in balls:

Bartłomiej Stodulski

unread,
Dec 16, 2014, 8:23:37 PM12/16/14
to vpytho...@googlegroups.com
Thank You very much Sir, really appreciate Your help!

Best regards, from Polish student

B.S.
Reply all
Reply to author
Forward
0 new messages