Making accurate scientific models is probably one of the greatest things that you can do with programming as they’re both useful tools for scientists to use and quite interesting to make as well. Now the globe that I’m making today isn’t exactly on the level of being a useful scientific tool but its the first step towards making something like that—i.e., an accurate model of the solar system.
Installing and Using VPython
To display any 3D shapes at all in Python you’re going to need to install a library since this functionality isn’t included in the standard library. For this article I’m choosing to use VPython as I think that its the best library around for Python in terms of 3D modeling but there are a number of other libraries that you could use for this, most notably Pygame.
Installing this library can be a bit annoying since the most recent version of Python that it runs on is version 2.7.9. This means that you’ll have to go and install this older version of Python, which they do link to on the download page, along with the installer for VPython. I highly recommend that you use the installer on the VPython website instead of Pip as pip may give you some trouble when installing it but their installer should work just fine.
The website for VPython also has a lot of documentation that you can go through which explains all of the ins and outs of the library in detail. Be warned though that its fairly dry reading and also the design of the site is not the greatest and so can be a bit straining on the eyes after a while. Also on the main docs page there are several links to YouTube videos on how to use the library which you may prefer to use instead of reading through site.
Creating Your Spinning Globe
Once you’ve successfully installed the library and created a new project the first thing you need to do is import all of the libraries features into the project. To do this put the following line at the top of your program:
from visual import *
Next you’ll want to set how far zoomed in the camera is onto the globe using the “scene.range()” function. Personally I like to have it set to 5 but it doesn’t really matter what you set it to as long as its visible to the user of course. After this you’re also going to want to set the camera to auto center onto the scene to ensure that you can see the globe easily.
scene.range = 5 #Sets Zoom Distance scene.autocenter = True #Auto Centers Camera on Scene
Now that you have camera set up you can go ahead and create your globe. In VPython a sphere object has 11 different properties including radius, coordinate position, and whether or not you want it to leave a trail behind. Thankfully if you don’t feel like tinkering with all of that right now you can leave all of these attributes blank and get a default standard sphere which is perfectly fine for our little program. There is one attribute you will definitely want to set though and that’s “material”. If you set this equal to “materials.earth” instead of just being gray you’re sphere will be overlayed with a texture of Earth when created.
globe = sphere(material = materials.earth) #Creates Globe and Gives it an Earth Texture
The last thing that we need to do is make our globe actually spin around. To this you’ll first need to create an infinite loop using “While:True”. Inside the loop first use the “rate()” function to specify the speed of your globes rotation to something fairly slow like 100. After that you should use the “rotate()” function on your sphere to get it to actually start rotating.
while True: # Endless Loop Continuously Rotates Globe rate(100) #Sets Animation Speed globe.rotate(angle=0.005, axis=(0, 1, 0)) #Sets Globe To Rotate
Here is an example of what your finished globe should look like roughly.
There’s no reason why you have to stop at just creating this fairly simple representation of the Earth. If you want to you can use Vpython to create an accurate model of Earths Rotation around the sun and you can take it even further and create an accurate model for the whole solar system and even give the program a space themed background as well.
Download the Source code here
If you have any questions or comments email them to me at email@example.com