-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetashapes.py
More file actions
135 lines (89 loc) · 3.32 KB
/
metashapes.py
File metadata and controls
135 lines (89 loc) · 3.32 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import bpy
import math
import random
import numpy as np
# Functions to compute grid points for a sphere, pyramid and square
def sphere(n,r):
theta =(math.pi)/n
phi = (2*math.pi)/n
vertices = []
for stack in range(n):
stackRadius = math.sin(theta*stack) * r
for slice in range(n):
x = math.cos(phi*slice) * stackRadius
y = math.sin(phi*slice) * stackRadius
z = math.cos(theta*stack) * r
vertices.append([x,y,z])
return vertices
s_pts = sphere(20,5)
def pyramid(n, scale):
vertices = []
for z in np.arange(-n,n,2):
for y in np.arange(-z,z,2):
for x in np.arange(-z,z,2):
vertices.append([x/scale,y/scale,z/scale])
return vertices
p_pts = pyramid(40,5)
def square(n, scale):
vertices = []
for z in np.arange(-n,n,2):
for y in np.arange(-n,n,2):
for x in np.arange(-n,n,2):
vertices.append([x/scale,y/scale,z/scale])
return vertices
sq_pts = square(20,5)
# Function to sample from the grid of points created in the functions above
# I do this as I want a fixed number of locations for each shape
def shapePts(array):
locations = []
for i in range(400):
index = random.randint(0,len(array)-1)
locations.append(array[index])
return locations
# Seelecting the scene and creating metaballs
scene = bpy.context.scene
mball = bpy.data.metaballs.new('MetaBall')
obj = bpy.data.objects.new('MetaBallObj', mball)
scene.objects.link(obj)
mball.resolution = 0.2
mball.render_resolution = 0.02
locations = shapePts(s_pts)
for i in range(len(locations)):
# print(locations[i][0])
coordinate = (locations[i][0], locations[i][1], locations[i][2])
element = mball.elements.new()
element.co = coordinate
element.radius = 0.5
# Animating the metaball elements as they move from location to location for each grid shape
# Should be fairly self explanatory
currframe = bpy.context.scene.frame_start
bpy.context.scene.frame_set(currframe)
locations = shapePts(p_pts)
for i in range(len(mball.elements)):
coordinate = (locations[i][0], locations[i][1], locations[i][2])
mball.elements[i].co = coordinate
mball.elements[i].keyframe_insert(data_path='co')
bpy.context.scene.frame_set(currframe+60)
locations = shapePts(s_pts)
for i in range(len(mball.elements)):
coordinate = (locations[i][0], locations[i][1], locations[i][2])
mball.elements[i].co = coordinate
mball.elements[i].keyframe_insert(data_path='co')
bpy.context.scene.frame_set(currframe+120)
locations = shapePts(sq_pts)
for i in range(len(mball.elements)):
coordinate = (locations[i][0], locations[i][1], locations[i][2])
mball.elements[i].co = coordinate
mball.elements[i].keyframe_insert(data_path='co')
bpy.context.scene.frame_set(currframe+180)
locations = shapePts(s_pts)
for i in range(len(mball.elements)):
coordinate = (locations[i][0], locations[i][1], locations[i][2])
mball.elements[i].co = coordinate
mball.elements[i].keyframe_insert(data_path='co')
bpy.context.scene.frame_set(currframe+240)
locations = shapePts(p_pts)
for i in range(len(mball.elements)):
coordinate = (locations[i][0], locations[i][1], locations[i][2])
mball.elements[i].co = coordinate
mball.elements[i].keyframe_insert(data_path='co')