3D Cube Create With Python
3D Cube Create With Python
🎥 Watch on YouTube: [Insert your YouTube video link here]
🧠 What You’ll Learn
In this tutorial, we’ll use Python and Kivy to create a mesmerizing 3D cube animation — all while enjoying a calming ASMR coding experience. Whether you're a beginner or an experienced dev, this is a perfect mix of visual satisfaction and hands-on learning.
🔧 Technologies Used
-
Python 3
-
Kivy Framework
-
-
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.graphics import Ellipse, Color, Line
import math
class CubeWidget(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.angle = 0
self.points3d = []
# 8 cube corners
for x in (-1, 1):
for y in (-1, 1):
for z in (-1, 1):
self.points3d.append((x, y, z))
# Define cube edges by index pairs
self.edges = [
(0, 1), (1, 3), (3, 2), (2, 0), # bottom face
(4, 5), (5, 7), (7, 6), (6, 4), # top face
(0, 4), (1, 5), (2, 6), (3, 7) # verticals
]
self.dots = []
self.projected = [(0, 0)] * len(self.points3d)
with self.canvas:
Color(0.9, 0.6, 0.2)
for _ in self.points3d:
self.dots.append(Ellipse(pos=(0, 0), size=(6, 6)))
self.line_color = Color(0.5, 1, 1)
self.lines = [Line(points=[],width = 2) for _ in self.edges]
Clock.schedule_interval(self.update, 1 / 60)
def update(self, dt):
self.angle += 0.02
cos_a = math.cos(self.angle)
sin_a = math.sin(self.angle)
cx = self.width / 2
cy = self.height / 2
scale = 100
distance = 3
# Project 3D points to 2D
for i, (x, y, z) in enumerate(self.points3d):
# Rotate around Y axis
x1 = x * cos_a - z * sin_a
z1 = x * sin_a + z * cos_a
# Rotate around X axis
y1 = y * cos_a - z1 * sin_a
z2 = y * sin_a + z1 * cos_a
factor = scale / (distance + z2)
px = cx + x1 * factor
py = cy + y1 * factor
self.projected[i] = (px, py)
self.dots[i].pos = (px, py)
# Update lines between connected points
for i, (start_idx, end_idx) in enumerate(self.edges):
p1 = self.projected[start_idx]
p2 = self.projected[end_idx]
self.lines[i].points = [*p1, *p2]
class CubeApp(App):
def build(self):
return CubeWidget()
if __name__ == "__main__":
CubeApp().run()
This Python Kivy project is great for learning how 3D rendering works in simple apps, and the ASMR format helps you enjoy the process mindfully. Perfect for night coding, background study sessions, or just chilling with some satisfying code visuals.
📈 SEO Keywords (Include throughout your post naturally)
-
Python Kivy 3D Animation
-
Kivy ASMR Code
-
Relaxing programming video
-
Python 3D cube example
-
ASMR Python coding
-
Kivy OpenGL 3D animation
-
Python wireframe cube
-
Best Python UI animation tutorial
Comments
Post a Comment