From: Symion <sy...@pr...> - 2008-03-24 15:12:45
|
Hi there. Here is a short python program that draws a simple face with roving eyes. As you right mouse the image, the eyes follow you. Change the position of the eyes with the cursor keys. Next step is to develop a topographic head with moving eyelids and eyebrows, then a little more work on the mouth and hey presto - A robotic head! Demonstrates how easy Vpython is to use. My Wish List: Figure out how to use the *scene.lights* function, so that orbiting objects are illuminated from a single source. i.e. Planets orbiting the Sun must change the angle of illumination. I've tried a few ideas but so far nothing works! Does anyone have a solution? Cheers Symion from __future__ import division from visual import * from random import * print '''Crazy Eyeballs -(c) Symion 2008 Use cursor keys to control eye position and width''' class eyeball: def __init__(self,p=(0,0,0),r=1.0,c=[(1,0,0)]*6,a=(0,1,0)): self.pos=p self.radius=r self.color=c self.axis=a self.eye=frame() sphere(frame=self.eye,pos=p,radius=r,axis=a,color=c[0]) for a in range(1,6): sphere(frame=self.eye,pos=vector(scene.objects[-1].pos.x+scene.objects[-1].radius/1.618,scene.objects[-1].pos.y,scene.objects[-1].pos.z),radius=scene.objects[-1].radius/1.618,color=c[a]) def look(self,k=True,s=1.0): if k==True and s!=0: self.eye.rotate(angle=((2*pi)/s)) self.eye.axis=(scene.mouse.camera-self.eye.pos) ## d=[(3,3,3)] for a in range(1,5): d.append((uniform(0,1),uniform(0,1),uniform(0,1))) d.append((0,0,0)) ## Initialize Display scene.autoscale=0 scene.title='Crazy EyeBalls - (c) 2008 Symion' scene.background=(0,0.1,0.2) scene.ambient=0.5 ## Begin eyesize=1.4 lefteye=eyeball(p=(0,0,0),r=eyesize,a=(1,0,0),c=d) rhyteye=eyeball(p=(0,0,0),r=eyesize,a=(1,0,0),c=d) ## Draw Face - Eyebrows box(pos=(0,5,-3),length=4,width=1,height=1,axis=(-1,-1,2),color=(0.4,0.4,0.4)) obj=scene.objects box(pos=vector(obj[-1].pos.x+1.5,obj[-1].pos.y,obj[-1].pos.z-3),length=3,width=1,height=1,axis=(-1,1,2),color=(0.4,0.4,0.4)) box(pos=(0,5,3),length=4,width=1,height=1,axis=(1,1,2),color=(0.4,0.4,0.4)) obj=scene.objects box(pos=vector(obj[-1].pos.x+1.5,obj[-1].pos.y,obj[-1].pos.z+3),length=3,width=1,height=1,axis=(1,-1,2),color=(0.4,0.4,0.4)) ## Nose box(pos=vector(-1,0,0),length=5,width=1,height=1,axis=(.5,1,0),color=(1,1,1)) obj=scene.objects box(pos=vector(obj[-1].pos.x-.5,obj[-1].pos.y-1.8,obj[-1].pos.z),length=1,width=3,height=1,axis=(0,1,0),color=(1,1,1)) obj=scene.objects ## Lips box(pos=vector(obj[-1].pos.x,obj[-1].pos.y-3,obj[-1].pos.z),length=.5,width=4,height=.8,color=(1,0,0)) box(pos=vector(obj[-1].pos.x,obj[-1].pos.y-2,obj[-1].pos.z),length=.5,width=5,height=.5,color=(1,0,0)) ## Position Eyes lefteye.eye.pos=(0,2.0,-2.5) rhyteye.eye.pos=(0,2.0, 2.5) ## Select viewing angle scene.forward=vector(14.1868312021088, -3.63010737884212, 0.00013678986975335) scene.range=vector(8,8,8) scene.fov=1 while 1: rate(100) lefteye.look(20) rhyteye.look(20) if scene.kb.keys>0: kb=scene.kb.getkey() if kb=='down': lefteye.eye.pos.y-=0.5 rhyteye.eye.pos.y-=0.5 elif kb=='up': lefteye.eye.pos.y+=0.5 rhyteye.eye.pos.y+=0.5 elif kb=='left': lefteye.eye.pos.z-=0.5 rhyteye.eye.pos.z+=0.5 elif kb=='right': lefteye.eye.pos.z+=0.5 rhyteye.eye.pos.z-=0.5 else: pass else: pass |