0% found this document useful (0 votes)
12 views

Tutorial 4 - Dynamic Content Mainipulation On Blender

Uploaded by

asura042005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Tutorial 4 - Dynamic Content Mainipulation On Blender

Uploaded by

asura042005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

TUTORIAL 4 – DYNAMIC CONTENT MANIPULATION ON BLENDER

Creating a 3D solar system simulation in Blender using Python involves several steps,
including setting up the scene, defining the planets, and animating their orbits. Below is a
straightforward Blender Python script that creates a simple solar system with the Sun and
a few planets orbiting around it.

Step 1: Set Up Your Blender Project

1. Open Blender and create a new project.


2. Save your project to ensure you don’t lose your work.

Step 2: Create the Python Script

Open the Scripting tab in Blender and create a new script. Copy and paste the following
code into the script editor:

import bpy
import math
from mathutils import Vector

# Define the properties of the solar system


PLANETS = {
"Sun": {"mass": 1.989e30, "radius": 0.5, "color": (1, 1, 0),
"distance": 0, "speed": 0},
"Earth": {"mass": 5.972e24, "radius": 0.1, "color": (0, 0, 1),
"distance": 3, "speed": 1.0},
"Mars": {"mass": 6.417e23, "radius": 0.07, "color": (1, 0, 0),
"distance": 4, "speed": 0.8},
"Venus": {"mass": 4.867e24, "radius": 0.09, "color": (1, 0.85, 0),
"distance": 2.5, "speed": 1.2},
}

# Create a planet
def create_planet(name, params):
bpy.ops.mesh.primitive_uv_sphere_add(radius=params["radius"],
location=(0, 0, 0))
planet = bpy.context.object
planet.name = name
# Create material
mat = bpy.data.materials.new(name=f"{name}_Material")
mat.diffuse_color = (*params["color"], 1) # RGBA
planet.data.materials.append(mat)

# Store properties in custom attributes


planet["distance"] = params["distance"]
planet["speed"] = params["speed"]
planet["angle"] = 0 # Initialize angle for orbiting

return planet

# Animate the planets


def animate_planets(planets):
frame_start = 0
frame_end = 250 # Total frames for the animation

for planet in planets:


for frame in range(frame_start, frame_end):
# Calculate the current angle based on time
angle = planet["angle"] + planet["speed"] * frame *
(math.pi / 180) # Speed is in degrees
planet["angle"] = angle # Update angle

# Calculate new position


x = planet["distance"] * math.cos(angle)
y = planet["distance"] * math.sin(angle)
z = 0 # Keeping it 2D for simplicity

# Set keyframe for position


planet.location = (x, y, z)
planet.keyframe_insert(data_path="location", frame=frame)

# Clear existing objects


bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()
# Create the Sun and planets
planets = []
for name, params in PLANETS.items():
planet = create_planet(name, params)
planets.append(planet)

# Animate the planets


animate_planets(planets)

# Set up scene for animation


bpy.context.scene.frame_start = 0
bpy.context.scene.frame_end = 250

Step 3: Run the Script

1. Update Your Project: Ensure you have no objects in your scene that you don’t want
to delete, as the script clears the scene at the start.
2. Run the Script: Click the "Run Script" button in the script editor.

Result

• The script will create a simple solar system with the Sun, Earth, Mars, and Venus,
each represented as a sphere.
• The planets will orbit the Sun in a 2D plane over 250 frames of animation.

Notes

• You can add more planets by extending the PLANETS dictionary.


• Adjust the distance and speed values for different orbits.
• This is a basic implementation. For more realism, consider adding more complex
behaviors, like elliptical orbits or gravitational interactions.

You might also like