-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
271 lines (243 loc) · 9.69 KB
/
script.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import mdl
from display import *
from matrix import *
from draw import *
"""======== first_pass( commands ) ==========
Checks the commands array for any animation commands
(frames, basename, vary)
Should set num_frames and basename if the frames
or basename commands are present
If vary is found, but frames is not, the entire
program should exit.
If frames is found, but basename is not, set name
to some default value, and print out a message
with the name being used.
==================== """
def first_pass( commands ):
name = 'animation'
num_frames = 1
basename = False
frames = False
vary = False
for instruction in commands:
if(instruction['op'] == 'basename'):
basename = True
name = instruction['args'][0]
if(instruction['op'] == 'frames'):
frames = True
num_frames = int(instruction['args'][0])
if(instruction['op'] == 'vary'):
vary = True
if vary and (not frames):
exit()
if frames and (not basename):
print("No basename given, so default is \'animation\'")
return (name, num_frames)
"""======== second_pass( commands ) ==========
In order to set the knobs for animation, we need to keep
a seaprate value for each knob for each frame. We can do
this by using an array of dictionaries. Each array index
will correspond to a frame (eg. knobs[0] would be the first
frame, knobs[2] would be the 3rd frame and so on).
Each index should contain a dictionary of knob values, each
key will be a knob name, and each value will be the knob's
value for that frame.
Go through the command array, and when you find vary, go
from knobs[0] to knobs[frames-1] and add (or modify) the
dictionary corresponding to the given knob with the
appropirate value.
===================="""
def second_pass( commands, num_frames ):
knobcommands = []
frames = [ {} for i in range(num_frames) ]
knobvals = {}
saved_knobs = {}
for instruction in commands:
if instruction['op'] == 'vary':
print(instruction)
inc = (instruction['args'][3] - instruction['args'][2]) / (instruction['args'][1] - instruction['args'][0])
for i in range(int(instruction['args'][0]), int(instruction['args'][1])+1):
frames[i][instruction['knob']] = instruction['args'][2] + inc * (i - instruction['args'][0])
if instruction['op'] == 'set':
knobvals[instruction['knob']] = instruction['args'][0]
if instruction['op'] == 'save_knobs':
saved_knobs[instruction['knob_list']] = knobvals.copy()
if instruction['op'] == 'tween':
k1 = saved_knobs[instruction['knob_list0']]
k2 = saved_knobs[instruction['knob_list1']]
for name in knobvals:
if name not in k1 or name not in k2:
continue
commands.append({
'op': 'vary',
'args': [
instruction['args'][0],
instruction['args'][1],
k1[name],
k2[name],
],
'knob': name
})
return frames
def run(filename):
"""
This function runs an mdl script
"""
p = mdl.parseFile(filename)
if p:
(commands, symbols) = p
else:
print("Parsing failed.")
return
view = [0,
0,
1];
ambient = [50,
50,
50]
light = [[0.5,
0.75,
1],
[255,
255,
255]]
color = [0, 0, 0]
symbols['.white'] = ['constants',
{'red': [0.2, 0.5, 0.5],
'green': [0.2, 0.5, 0.5],
'blue': [0.2, 0.5, 0.5]}]
reflect = '.white'
(name, num_frames) = first_pass(commands)
frames = second_pass(commands, num_frames)
for frame in range(int(num_frames)):
if num_frames > 1:
for knob in frames[frame]:
symbols[knob] = ['knob', frames[frame][knob]]
tmp = new_matrix()
ident( tmp )
stack = [ [x[:] for x in tmp] ]
screen = new_screen()
zbuffer = new_zbuffer()
tmp = []
step_3d = 100
consts = ''
coords = []
coords1 = []
for command in commands:
print(command)
c = command['op']
args = command['args']
knob_value = 1
if c == 'box':
if command['constants']:
reflect = command['constants']
add_box(tmp,
args[0], args[1], args[2],
args[3], args[4], args[5])
matrix_mult( stack[-1], tmp )
draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
tmp = []
reflect = '.white'
elif c == 'sphere':
if command['constants']:
reflect = command['constants']
add_sphere(tmp,
args[0], args[1], args[2], args[3], step_3d)
matrix_mult( stack[-1], tmp )
draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
tmp = []
reflect = '.white'
elif c == 'torus':
if command['constants']:
reflect = command['constants']
add_torus(tmp,
args[0], args[1], args[2], args[3], args[4], step_3d)
matrix_mult( stack[-1], tmp )
draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
tmp = []
reflect = '.white'
elif c == 'cone':
if command['constants']:
reflect = command['constants']
add_cone(tmp,
args[0], args[1], args[2], args[3], args[4], step_3d)
matrix_mult( stack[-1], tmp )
draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
tmp = []
reflect = '.white'
elif c == 'cylinder':
if command['constants']:
reflect = command['constants']
add_cylinder(tmp,
args[0], args[1], args[2], args[3], args[4], step_3d)
matrix_mult( stack[-1], tmp )
draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
tmp = []
reflect = '.white'
elif c == 'pyramid':
if command['constants']:
reflect = command['constants']
add_pyramid(tmp,
args[0], args[1], args[2], args[3], args[4])
matrix_mult( stack[-1], tmp )
draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
tmp = []
reflect = '.white'
elif c == 'line':
add_edge(tmp,
args[0], args[1], args[2], args[3], args[4], args[5])
matrix_mult( stack[-1], tmp )
draw_lines(tmp, screen, zbuffer, color)
tmp = []
elif c == 'move':
if command['knob']:
tmp = make_translate(args[0] * symbols[command['knob']][1], args[1] * symbols[command['knob']][1], args[2] * symbols[command['knob']][1])
else:
tmp = make_translate(args[0], args[1], args[2])
matrix_mult(stack[-1], tmp)
stack[-1] = [x[:] for x in tmp]
tmp = []
elif c == 'scale':
if command['knob']:
tmp = make_scale(args[0] * symbols[command['knob']][1], args[1] * symbols[command['knob']][1], args[2] * symbols[command['knob']][1])
else:
tmp = make_scale(args[0], args[1], args[2])
matrix_mult(stack[-1], tmp)
stack[-1] = [x[:] for x in tmp]
tmp = []
elif c == 'rotate':
if command['knob']:
theta = args[1] * (math.pi/180) * symbols[command['knob']][1]
else:
theta = args[1] * (math.pi/180)
if args[0] == 'x':
tmp = make_rotX(theta)
elif args[0] == 'y':
tmp = make_rotY(theta)
else:
tmp = make_rotZ(theta)
matrix_mult( stack[-1], tmp )
stack[-1] = [ x[:] for x in tmp]
tmp = []
elif c == 'mesh':
if command['constants']:
reflect = command['constants'] if command['constants'] != ':' else '.white'
add_mesh(tmp, command['cs'])
matrix_mult(stack[-1], tmp)
draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
tmp = []
reflect = '.white'
tmp = []
elif c == 'push':
stack.append([x[:] for x in stack[-1]] )
elif c == 'pop':
stack.pop()
elif c == 'display':
display(screen)
elif c == 'save':
save_extension(screen, args[0] + '.png')
# end operation loop
if num_frames > 1:
save_extension(screen, "anim/" + name + "%03d"%frame + '.png')
if num_frames > 1:
make_animation(name)