3D animation in Python: vpython

3D animation in Python vpython

Yes, apart from a Graphical User Interface i.e. GUI, python can also play with 3D animations! 3D animations make easier in python with the help of the module called vpython and some logic.

What is vpython?

vpython stands for Visual Python, gives easier functionality to create 3D animations and object which can actually be navigated! It can be able to create different shapes and based on it, you can actually create some animation.
vpython is not just only for those who are clever python programmers. No! It is totally beginner-friendly module and doesn’t need much skills or experience.
The module is basically used for education purposes, especially in Physics, to demonstrate certain phenomena like the solar system, rocket landing, pendulum, etc.

Let’s get started:

Using vpython:

  • If you’re using anaconda based jupyter notebook for coding, animation can be created within the notebook.
  • If the code is written in any other IDE with installed python or executed from command line, the animation will open inside the browser window automatically.
  • The other way to execute this, is to create account, sign in or log in, run and share the code in https://www.glowscript.org/ Glowscript allows you to run all the python code without importing any modules. It also works offline. It saves the file into the cloud storage.
assignment advertisement

Installing vpython:
For anaconda-python distribution install with code:

conda install -c vpython vpython

Apart from anaconds, install by inputting command into the terminal, as:

pip install vpython

After installation quickly import the module into your python file for vpython!
Note: If you are using Glowscript, you don’t need to externally import the module.

Let’s create a sphere.
The code is very simple. In vpython for positioning an object, vector(x,y,z) method is used.

from vpython import *

This will create a sphere with all the default values.

Isn’t it a 3D? Well, you’ll be more convinced when it can actually rotate or move!
So, here’s the thing: You can zoom in or zoom out like normally you do in windows. For rotation, right click the pointer and hold it and move the object with left hand. Here you can see:

Now, let’s change some attributes of this sphere and customize it.

  • pos : Positioning the center of sphere with vector(x,y,z)
  • axis : axis of alignment of sphere or rotation.
  • color : Color of sphere. vpython basically contains some colors. It provides only main and usual colors in class called color and to access it we have to use color.color-name.
  • opacity : opactiy ranges from 0 (least visual) to 1 (most visual)
  • radius : Radius value of sphere
  • size : Sizing the object with vector(width, height, depth)
  • texture : Sets the texture from the texture class
  • shininess : values ranges from 0 (least shiny) to 1 (most shiny)
  • emissive : can be set to True if emissive or False for not emissivity.
from vpython import *
sph=sphere(pos =vector(0,0,0), color=vector(1,2,5), shininess = 1, opacity=0.6, radius =2, size=vector(1,1,1), texture=textures.stucco)
i = 1
    rate(10) #no. of loops per second

With the help of some simple looping logic, we just try to move the sphere with updated attributes.
rate() : used for no. of loops per second
To increasing only x parameter in the position, sph.pos.x is used. ‘.’ represents the attribute.

Now, thus you can play around with the sphere with some logic.
Now, you can create a Solar system with it!

Now, let’s move this sphere like a bounce ball in between two walls. Walls can be created using box as same as this sphere!

wall1 = box(pos=vector(2,0,0),size=vector(0.1,1,1), color=vector(3,4,4))
wall2 = box(pos=vector(-2,0,0),size=vector(0.1,1,1), color=vector(3,4,4))

Here, the sphere ‘my_sphere’ is moving between two walls, ‘wall1’ and ‘wall2’. To avoid the sphere to look like it’s actually going inside the wall and then moving sidewise, we’ve introduce ‘edge’ to make it like it is bouncing!

Similarily, you can create cone, arrow, cylinder, helix, ellipsoid, spring, points, pyramid, text, etc. with the help of vpython.

Now, let’s give a try to 3D text:

from vpython import *

Attributes with text:

  • pos : position in vector(x,y,z)
  • align : align in terms of ‘left’, ‘right’, ‘center’
  • height
  • length
  • depth
  • font : font-style eg. ‘Sans-Serif’
  • background : bg color
  • billboard : It sets to True if the text will face you or False if not
  • opacity: opacity of text
  • shininess : ranges from 0 to 1
  • emissive : True for emissivity and False for not emissive

Other attributes includes spacing, start, end position, etc.

text(text = "Welcome", pos=vector(-2,-1,0),color = vector(2,3,6),opacity = 0.5,shininess = 1,emissive = True, font="serif") 

This text can be also rotated just like sphere and also zoom in and out!

Similarily, you can try such animations with other objects and create magic!

For documentation : https://vpython.org/contents/doc.html
For GlowScript: https://www.glowscript.org/

Thank You!

Also Read:

Browse Post Tags-

Avatar of ishika dabare

Author: ishika dabare