Repository navigation
Expand file tree
/
Copy pathscript.py
More file actions
205 lines (184 loc) · 6.54 KB
/
Copy pathscript.py
File metadata and controls
205 lines (184 loc) · 6.54 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
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
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 = ''
num_frames = 1
for command in commands:
if command['op'] == 'basename':
name = command['args'][0]
if command['op'] == 'frames':
num_frames = command['args'][0]
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
appropriate value.
===================="""
def second_pass( commands, num_frames ):
frames = [ {} for i in range(int(num_frames)) ]
for command in commands:
if command['op'] == 'vary':
knob = command['knob']
args = command['args']
length = args[1] - args[0]
speed = args[3] - args[2]
change = speed / length
for i in range(int(length)):
frames[int(i + args[0])][knob] = args[2] + change * i
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)
tmp = new_matrix()
ident( tmp )
stack = [ [x[:] for x in tmp] ]
screen = new_screen()
zbuffer = new_zbuffer()
tmp = []
step_3d = 100
consts = ''
coords = []
coords1 = []
i = 0
for frame in frames:
symbols.update(frame)
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 == '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':
vel = 1
if command['knob'] is not None:
vel = symbols[command['knob']]
tmp = make_translate(args[0] * vel, args[1] * vel, args[2] * vel)
matrix_mult(stack[-1], tmp)
stack[-1] = [x[:] for x in tmp]
tmp = []
elif c == 'scale':
vel = 1
if command['knob'] is not None:
vel = symbols[command['knob']]
tmp = make_scale(args[0] * vel, args[1] * vel, args[2] * vel)
matrix_mult(stack[-1], tmp)
stack[-1] = [x[:] for x in tmp]
tmp = []
elif c == 'rotate':
vel = 1
if command['knob'] is not None:
vel = symbols[command['knob']]
theta = args[1] * (math.pi/180)
if args[0] == 'x':
tmp = make_rotX(theta * vel)
elif args[0] == 'y':
tmp = make_rotY(theta * vel)
else:
tmp = make_rotZ(theta * vel)
matrix_mult( stack[-1], tmp )
stack[-1] = [ x[:] for x in tmp]
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])
# end operation loop
print('/anim/' + name + "%03d"%i)
save_extension(screen, 'anim/' + name + "%03d"%i + '.gif')
i += 1