-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanim.py
179 lines (149 loc) · 6.06 KB
/
anim.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import bpy
import mathutils
import math
from math import radians
from mathutils import Matrix
# When DEBUG = True, this sets up a change-frame callback
# that modifies the scene to be the current state of the animation,
# deleting every object that doesn't have the 'pres' attribute set.
# When DEBUG = False, it actually renders the animation to a bunch of frames.
# I seem to be unable to use blender's own animation rendering pipeline since
# it doesn't have the context around for me to create primitive objects in??
DEBUG = False
SF = 5 # scale factor; ratio of how wide torus is to how thick it is
MAXF = 60 # number of frames of animation
C = bpy.context
D = bpy.data
scene = C.scene
scene.frame_start = 1
scene.frame_end = MAXF
# Uniform vector
def uni(x):
return ((x, x, x))
# Make a circle bezier curve with a name
def circ(name, **kwargs):
bpy.ops.curve.primitive_bezier_circle_add(**kwargs)
C.active_object.name = name
# kinda surprised this is so hard. I would expect a 'make this bezier
# not cyclic but make sure it has the same shape' operation to exist
# as a UI-exposed option, but I haven't found it
def decycle(sp):
save_left = sp.bezier_points[0].handle_left.copy()
save_right = sp.bezier_points[0].handle_right.copy()
sp.use_cyclic_u = False
sp.bezier_points.add(1)
sp.bezier_points[len(sp.bezier_points)-1].co = sp.bezier_points[0].co.copy()
sp.bezier_points[len(sp.bezier_points)-1].handle_left = save_left
sp.bezier_points[len(sp.bezier_points)-1].handle_right = save_right
sp.bezier_points[0].handle_left_type = 'FREE'
sp.bezier_points[0].handle_right_type = 'FREE'
sp.bezier_points[0].handle_left = save_left
sp.bezier_points[0].handle_right = save_right
def build_scene(scene, time, etime):
# time ∈ [0, 1]
# Delete everything. Not 100% sure I'm deleting enough everything to
# avoid leaks, but I'm doing my best to cover the stuff I know about.
for ob in scene.objects:
ob.select_set(True)
if 'pres' in ob:
ob.select_set(False)
bpy.ops.object.delete()
for block in D.curves:
if block.users == 0:
D.curves.remove(block)
for block in D.cameras:
if block.users == 0:
D.cameras.remove(block)
for block in D.meshes:
if block.users == 0:
D.meshes.remove(block)
# Set cursor to origin
scene.cursor.location = uni(0)
# parent
bpy.ops.object.empty_add(type='PLAIN_AXES')
C.view_layer.objects.active.name = 'Parent'
# Ring
bpy.ops.curve.primitive_bezier_circle_add(radius=1 - 1./(SF-1), location=uni(0))
C.view_layer.objects.active.name = 'c1'
bpy.ops.curve.primitive_bezier_circle_add(radius=1 + 1./(SF-1), location=uni(0))
C.view_layer.objects.active.name = 'c2'
bpy.data.objects['c1'].rotation_euler = mathutils.Euler((radians(180),0,0), 'XYZ')
D.objects['c1'].select_set(True)
D.objects['c2'].select_set(True)
bpy.ops.object.join()
C.view_layer.objects.active.name = 'Ring'
D.objects['Ring'].data.resolution_u = 24 # up from default 12
# Outer
circ('Outer', radius=SF)
D.objects['Outer'].data.bevel_mode = 'OBJECT'
D.objects['Outer'].data.bevel_object = D.objects['Ring']
D.objects['Outer'].data.resolution_u = 24 # up from default 12
decycle(D.objects['Outer'].data.splines[0])
D.objects['Outer'].data.bevel_factor_end = etime
D.objects['Outer'].data.bevel_factor_start = 0
D.objects['Outer'].data.use_fill_caps = True
# end caps
bpy.ops.mesh.primitive_torus_add(major_radius=1, minor_radius=1./(SF-1), location=((-SF, 0, 0)),
rotation=mathutils.Euler((radians(90), 0, 0), 'XYZ'))
C.view_layer.objects.active.name = 'Cap1'
for p in bpy.data.objects['Cap1'].data.polygons:
p.use_smooth = True
bpy.ops.mesh.primitive_torus_add(major_radius=1, minor_radius=1./(SF-1), location=((-SF, 0, 0)),
rotation=mathutils.Euler((radians(90), 0, 0), 'XYZ'))
C.view_layer.objects.active.name = 'Cap2'
for p in bpy.data.objects['Cap2'].data.polygons:
p.use_smooth = True
obj = bpy.data.objects['Cap2']
mat = Matrix.Rotation(-etime * radians(360), 4, 'Z')
obj.matrix_world = mat @ obj.matrix_world
# Parenting
D.objects['Outer'].parent = D.objects['Parent']
D.objects['Cap1'].parent = D.objects['Parent']
D.objects['Cap2'].parent = D.objects['Parent']
# Materials
bpy.data.objects['Outer'].active_material = bpy.data.materials['Red']
bpy.data.objects['Cap1'].active_material = bpy.data.materials['Red']
bpy.data.objects['Cap2'].active_material = bpy.data.materials['Red']
# We get Z-fighting at time zero
if time == 0:
bpy.data.objects['Cap2'].select_set(True)
bpy.ops.object.delete()
obj = bpy.data.objects['Parent']
mat = Matrix.Translation(((1. - etime) * SF, 0, 0))
obj.matrix_world = mat @ obj.matrix_world
mat = Matrix.Rotation(-etime * radians(90), 4, 'X')
obj.matrix_world = mat @ obj.matrix_world
mat = Matrix.Scale(math.pow(1. / SF, etime), 4)
obj.matrix_world = mat @ obj.matrix_world
# for area in bpy.context.screen.areas:
# if area.type == 'VIEW_3D':
# override = bpy.context.copy()
# override['area'] = area
# bpy.ops.view3d.viewnumpad(override, type = 'CAMERA')
# break
# scene.camera = D.objects['Camera']
# for area in bpy.context.screen.areas:
# if area.type == 'VIEW_3D':
# area.spaces[0].region_3d.view_perspective = 'CAMERA'
def ease(x):
return 6*x*x*x*x*x - 15*x*x*x*x + 10*x*x*x
def handler(scene):
t = (scene.frame_current - 1) / MAXF
build_scene(scene, t, ease(t))
bpy.app.handlers.frame_change_pre.clear()
if DEBUG:
bpy.app.handlers.frame_change_pre.append(handler)
else:
bpy.context.scene.render.filepath = '/tmp/frames/'
for n in range(MAXF):
scene.frame_start = n
scene.frame_end = n
t = n * 1.0 / MAXF
build_scene(scene, t, ease(t))
# bpy.context.scene.render.resolution_x = 1920
# bpy.context.scene.render.resolution_y = 1080
bpy.context.scene.render.resolution_x = 3840
bpy.context.scene.render.resolution_y = 2160
C.scene.render.image_settings.file_format = 'PNG'
C.scene.cycles.samples = 256
bpy.ops.render.render(animation=True)