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:
- 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.
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 * sphere()
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 dx=0.1 while(i<=1000): rate(10) #no. of loops per second sph.pos.x=sph.pos.x+dx i+=1
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!
my_sphere=sphere(pos=vector(0,0,0,),radius=0.25,color=vector(1,2,4),shininess=1) 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)) edge1=wall1.pos.x-wall1.size.x/2 edge2=wall2.pos.x-wall2.size.x/2 i=1 dx=0.1 while(i<=1000): rate(10) if(my_sphere.pos.x+my_sphere.radius>=edge1)or(my_sphere.pos.x-my_sphere.radius<=edge2): dx=-dx my_sphere.pos.x=my_sphere.pos.x+dx i=i+1
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 * text(text="Welcome")
Attributes with text:
- pos : position in vector(x,y,z)
- align : align in terms of ‘left’, ‘right’, ‘center’
- 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 a sphere and also zoom in and out!
Similarily, you can try such animations with other objects and create magic!
- Create your own ChatGPT with Python
- Bakery Management System in Python | Class 12 Project
- SQLite | CRUD Operations in Python
- Event Management System Project in Python
- Ticket Booking and Management in Python
- Hostel Management System Project in Python
- Sales Management System Project in Python
- Bank Management System Project in C++
- Python Download File from URL | 4 Methods
- Python Programming Examples | Fundamental Programs in Python
- Spell Checker in Python
- Portfolio Management System in Python
- Stickman Game in Python
- Contact Book project in Python
- Loan Management System Project in Python
- Cab Booking System in Python
- Brick Breaker Game in Python
- Tank game in Python
- GUI Piano in Python
- Ludo Game in Python
- Rock Paper Scissors Game in Python
- Snake and Ladder Game in Python
- Puzzle Game in Python
- Medical Store Management System Project in Python
- Creating Dino Game in Python
- Tic Tac Toe Game in Python
- Test Typing Speed using Python App
- MoviePy: Python Video Editing Library
- Scientific Calculator in Python
- GUI To-Do List App in Python Tkinter