Recent Forum Posts
From categories:

Hi, doing a physics simulation for my A-Level computing project. Unfortunately none of my computing teachers know much about python.
So I'm trying to add items to menu inside a loop, but I am having difficulty. The general code looks like this:


While not bool:
Y = CalcY
X = CalcX
If Y <= 0:
menu.items.append(text, lambda:procedure(X))
[[/code]]

but when I go to the menu, all items are under the last X value for some reason. I have also tried coding it using a counter but no luck with either.


While not bool:
Y = CalcY
X = CalcX
If Y <= 0:
counter +=1
menu.items.append(text, lambda:procedure(counter))
array.append(X)
[[/code]]

The procedure then looks at array position for the X value, but its always the last counter value (say loops through 14 times, when i go to use the menu its argument for the procedure is always 14). Anybody know how to solve this?

Thanks.

VPython Menus by rich_jones93rich_jones93, 02 Dec 2012 12:53

I had the same problem and searching for the solution came to this thread. I could not find any proper solution yet. However, I did find an work around.

After doing the "setup.py py2exe" find the library.zip file. Extract the zip file on the same location and remove the zip file. Now, it should work fine.

This may not be the best solution, so if any one else have any solution please let us know.

Thank you

Re: vpython5 and py2exe by mihassanmihassan, 31 Aug 2010 17:44

I am having trouble using py2exe with visual python 5. This problem appears even with the simple examples that are included with the visual-python package.

If I do a standard “setup.py py2exe” with the option "skip_archive":1 to avoid making library.zip, and then I try to run my newly created exe in the dist folder, it complains about being unable to find the file turbulence3.tga.

If I then copy the three tga files from site-packages\visual into my dist\visual folder, it all works fine.

Next, if I do a standard “setup.py py2exe”, which makes a big library.zip, and then I run the myProgram.exe, I get this:
C:\DOCUME~1\092710\MYDOCU~1\python\dist>myProgram
Traceback (most recent call last):
File "Tashe-001.py", line 6, in <module>
File "AnimTools.pyc", line 1, in <module>
File "visual\init.pyc", line 66, in <module>
File "visual\ui.pyc", line 3, in <module>
File "visual\materials.pyc", line 151, in <module>
File "visual\materials.pyc", line 129, in loadTGA
IOError: [Errno 2] No such file or directory: 'C:\\DOCUME~1\\092710\\MYDOCU~1\\p
ython\\dist\\library.zip\\visual/turbulence3.tga'

If I then use either winzip or python's zipfile to add those three tga files to the visual folder inside library.zip, I still get the same traceback.

Any ideas? How to get py2exe to find those three files and put them in library.zip? Or should I try to import less than * everything from visual in myProgram?—-I'm not using those tga files at all.

Thank you.

vpython5 and py2exe by Greg SwiftGreg Swift, 03 Mar 2010 21:17

I'm afraid this forum isn't read by many people. It would be better to post this to the VPython mailing list.

The following code should show a PyQt4 form together with a VPython scene.
Using VPython3 it works correctly, while useing VPython5 it crashes.
Does anyone has experience on facing this problem?
I would be very grateful if anyone can help me.

from visual import *
from PyQt4 import QtCore, QtGui
import sys
 
class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QDialog.__init__(self)
 
scene.visible = 1
b = box()
 
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

replacing

scene.visible = 1

with
scene.visible = 0

it works and the Qt window is shown also with VPython5 (but the scene can't be obviously seen!).

Thanks in advance
Matteo

The SConstruct file has not been maintained is no longer relevant. Start by reading the file INSTALL.txt. It looks to me like maybe you're trying to build against Numeric, which is no longer supported, rather than numpy.

Hi all,

I've been playing around with VPython for a little less than a year now. I installed the package from the Debian packages and had great success. But some sort of update in the last week or so is causing a seg fault when even the examples are run. The bug has already been reported to the package maintainers.

http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=539922

So I've tried building from source (5.11) after grabbing the .bz tarball, but I still get a seg fault.

Next I tried grabbing the latest (as of Aug. 14th) CVS files from Sourceforge for the vpython-core2 package to try to build. Does anyone know if this is where I should be starting?

I've got Python 2.5 installed, so I edited the SConstruct file to reflect this. I also added '/usr/include/libglademm-2.4' to the CPPPATH to pick up one of the include files that's buried a bit deeper on my system. However, I'm still getting errors.

In file included from src/python/num_util_impl_numeric.cpp:4:
include/python/num_util_impl.hpp:26: error: ‘array_types’ has not been declared
include/python/num_util_impl.hpp:27: error: ISO C++ forbids declaration of ‘array_types’ with no type
include/python/num_util_impl.hpp:27: warning: ‘array_types’ initialized and declared ‘extern’
include/python/num_util_impl.hpp:27: error: ‘type_impl’ was not declared in this scope
include/python/num_util_impl.hpp:27: error: expected ‘,’ or ‘;’ before ‘(’ token
include/python/num_util_impl.hpp:31: error: ‘array_types’ is not a type
src/python/num_util_impl_numeric.cpp:14: error: ‘NPY_TYPES cvisual::python::type’ redeclared as different kind of symbol
include/python/num_util.hpp:127: error: previous declaration of ‘NPY_TYPES cvisual::python::type(boost::python::numeric::array)’
src/python/num_util_impl_numeric.cpp:15: error: ‘array_types’ does not name a type
src/python/num_util_impl_numeric.cpp:16: error: ‘char cvisual::python::type2char’ redeclared as different kind of symbol
include/python/num_util.hpp:225: error: previous declaration of ‘char cvisual::python::type2char(NPY_TYPES)’
src/python/num_util_impl_numeric.cpp: In function ‘void cvisual::python::init_numeric_impl()’:
src/python/num_util_impl_numeric.cpp:33: error: ‘import_array’ was not declared in this scope
src/python/num_util_impl_numeric.cpp: At global scope:
src/python/num_util_impl_numeric.cpp:42: error: ‘NPY_TYPES cvisual::python::type’ redeclared as different kind of symbol
include/python/num_util.hpp:127: error: previous declaration of ‘NPY_TYPES cvisual::python::type(boost::python::numeric::array)’
src/python/num_util_impl_numeric.cpp:42: error: expected ‘)’ before ‘t’
src/python/num_util_impl_numeric.cpp:205: error: expected ‘}’ at end of input
src/python/num_util_impl_numeric.cpp:205: error: expected ‘}’ at end of input
scons: *** [src/python/num_util_impl_numeric.os] Error 1
scons: building terminated because of errors.

Has anyone see this or have suggestions so as to get VPython back up and running on my system?

Matt

VPython crashing on Debian by mattbellismattbellis, 15 Aug 2009 06:51

There were several pending new member requests that had not been approved. They have all been approved now. Sorry for the delay.

New Member Requests by Tim KorbTim Korb, 14 Aug 2009 21:46

Collision detection is essential but difficult to implement for mechanics simulations.
I modified the pyODE collision detection example to work with VPython.
You can create robots and artificial life by adding and animating pyODE joints between the blocks.

# pyODE example 3: Collision detection

# Originally by Matthias Baas.
# Modified by Miles Jacobs to make collision detection work with VPython.
# With thanks to http://python.matrix.jp/projects/pyogre/ode_test2.py.

import sys, os, random, time
from math import *

from visual import *
import ode

# geometric utility functions
def scalp (vec, scal):
    vec[0] *= scal
    vec[1] *= scal
    vec[2] *= scal

def length (vec):
    return sqrt (vec[0]**2 + vec[1]**2 + vec[2]**2)

# create_box
def create_box(world, space, density, lx, ly, lz):
    """Create a box body and its corresponding geom."""

    # Create body
    body = ode.Body(world)
    M = ode.Mass()
    M.setBox(density, lx, ly, lz)
    body.setMass(M)

    # Set parameters for drawing the body
    body.shape = "box"
    body.boxsize = (lx, ly, lz)

    # Create a box geom for collision detection
    geom = ode.GeomBox(space, lengths=body.boxsize)
    geom.setBody(body)

    return body

# drop_object
def drop_object():
    """Drop an object into the scene."""

    global bodies, counter, objcount

    body = create_box(world, space, 1000, 1.0,1.0,1.0)
    body.setPosition((random.gauss(0,0.1),10.0,random.gauss(0,0.1)))
    theta = random.uniform(0,2*pi)
    ct = cos (theta)
    st = sin (theta)
    body.setRotation([ct, 0., -st, 0., 1., 0., st, 0., ct])
    icecube = box(pos=body.getPosition(), axis=(ct,0.,st),length=1.0, height=1.0, width=1.0,

    color=(random.gauss(0.1,1),random.gauss(0.1,1),random.gauss(0.1,1)))
    bodies.append(body)
    cubes.append(icecube)
    counter=0
    objcount+=1

# explosion
def explosion():
    """Simulate an explosion.

    Every object is pushed away from the origin.
    The force is dependent on the objects distance from the origin.
    """
    global bodies

    for b in bodies:
        l=b.getPosition ()
        d = length (l)
        a = max(0, 800000*(1.0-0.2*d*d))
        l = [l[0] / 4, l[1], l[2] /4]
        scalp (l, a / length (l))
        b.addForce(l)

# pull
def pull():
    """Pull the objects back to the origin.

    Every object will be pulled back to the origin.
    Every couple of frames there'll be a thrust upwards so that
    the objects won't stick to the ground all the time.
    """
    global bodies, counter

    for b in bodies:
        l=list (b.getPosition ())
        scalp (l, -100000 / length (l))
        b.addForce(l)
        if counter%60==0:
            b.addForce((0,100000,0))

# Collision callback
def near_callback(args, geom1, geom2):
    """Callback function for the collide() method.

    This function checks if the given geoms do collide and
    creates contact joints if they do.
    """

    # Check if the objects do collide
    contacts = ode.collide(geom1, geom2)

    # Create contact joints
    world,contactgroup = args
    for c in contacts:
        c.setBounce(0.2)
        c.setMu(5000)
        j = ode.ContactJoint(world, contactgroup, c)
        j.attach(geom1.getBody(), geom2.getBody())

######################################################################

# Create a world object
world = ode.World()
world.setGravity( (0,-9.81,0) )
world.setERP(0.8)
world.setCFM(1E-5)

# Create a space object
space = ode.Space()

# Create a plane geom which prevent the objects from falling forever
floor = ode.GeomPlane(space, (0,1,0), 0)
ground=box(pos=(0,-0.1,0), width=20, height=0.1, length=20, 
color=color.white)

# A list with ODE bodies and Visual objects to see the bodies
bodies = []
cubes = []

# A joint group for the contact joints that are generated whenever
# two bodies collide
contactgroup = ode.JointGroup()

# Some variables used inside the simulation loop
fps = 25
dt = 1.0/fps
running = True
state = 0
counter = 0
objcount = 0
scene.autoscale=0
scene.forward=(-5,-5,-5)

while(1):

    global counter, state

    counter += 1

    if state==0:
        if counter==20:
            drop_object()
        if objcount==10:
            state=1
            counter=0
    # State 1: Explosion and pulling back the objects
    elif state==1:
        if counter==100:
            explosion()
        if counter>300:
            pull()
        if counter==500:
            counter=20

    for b in range(len(bodies)):
        x,y,z = bodies[b].getPosition()
        R = bodies[b].getRotation()
        cubes[b].pos=(x,y,z)
        cubes[b].axis=R[0], R[3], R[6]
        cubes[b].up=R[1], R[4], R[7]

    print state, counter
    # Simulate
    n = 2

    for i in range(n):
        # Detect collisions and create contact joints
        space.collide((world,contactgroup), near_callback)

        # Simulation step
        world.step(dt/n)

        # Remove all contact joints
        contactgroup.empty()

    rate(25)
Version 4
wyleuwyleu 29 Oct 2008 18:47
in discussion Hidden / Per page discussions » Visual 4

Certainly looking forward to see what it will do.

Version 4 by wyleuwyleu, 29 Oct 2008 18:47

Ah. thanks for the answer. I was mildly surprised when 2.6 came out, I didn't realize anything before 3.0 was planned.

I really like VPython, particularly in my intro. to comps. course. If I have to choose between 2.6 and Vpython, the choice is easy.

-Bob Montante

Re: Python 2.6 and VPython? by bobmonbobmon, 27 Oct 2008 14:17

Indeed, the version of Visual has to have been built for a specific version of Python, and a mismatch cannot work. Just this week I worked a bit with Python 2.6 to see what the issues are going to be. There are some problems. The Boost libraries, which enhance C++, have to be built for a specific version of Python, and since there aren't yet available these binaries for 2.6, I built them from source. Similarly for numpy. Turns out that something has changed in these components, because there's now a type mismatch between numpy integers and Python integers (this is a simplified description). Moreover, for the new Visual about to be released, there's a problem with a native-mode version for the Mac because the Mac Python 2.6 has an IDLE that won't run!

Summary: There are various problems in the Python 2.6 world. But we'll be tracking them and looking for solutions. We would prefer to release the new Visual for Python 2.6, because the new Python builds a bridge between Python 2.5 and the coming Python 3.0, a version of Python that will actually break some existing programs. Python 2.6 offers tools to check on possible incompatibilities.

Bruce Sherwood

How can I use VPython w/ Python 2.6, in Windows? I tried the naive approach of running "VPython-Win-Py2.5-3.2.11.exe" —- it doesn't find the C:\Python_26 directory, and if I force it it still fails. Specifically, "bounce.py" fails with a complaint about not finding cvisual.pyd.

It's not a major problem for me (yet) since I still have Python v2.5.2 around, but I thought I'd ask.

Python 2.6 and VPython? by bobmonbobmon, 14 Oct 2008 13:05

Many thanks for setting this up.

Chris@wyleu

A new play to play with Vpython! by wyleuwyleu, 07 Oct 2008 16:28

Hmm, attaching files doesn't seem to work well. Here's another approach, using the code feature to post it inline. In fact, with Python mode, it looks pretty nice.

from visual import *
 
floor = box (pos=(0,0,0), length=4, height=0.5, width=4, color=color.blue)
ball = sphere (pos=(0,4,0), radius=1, color=color.red)
ball.velocity = vector(0,-1,0)
dt = 0.01
 
while 1:
    rate (100)
    ball.pos = ball.pos + ball.velocity*dt
 
    if ball.y < ball.radius:
        ball.velocity.y = -ball.velocity.y
    else:
        ball.velocity.y = ball.velocity.y - 9.8*dt
Re: sample file posting by Tim KorbTim Korb, 28 Sep 2008 17:08

The attached file is the sample program from www.vpython.org. (I see that the file type contents are incorrectly determined to be Java program text!)

sample-file-posting/sample.py

I uploaded a file named sample.py to "this page", but can't seem to get a reference to it.

sample file posting by Tim KorbTim Korb, 28 Sep 2008 17:05
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License