{"id":7206,"date":"2021-02-09T17:12:58","date_gmt":"2021-02-09T11:42:58","guid":{"rendered":"https:\/\/copyassignment.com\/?p=7206"},"modified":"2022-12-02T16:50:14","modified_gmt":"2022-12-02T11:20:14","slug":"3d-animation-in-python-vpython","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/3d-animation-in-python-vpython\/","title":{"rendered":"3D animation in Python: vpython"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is vpython?<\/strong><\/h2>\n\n\n\n<p>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.<br>vpython is not just only for those who are clever python programmers. No! It is totally beginner-friendly module and doesn&#8217;t need much skills or experience.<br>The module is basically used for education purposes, especially in Physics, to demonstrate certain phenomena like the solar system, rocket landing, pendulum, etc. <\/p>\n\n\n\n<p>Let&#8217;s get started:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\"><strong>Using vpython:<\/strong><\/h2>\n<\/div><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you&#8217;re using anaconda based jupyter notebook for coding, animation can be created within the notebook.<\/li>\n\n\n\n<li>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.<\/li>\n\n\n\n<li>The other way to execute this, is to create account, sign in or log in, run and share the code in <a href=\"https:\/\/www.glowscript.org\/\">https:\/\/www.glowscript.org\/<\/a>  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.<\/li>\n<\/ul>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p><strong><span style=\"text-decoration: underline\">Installing vpython:<\/span><\/strong><br>For anaconda-python distribution install with code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>conda install -c vpython vpython<\/code><\/pre>\n\n\n\n<p>Apart from anaconds, install by inputting command into the terminal, as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install vpython<\/code><\/pre>\n\n\n\n<p>After installation quickly import the module into your python file for vpython!<br><span style=\"text-decoration: underline\">Note: <\/span>If you are using Glowscript, you don&#8217;t need to externally import the module.<\/p>\n\n\n\n<p>Let&#8217;s create a sphere. <br>The code is very simple. In vpython for positioning an object, vector(x,y,z) method is used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from vpython import *\nsphere()<\/code><\/pre>\n\n\n\n<p>This will create a sphere with all the default values.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/basic_sphere.jpg\" alt=\"\" class=\"wp-image-19610 lazyload\" width=\"677\" height=\"467\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/basic_sphere.jpg 982w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/basic_sphere-300x207.jpg 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/basic_sphere-768x529.jpg 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/basic_sphere-675x465.jpg 675w\" data-sizes=\"(max-width: 677px) 100vw, 677px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 677px; --smush-placeholder-aspect-ratio: 677\/467;\" \/><\/figure>\n\n\n\n<p>Isn&#8217;t it a 3D? Well, you&#8217;ll be more convinced when it can actually rotate or move! <br>So, here&#8217;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. <strong>Here you can see:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"720\" style=\"aspect-ratio: 1280 \/ 720;\" width=\"1280\" controls src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/sphere1.webm\"><\/video><\/figure>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>Now, let&#8217;s change some attributes of this sphere and customize it. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>pos : Positioning the center of sphere with vector(x,y,z)<\/li>\n\n\n\n<li>axis : axis of alignment of sphere or rotation.<\/li>\n\n\n\n<li>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.<\/li>\n\n\n\n<li>opacity : opactiy ranges from 0 (least visual) to 1 (most visual)<\/li>\n\n\n\n<li>radius : Radius value of sphere<\/li>\n\n\n\n<li>size : Sizing the object with vector(width, height, depth)<\/li>\n\n\n\n<li>texture : Sets the texture from the texture class<\/li>\n\n\n\n<li>shininess : values ranges from 0 (least shiny) to 1 (most shiny)<\/li>\n\n\n\n<li>emissive : can be set to True if emissive or False for not emissivity.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from vpython import *\nsph=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)\ni = 1\ndx=0.1\nwhile(i&lt;=1000):\n    rate(10) #no. of loops per second\n    sph.pos.x=sph.pos.x+dx\n    i+=1<\/code><\/pre>\n\n\n\n<p>With the help of some simple looping logic, we just try to move the sphere with updated attributes. <br>rate() : used for no. of loops per second<br>To increasing only x parameter in the position, sph.pos.x is used. &#8216;.&#8217; represents the attribute.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"720\" style=\"aspect-ratio: 1280 \/ 720;\" width=\"1280\" controls src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/sphere2.webm\"><\/video><\/figure>\n\n\n\n<p>Now, thus you can play around with the sphere with some logic. <br>Now, you can create a Solar system with it!<\/p>\n\n\n\n<p>Now, let&#8217;s move this sphere like a bounce ball in between two walls. Walls can be created using box as same as this sphere! <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_sphere=sphere(pos=vector(0,0,0,),radius=0.25,color=vector(1,2,4),shininess=1)\nwall1 = box(pos=vector(2,0,0),size=vector(0.1,1,1), color=vector(3,4,4))\nwall2 = box(pos=vector(-2,0,0),size=vector(0.1,1,1), color=vector(3,4,4))\nedge1=wall1.pos.x-wall1.size.x\/2\nedge2=wall2.pos.x-wall2.size.x\/2\ni=1\ndx=0.1\nwhile(i&lt;=1000):\n    rate(10)\n    if(my_sphere.pos.x+my_sphere.radius&gt;=edge1)or(my_sphere.pos.x-my_sphere.radius&lt;=edge2):\n        dx=-dx\n    my_sphere.pos.x=my_sphere.pos.x+dx\n    i=i+1\n<\/code><\/pre>\n\n\n\n<p>Here, the sphere &#8216;my_sphere&#8217; is moving between two walls, &#8216;wall1&#8217; and &#8216;wall2&#8217;. To avoid the sphere to look like it&#8217;s actually going inside the wall and then moving sidewise, we&#8217;ve introduce &#8216;edge&#8217; to make it like it is bouncing!<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"720\" style=\"aspect-ratio: 1280 \/ 720;\" width=\"1280\" controls src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/sphere3.webm\"><\/video><\/figure>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>Similarily, you can create cone, arrow, cylinder, helix, ellipsoid, spring, points, pyramid, text, etc. with the help of vpython.<\/p>\n\n\n\n<p>Now, let&#8217;s give a try to 3D text:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from vpython import *\ntext(text=\"Welcome\")<\/code><\/pre>\n\n\n\n<p>Attributes with text:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>pos : position in vector(x,y,z)<\/li>\n\n\n\n<li>align : align in terms of &#8216;left&#8217;, &#8216;right&#8217;, &#8216;center&#8217;<\/li>\n\n\n\n<li>height<\/li>\n\n\n\n<li>length<\/li>\n\n\n\n<li>depth<\/li>\n\n\n\n<li>font : font-style eg. &#8216;Sans-Serif&#8217;<\/li>\n\n\n\n<li>background : bg color<\/li>\n\n\n\n<li>billboard : It sets to True if the text will face you or False if not<\/li>\n\n\n\n<li>opacity: opacity of text<\/li>\n\n\n\n<li>shininess : ranges from 0 to 1<\/li>\n\n\n\n<li>emissive : True for emissivity and False for not emissive<\/li>\n<\/ul>\n\n\n\n<p>Other attributes includes spacing, start, end position, etc.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text(text = \"Welcome\", pos=vector(-2,-1,0),color = vector(2,3,6),opacity = 0.5,shininess = 1,emissive = True, font=\"serif\") \n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/text1.jpg\" alt=\"\" class=\"wp-image-19616 lazyload\" width=\"648\" height=\"453\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/text1.jpg 984w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/text1-300x210.jpg 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/text1-768x537.jpg 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/text1-675x472.jpg 675w\" data-sizes=\"(max-width: 648px) 100vw, 648px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 648px; --smush-placeholder-aspect-ratio: 648\/453;\" \/><\/figure>\n\n\n\n<p>This text can be also rotated just like a sphere and also zoom in and out!<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"720\" style=\"aspect-ratio: 1280 \/ 720;\" width=\"1280\" controls src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/10\/text.webm\"><\/video><\/figure>\n\n\n\n<p>Similarily, you can try such animations with other objects and create magic!<\/p>\n\n\n\n<p>For documentation : <a href=\"https:\/\/vpython.org\/contents\/doc.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/vpython.org\/contents\/doc.html<\/a><br>For GlowScript: <a href=\"https:\/\/www.glowscript.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.glowscript.org\/<\/a><\/p>\n\n\n\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-9886351916045880\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-9886351916045880\"\n     data-ad-slot=\"7933252109\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>Thank You!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>Also Read:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/create-your-own-chatgpt-with-python\/\">Create your own ChatGPT with\u00a0Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/bakery-management-system-in-python-class-12-project\/\">Bakery Management System in Python | Class 12 Project<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sqlite-crud-operations-in-python\/\">SQLite | CRUD Operations in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/event-management-system-project-in-python\/\">Event Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/ticket-booking-and-management-in-python\/\">Ticket Booking and Management in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hostel-management-system-project-in-python\/\">Hostel Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sales-management-system-project-in-python\/\">Sales Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/bank-management-system-project-in-cpp\/\">Bank Management System Project in C++<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/python-download-file-from-url-4-methods\/\">Python Download File from URL | 4 Methods<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/python-programming-examples-fundamental-programs-in-python\/\">Python Programming Examples | Fundamental Programs in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/spell-checker-in-python\/\">Spell Checker in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/portfolio-management-system-in-python\/\">Portfolio Management System in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/stickman-game-in-python\/\">Stickman Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/contact-book-project-in-python\/\">Contact Book project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/loan-management-system-project-in-python\/\">Loan Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/cab-booking-system-in-python\/\">Cab Booking System in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/brick-breaker-game-in-python\/\">Brick Breaker Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/tank-game-in-python\/\">Tank game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-piano-in-python\/\">GUI Piano in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/ludo-game-in-python\/\">Ludo Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/rock-paper-scissors-game-in-python\/\">Rock Paper Scissors Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/snake-and-ladder-game-in-python\/\">Snake and Ladder Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/puzzle-game-in-python\/\">Puzzle Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/medical-store-management-system-project-in-python\/\">Medical Store Management System Project in\u00a0Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/creating-dino-game-in-python\/\">Creating Dino Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/tic-tac-toe-game-in-python\/\">Tic Tac Toe Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/test-typing-speed-using-python-app\/\">Test Typing Speed using Python App<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/moviepy-python-video-editing-library\/\">MoviePy: Python Video Editing Library<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/scientific-calculator-in-python-2\/\">Scientific Calculator in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-to-do-list-app-in-python-tkinter\/\">GUI To-Do List App in Python Tkinter<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":62,"featured_media":12579,"comment_status":"closed","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1475,1060,1403],"tags":[1236],"class_list":["post-7206","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allcategorites","category-final-year-project","category-game-in-python","category-python-projects","tag-3d-animation-in-python-vpython","wpcat-22-id","wpcat-1475-id","wpcat-1060-id","wpcat-1403-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/7206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=7206"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/7206\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media\/12579"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=7206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=7206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=7206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}