To create a beautiful 3D model of Earth using multiple Python libraries, we'll utilize:
numpy for numerical computations.
mayavi / tvtk for 3D visualization.
urllib.request for downloading Earth texture images.
os for file management.
- Optionally
PIL (Pillow) for image manipulation if needed.
✅ Features of This Model:
- High-resolution Earth texture applied to a sphere.
- Atmosphere simulation using a transparent outer sphere.
- Proper lighting and black background for realism.
- Automated texture download for convenience.
📦 Installation Notes:
Before running the code, ensure you have the required libraries installed:
bash
pip install numpy mayavi vtk pillow
Or using conda:
bash
conda install -c conda-forge numpy mayavi vtk pillow
🌍 Complete Python Code
python
import numpy as np
import os
import urllib.request
from mayavi import mlab
from tvtk.api import tvtk
# Download Earth texture if not present
texture_url = 'https://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73909/land_ocean_ice_cloud_2048.jpg'
texture_file = 'earth_texture.jpg'
if not os.path.isfile(texture_file):
print("🌐 Downloading Earth texture image...")
urllib.request.urlretrieve(texture_url, texture_file)
print("✅ Download completed.")
# Create a sphere source
sphere = tvtk.SphereSource(radius=1.0, theta_resolution=100, phi_resolution=100)
sphere.update()
# Load texture
reader = tvtk.JPEGReader()
reader.file_name = texture_file
reader.update()
# Create texture object
texture = tvtk.Texture()
texture.input_connection = reader.output_port
texture.interpolate = True # Smoother texture
# Mapper and actor for Earth
mapper = tvtk.PolyDataMapper(input_connection=sphere.output_port)
earth_actor = tvtk.Actor(mapper=mapper)
earth_actor.texture = texture
# Atmosphere effect (a semi-transparent sphere)
atmosphere = tvtk.SphereSource(radius=1.02, theta_resolution=100, phi_resolution=100)
atmosphere.update()
atmosphere_mapper = tvtk.PolyDataMapper(input_connection=atmosphere.output_port)
atmosphere_actor = tvtk.Actor(mapper=atmosphere_mapper)
atmosphere_actor.property.color = (0.3, 0.5, 1.0) # Light blue
atmosphere_actor.property.opacity = 0.2 # Transparent
# Optional: Add lighting
light = tvtk.Light()
light.intensity = 1.0
light.position = (0, 0, 1)
light.focal_point = (0, 0, 0)
# Scene setup
fig = mlab.figure(bgcolor=(0, 0, 0)) # Black background for space
scene = fig.scene
scene.add_actor(earth_actor)
scene.add_actor(atmosphere_actor)
scene.add_light(light)
# Camera view
mlab.view(azimuth=0, elevation=90, distance=5, focalpoint=(0, 0, 0))
mlab.title("Beautiful 3D Model of Earth", size=0.5)
mlab.xlabel("X")
mlab.ylabel("Y")
mlab.zlabel("Z")
# Show the 3D visualization
mlab.show()
🧠 Explanation of Key Components
- Texture Mapping: The Earth image is applied to the surface of a 3D sphere using VTK's texture mapping capabilities.
- Atmosphere: A slightly larger transparent sphere creates the illusion of Earth's atmosphere.
- Lighting: A directional light simulates sunlight coming from above.
- Camera Setup: View is centered on the equator and oriented for a clear global perspective.
📝 Notes
- You can enhance the model by adding cloud layers using a second texture.
- The Earth texture used is from NASA’s Blue Marble series (public domain).
- This visualization runs interactively in a window where you can rotate, zoom, and explore the 3D Earth.
🧪 Output
When executed, this script will:
- Download the Earth texture (if not already present).
- Render a realistic 3D Earth in a 3D viewer.
- Display a rotatable, zoomable 3D sphere with:
- Textured surface
- Atmosphere effect
- Realistic lighting
🚀 Bonus Ideas
- Add rotation animation with
mlab.animate.
- Overlay country borders or weather data.
- Use Plotly Dash to make this interactive in a web browser.
This solution combines scientific visualization and artistic rendering to create an impressive Earth model using Python.