Hi
I have been mucking around with VPython for a bit now and i can use it for basic 3D Python but i have not yet worked out how to do things that arent the usual, add square, add arrow, add sphere.

So i was wondering if it was even possible to do things in VPython such as importing 3D models that you could use as a world terrain or even just adding textures to things like the square and circle? Either of those would be really nice.

If you could help that would be wonderful.
Thanks

Recommended Answers

All 5 Replies

I have played around with VPython, looks like I have to do some more. Here are some of my favorite programs ...

# experiments with visual Python (VPython)
# free download from http://vpython.org
# bounce a ball on the floor (no wind resistance)
# drag the right mouse button to change viewing angle

import visual as vs

vs.scene.width = 350
vs.scene.height = 500
vs.scene.center = (0,0.5,0)
vs.scene.title = "Bouncing Ball (drag right mouse button)"
# avoid autoscale (default)
vs.scene.autoscale = False

# a box object with attributes (x, y, z) or (length, height, width), color
floor = vs.box(pos=(0, -5.4, 0), size=(4, 0.2, 4), color=vs.color.green)

# a spherical object with attributes position (x, y, z), radius, color
ball = vs.sphere(pos=(0, 7.3, 0), radius=0.7, color=vs.color.red)

# ball moves in the y axis
ball.velocity = vs.vector(0, -1, 0)

# delta time
dt = 0.005
while 1:
    # set the rate of animation speed (update frequency)
    vs.rate(200)
    # change the position of ball based on the velocity on the y axis
    ball.pos = ball.pos + ball.velocity * dt
    # reverse ball direction within the y range
    if ball.y < -5:
        ball.velocity.y = -ball.velocity.y
    else:
        ball.velocity.y = ball.velocity.y - 9.8 * dt

and ...

# experiments with visual Python (VPython)
# free download from http://vpython.org
# create random spheres of random color, size and position
# animate into random directions displaying a trail
# drag the right mouse button to change the viewing angle

import visual as vs
import random as rn

vs.scene.width = 600
vs.scene.height = 500
vs.scene.title = "Random Spheres (drag right mouse button)"
# avoid autoscale (default)
vs.scene.autoscale = False

# make n balls and create a list of balls
n = 33
balls = []
for i in range(n):
    balls += [vs.sphere(pos=(rn.uniform(-5,5),rn.uniform(-5,5),
        rn.uniform(-5,5)),
        radius=rn.random(),
        color=(rn.random(), rn.random(), rn.random()))]
    balls[i].velocity = vs.vector(rn.uniform(-1,1), rn.uniform(-1,1),
        rn.uniform(-1,1))
    balls[i].trail = vs.curve(color=balls[i].color)

# delta time (seconds)
dt = 0.01
while True:
    # update once per dt
    vs.rate(1/dt)
    for i in range(n):
        balls[i].pos += balls[i].velocity * dt
        balls[i].trail.append(pos=balls[i].pos)

or simply ...

# just a yellow sphere sitting on a blue box
# you can drag the right mouse button to change the viewing angle

import visual as vs

vs.scene.title = "Ball on floor (drag right mouse button)"

# create a sphere at position coordinates (x, y, z)
# play with (x, y, z) to see how it behaves
ball = vs.sphere(pos=(0, 3, 0), color=vs.color.yellow, radius=2)
# create a box
floor = vs.box(length=10, height=2, width=4, color=vs.color.blue)
commented: Great posts, amazing effort +1

All I could find so far is the use of faces to form shapes. With faces you can add textures to your shapes, but you have to be pretty good with mathematics. Here is an example ...

# experiments with visual Python (VPython)
# free download from http://vpython.org
# example use of faces object for building arbitrary shapes
# (here, a cone) original by David Scherer July 2001
# adds a texture to the shape's surface

import visual as vs

vs.scene.width = 500
vs.scene.height = 500
vs.scene.title = "Cone using faces (drag right mouse button)"

# create a cone in a frame
# smooth shading, no bottom, normals are not quite physical
f = vs.frame()

N = 20
model = vs.faces(pos=vs.zeros((N*3,3), vs.Float), frame=f)

# like range() but returns a list of floats
t = vs.arange(0, 2*vs.pi+2*vs.pi/N, 2*vs.pi/N)
print t  # test only

# vertex 0 of triangles
model.pos[0::3, 0] = vs.sin(t[:-1])
model.pos[0::3, 2] = vs.cos(t[:-1])

# vertex 1 of triangles
model.pos[1::3, 0] = vs.sin(t[1:])
model.pos[1::3, 2] = vs.cos(t[1:])

# vertex 2 of triangles (point of cone)
model.pos[2::3, 1] = 2

# all normals point outward in XZ ...
# not quite right, but there is already a cone primitive
model.normal = model.pos
model.normal[2::3,0] = vs.sin((t[:-1]+t[1:])/2)
model.normal[2::3,1] = 0
model.normal[2::3,2] = vs.cos((t[:-1]+t[1:])/2)

model.color = model.pos/2 + (0.5,0,0.5)
model.smooth_shade(doublesided=0)

# optionally show where the triangles are
#model.color[0::3] = (1, 1, 1)

'''
# optionally animate ...
while True:
    vs.rate(100)
    f.rotate(angle=0.01)
'''

Another way to give a shape some texture is to create it with slices of other shapes ...

# experiments with visual Python (VPython) from: http://vpython.org/
# draw a series of increasing radius cylinders to form
# a cone, also change color for texture effect
# press and drag the right mouse button to rotate the cone

import visual as vs

vs.scene.width = 600
vs.scene.height = 500
vs.scene.title = "Cylinder Cone (drag right mouse button)"
# avoid autoscale (autoscale default=True)
vs.scene.autoscale = False

# create a cone from a series of cylinders
# with increasing radius and position
len = 0.2
for r in vs.arange(0, 4.5, len):
    x = -4.5 + r/0.5
    # change blue color of (r,g,b) tuple
    b = 0 + r/5.0
    print b  # testing
    vs.cylinder(pos=(x,0,0), radius=r, length=len, color=(1-b,1-b,b))

Use your imagination.

Wow vega, thats brilliant. I never thought of doing things like that. Thanks a lot, i have the rather large task in front of me that is understanding the whole thing. So thanks again, i think thats all i need.

Yeah vega, VPython is an interesting module for 3D modeling (free too). I played around with it and came up with this:

import visual as vs

vs.scene.width = 500
vs.scene.height = 500
vs.scene.title = "draw a cone on a ring (drag with right mouse button)"
# avoid autoscale (autoscale default=True)
vs.scene.autoscale = False

ring = vs.ring(pos=(0,0,0), axis=(8,0,-3), radius=5)
ring.color = (1.0, 1.0, 0.5)
# thickness of ring by default is 1/10th of radius
ring.thickness = 0.3

cone = vs.cone(pos=(0,5.7,0), axis=(0,-1,0), radius=1.8)
cone.color = (0.75, 1.0, 0.2)
commented: wow! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.