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

Lab.1_Computer Graphics

Blender is a free and open-source 3D creation suite that supports various features including modeling, animation, and Python scripting. The document provides a guide on downloading Blender, using Python for automation and creating 3D objects, as well as understanding animation and keyframing. It includes a practical lab task to animate two spheres moving in opposite directions while staying parallel.

Uploaded by

vohev62095
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)
5 views

Lab.1_Computer Graphics

Blender is a free and open-source 3D creation suite that supports various features including modeling, animation, and Python scripting. The document provides a guide on downloading Blender, using Python for automation and creating 3D objects, as well as understanding animation and keyframing. It includes a practical lab task to animate two spheres moving in opposite directions while staying parallel.

Uploaded by

vohev62095
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/ 7

Computer

Graphics
Lab.1

First, What is Blender?


Blender is a free and open-source 3D
creation suite used for modeling,
sculpting, animation, rendering,
compositing, and even game
development. It supports a variety of
features like physics simulations,
motion tracking, and scripting with
Python, making it a powerful tool for
artists and developers.

How to Download Blender


1. Go to the official website: Blender.org
2. Click on "Download Blender"
3. Select your operating system (Windows,
macOS, or Linux).
4. Download the installer and run it.
5. Follow the installation steps to complete the
setup.

Python Scripting in Blender


Python scripting in Blender allows users to automate tasks, create custom tools, and control
Blender’s functionalities programmatically.

Why Use Python in Blender?


• Automation: Automate repetitive tasks like importing/exporting files, applying
modifiers, or setting up scenes.
• Custom Tools: Create add-ons, scripts, and UI elements tailored to your workflow.
• Procedural Modeling: Generate 3D objects using code instead of manual modeling.
• Simulation and Animation: Control object movements, physics simulations, and
procedural animations.
How to Access the Python
Console in Blender?
1. Open Blender.
2. Go to the "Scripting"
workspace (from the top
menu).
3. Open the Text Editor
You can now run Python
commands inside Blender.

Note: Always delete the default cube in the scene to clearly view your code creation.

Right-click on
the cube and
then select
"Delete."

Blender has a built-in Python API (Application Programming Interface) called bpy, that allows
users to interact with and control Blender programmatically. It provides access to Blender's
internal functions for modeling, animation, rendering, scene management, and UI scripting.

Basic bpy Structure


bpy is structured into different modules for managing various parts of Blender. Here are the
most important ones:

Module Purpose
bpy.ops Operators (perform actions like adding, deleting, modifying objects).
bpy.context Get information about the current selection, mode, and active objects.
bpy.types Define new classes and UI elements (useful for add-ons).
1. Creating a 3D Object in Blender
First, Create a new file to write the code

Then we need to import bpy

import bpy

To add a cube to the scene


1. bpy.ops → stands for Blender Operators to add object
2. bpy.ops.mesh → contains functions (operators) specific to mesh objects (mesh is a 3D
models made of vertices, edges, and faces).
3. bpy.ops.mesh.primitive → Primitive Shape Creation, primitive refers to basic
geometric shapes that can be added to the scene.
4. bpy.ops.mesh.primitive_cube_add() → Adds a Cube

bpy.ops.mesh.primitive_cube_add(): This command adds a cube to the scene at the


default location (0,0,0).

import bpy
bpy.ops.mesh.primitive_cube_add()

2. Selecting & Accessing the Object


c = bpy.context.active_object

• bpy.context.active_object: Refers to the currently selected object (in this case,


the cube we just created).
• We store this object in the variable c to manipulate it later.

Why Use bpy.context.active_object?


It is useful because after adding an object, we can modify its properties (like location, scale,
and animation).

Changing Object Position


c.location = (5,5,5)

• The .location attribute sets the X, Y, and Z coordinates of the object.

• This code moves the cube from (0,0,0) to (5,5,5).


To Run your code and view the creation on the scene

Understanding Animation & Keyframing in Blender


In Blender, animation works by changing an object's properties (like location, rotation,
scale, etc.) over time. This change is controlled using frames and keyframes.

What is a Frame?
A frame is a single moment in an animation.
Blender plays animations frame by frame, just like a movie.
• If an animation runs at 24 FPS (frames per second), it means 24 frames will be shown
every second.
• If an animation runs at 60 FPS, it means 60 frames are shown per second.

Example:
• Frame 1 = Start of the animation.
• Frame 100 = Later in the animation (depends on FPS, at 24 FPS, frame 100 ≈ 4
seconds).

What is a Keyframe?
A keyframe is a marker that saves an object's property (like position, rotation, scale) at a
specific frame.
Blender automatically generates smooth transitions between keyframes.
Example:
• At frame 1, the object is at (0,0,0).
• At frame 100, the object moves to (5,5,5).
• Blender automatically interpolates (fills in) the movement between these
frames.

Understanding the Code for Animation


c.keyframe_insert("location", frame=1)

• This saves the current position of the cube as a keyframe at frame 1.


• Keyframes are used in animations to define positions at specific frames.
Adding More Keyframes:
Let’s break down the script:

c.location = (0, 0, 0) # Move cube to (0,0,0)


c.keyframe_insert("location", frame=1) # Save position at frame 1

# Move the cube to a new position at frame 100


c.location = (5, 5, 5)
c.keyframe_insert("location", frame=100) # Save new position at frame 100

• The cube starts at (0,0,0) at frame 1.


• The cube moves to (5,5,5) at frame 100.
• Blender automatically calculates the movement between these two frames, creating
smooth animation.

What is the Timeline in Blender?


The timeline is the area in Blender where you control animation:
• It shows all frames (from frame 1 to the last frame).
• You can scrub through the timeline to see object movement.

Where is the Timeline?

How to Play Animation in Blender?


Click Play ( ) in the timeline
Full Example: Now, let's create an object that moves from one location to another and then
returns to its original location using keyframes

Solution:

import bpy # Import Blender's Python API

# Step 1: Create an Object (Cube)


bpy.ops.mesh.primitive_cube_add() # Adds a cube to the scene

# Step 2: Store the Cube Object


c = bpy.context.active_object # Get active object(created cube)

# Step 3: Set Initial Position and Insert First Keyframe


c.location = (5, 5, 5) # Set initial position of the cube
c.keyframe_insert("location", frame=1) # Insert keyframe at frame 1

# Step 4: Move to a New Position and Insert Second Keyframe


c.location = (-5, -5, -5) # Move cube to a different location
c.keyframe_insert("location", frame=100) # Insert keyframe at frame 100

# Step 5: Return to Original Position and Insert Third Keyframe


c.location = (5, 5, 5) # Move back to original position
c.keyframe_insert("location", frame=200) # Insert keyframe at frame 200

Animation on the time line will be like this:

Frame 1

Frame 100

Frame 200
Lab.1
Animating Two Spheres Moving in Opposite Directions While
Staying Parallel

Task Description
Create an animation in Blender where:

1. Two spheres (balls) are added to the scene


2. Ball 1 starts at (5,2,0) and moves to (-5,2,0) by frame 100
3. Ball 2 starts at (-5,-2,0) and moves to (5,-2,0) by frame 100
4. The animation should run from frame 1 to frame 100, making the two balls swap
positions smoothly while staying parallel.

Solution:
import bpy

# 🏀 Ball 1: Moves from (5,2,0) to (-5,2,0)


bpy.ops.mesh.primitive_uv_sphere_add() # Add first sphere
ball_1 = bpy.context.active_object # Sphere as the active object
ball_1.location = (5, 2, 0) # Set starting position
ball_1.keyframe_insert("location", frame=1) # Insert keyframe at frame 1

ball_1.location = (-5, 2, 0) # Move Ball 1 to (-5,2,0)


ball_1.keyframe_insert("location", frame=100) # Insert keyframe at frame 100

# 🏀 Ball 2: Moves from (-5,-2,0) to (5,-2,0)


bpy.ops.mesh.primitive_uv_sphere_add() # Add second sphere (Ball 2)
ball_2 = bpy.context.active_object # Get the new active object
ball_2.location = (-5, -2, 0) # Set starting position
ball_2.keyframe_insert("location", frame=1) # Insert keyframe at frame 1

ball_2.location = (5, -2, 0) # Move Ball 2 to (5,-2,0)


ball_2.keyframe_insert("location", frame=100) # Insert keyframe at frame 100

You might also like