-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathtetrahedron_fractal.py
77 lines (61 loc) · 2.32 KB
/
tetrahedron_fractal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import bpy
import bmesh
import numpy as np
from mathutils import Vector, Matrix
from math import sqrt
import itertools as it
import utils
def tetrahedron_points(r=1, origin=(0, 0, 0)):
origin = Vector(origin)
# Formulas from http://mathworld.wolfram.com/RegularTetrahedron.html
a = 4*r/sqrt(6)
points = [( sqrt(3)*a/3, 0, -r/3), \
(-sqrt(3)*a/6, -0.5*a, -r/3), \
(-sqrt(3)*a/6, 0.5*a, -r/3), \
(0, 0, sqrt(6)*a/3 - r/3)]
points = [Vector(p) + origin for p in points]
return points
def recursive_tetrahedron(bm, points, level=0):
sub_tetras = []
for i in range(len(points)):
p0 = points[i]
pK = points[:i] + points[i + 1:]
sub_tetras.append([p0] + [(p0 + p)/2 for p in pK])
if 0 < level:
for subTetra in sub_tetras:
recursive_tetrahedron(bm, subTetra, level-1)
else:
for subTetra in sub_tetras:
verts = [bm.verts.new(p) for p in subTetra]
faces = [bm.faces.new(face) for face in it.combinations(verts, 3)]
bmesh.ops.recalc_face_normals(bm, faces=faces)
if __name__ == '__main__':
# Remove all elements
utils.remove_all()
# Creata fractal tetrahedron
bm = bmesh.new()
tetrahedron_base_points = tetrahedron_points(5)
recursive_tetrahedron(bm, tetrahedron_base_points, level=4)
# Create obj and mesh from bmesh object
mesh = bpy.data.meshes.new("TetrahedronMesh")
bm.to_mesh(mesh)
bm.free()
obj = bpy.data.objects.new("Tetrahedron", mesh)
bpy.context.collection.objects.link(obj)
# Create camera and lamp
target = utils.create_target((0, 0, 1))
utils.create_camera((-8, 10, 5), target, type='ORTHO', ortho_scale=10)
utils.create_light((10, -10, 10), target=target, type='SUN', energy=100)
# Select colors
palette = [(181, 221, 201, 255), (218, 122, 61, 255)]
palette = [utils.colorRGB_256(color) for color in palette] # Adjust color to Blender
# Set background color of scene
bpy.context.scene.world.node_tree.nodes["Background"] \
.inputs[0].default_value = palette[0]
# Set material for object
mat = utils.create_material(palette[1])
obj.data.materials.append(mat)
# Render scene
utils.render(
'rendering', 'tetrahedron_fractal', 500, 500,
render_engine='CYCLES')