-
Notifications
You must be signed in to change notification settings - Fork 15
/
convert.py
436 lines (388 loc) · 12.7 KB
/
convert.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import re, sys
from biplist import readPlist
from pprint import pprint
import base64, os
import yaml
import json
try:
from jsmin import jsmin as jsmin_
except:
jsmin_ = lambda x: x
import httplib, urllib
basedeps = ['matrix', 'base']
jsdeps = basedeps[:]
builtins = ['QCIteratorVariables']
packing = False
nameMap = {}
nameI = 0
def genName(name):
global nameI
if not packing:
return name
if name not in nameMap:
i = nameI
nameI += 1
rep = '_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'[i % 53]
i /= 53
while i:
rep += '_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'[i % 63]
i /= 63
nameMap[name] = rep
return nameMap[name]
def pruneUserInfo(data):
if isinstance(data, list):
map(pruneUserInfo, data)
elif isinstance(data, dict):
if 'userInfo' in data:
del data['userInfo']
map(pruneUserInfo, data.values())
def fix(name):
if name.startswith('input') and len(name) > 5:
name = name[5:]
elif name.startswith('output') and len(name) > 6:
name = name[6:]
if name[0] in '0123456789':
return '_' + name
return name
def closureCompile(code):
params = urllib.urlencode([
('js_code', code),
('compilation_level', 'ADVANCED_OPTIMIZATIONS'),
('output_format', 'text'),
('output_info', 'compiled_code'),
])
headers = { "Content-type": "application/x-www-form-urlencoded" }
conn = httplib.HTTPConnection('closure-compiler.appspot.com')
conn.request('POST', '/compile', params, headers)
response = conn.getresponse()
data = response.read()
return data
def shadermin(shader):
out = [shader]
gsub = lambda x, y, m=False: out.__setitem__(0, re.sub(x, y, out[0], flags=(re.M if m else 0)|re.I))
gsub(r'//.*$', '')
gsub(r'\s+', ' ', True)
gsub(r'/\*.*?\*/', '')
gsub(r'\.0+([^0-9])', r'.\1')
gsub(r'0+([1-9]+\.[^a-z_])', r'\1')
gsub(r'0+([1-9]*\.[0-9])', r'\1')
gsub(r'\s*(;|{|}|\(|\)|=|\+|-|\*|/|\[|\]|,|\.|%|!|~|\?|:|<|>)\s*', r'\1', True)
return out[0].strip()
def processJS(code):
out = ''
while len(code):
try:
first, body = code.split('<shader>', 1)
body, code = body.split('</shader>', 1)
except:
out += code
break
out += first
out += repr(shadermin(body))
return out
def jsmin(code):
#return closureCompile(code)
return jsmin_(code)
def rewriteJS(script):
script = re.sub(r'/\*[\s\S]*?\*/', '', script, flags=re.M)
script = re.sub(r'//.*$', '', script, flags=re.M)
prolog, script = script.split('{', 1)
#params = prolog.split('main', 1)[1]
#params = params.split('(', 1)[1].split(')', 1)[0].split(',')
#params = [param.strip().split(' ')[-1] for param in params]
script = 'function(params) { with(params) { ' + script + '}'
return script
def dump(plist):
#pprint(plist)
def dumpNode(obj, tab=0):
def line(x, i=0):
print ' ' * (tab+i) + x
line('patch:')
if 'connections' in obj['state']:
connections = obj['state']['connections'].values()
else:
connections = []
nodes = obj['state']['nodes']
anodes = {}
for node in nodes:
snode = None
if 'nodes' in node['state']:
snode = node
anodes[node['key']] = (node['class'], node['identifier'] if 'identifier' in node else None, [], [], snode)
state = node['state']
if 'customInputPortStates' in state:
for name in state['customInputPortStates']:
name = fix(name)
if name not in anodes[node['key']][2]:
anodes[node['key']][2].append(name)
if 'ivarInputPortStates' in state:
for name in state['ivarInputPortStates']:
name = fix(name)
if name not in anodes[node['key']][2]:
anodes[node['key']][2].append(name)
if 'fragmentShader' in state:
anodes[node['key']][2].append('FragmentShader')
if 'vertexShader' in state:
anodes[node['key']][2].append('VertexShader')
if 'expression' in state:
anodes[node['key']][2].append('Expression')
for key in state:
anodes[node['key']][2].append('__state.' + key)
for conn in connections:
dest = fix(conn['destinationPort'])
if dest not in anodes[conn['destinationNode']][2]:
anodes[conn['destinationNode']][2].append(dest)
src = fix(conn['sourcePort'])
if src not in anodes[conn['sourceNode']][3]:
anodes[conn['sourceNode']][3].append(src)
for name, (cls, format, inp, out, snode) in anodes.items():
line('%s(%s%s):' % (name, cls, '.' + format if format else ''), 1)
if inp:
line('in:', 2)
for elem in inp:
line('- %s' % elem, 3)
if out:
line('out:', 2)
for elem in out:
line('- %s' % elem, 3)
if snode:
dumpNode(snode, tab+2)
dumpNode(plist['rootPatch'])
print
class Node(object):
_inports = ''
_outports = ''
def __init__(self):
self.format = None
self.cls = self.__class__.__name__
if self.cls not in jsdeps:
jsdeps.append(self.cls)
self.outports = {}
for port in self._outports:
port = fix(str(port))
if port not in self.outports:
self.outports[port] = OutPort(self, port)
self.inports = {}
for port in self._inports:
port = fix(str(port))
if port not in self.inports:
self.inports[port] = None
self.sub = self.func = None
def outport(self, name):
name = fix(name)
if name not in self.outports:
self.outports[name] = OutPort(self, name)
return self.outports[name].ref()
def inport(self, name, value):
self.inports[fix(name)] = value
class OutPort(object):
def __init__(self, srcnode, srcport):
self.srcnode, self.srcport = srcnode, srcport
self.refs = 0
def ref(self):
self.refs += 1
return self
def code(self):
if self.srcnode.cls == 'QCIteratorVariables':
return 'this.%s' % self.srcport
return 'this.nodes.%s.outs.%s' % (self.srcnode.name, self.srcport)
class QCPatch(Node):
def __init__(self, patch):
Node.__init__(self)
if 'key' in patch:
if patch['class'] == 'QCPatch':
self.name = patch['key']
else:
self.name = '__patch__' + patch['key']
self.name += str(id(self))
else:
self.name = 'rootPatch'
self.name = genName(self.name)
self.nodes = {}
deps = {}
state = patch['state']
for elem in state['nodes']:
if elem['class'] == 'QCPatch':
self.nodes[genName(elem['key'])] = QCPatch(elem)
deps[genName(elem['key'])] = []
continue
try:
node = globals()[elem['class']]()
except:
print 'No class', elem['class']
sys.exit()
node.name = genName(elem['key'])
if 'identifier' in elem:
node.format = elem['identifier']
estate = elem['state']
_statemap = dict(
fragmentShader='FragmentShader',
vertexShader='VertexShader',
expression='Expression',
outputCount='OutputCount',
inputCount='InputCount',
portClass='PortClass',
resetOutputs='ResetOutputs',
numberOfLights='NumberOfLights',
slices='Slices',
stacks='Stacks',
operationCount='OperationCount',
fillBackground='FillBackground',
)
if 'customInputPortStates' in estate:
for k, v in estate['customInputPortStates'].items():
if 'value' in v:
node.inport(k, v['value'])
if 'ivarInputPortStates' in estate:
for k, v in estate['ivarInputPortStates'].items():
node.inport(k, v['value'])
for k, v in _statemap.items():
if k in estate:
node.inport(v, estate[k])
if 'systemInputPortStates' in estate and '_enable' in estate['systemInputPortStates']:
node.inport('_enable', estate['systemInputPortStates']['_enable']['value'])
if 'nodes' in estate:
node.sub = QCPatch(elem)
if 'script' in estate:
node.func = rewriteJS(estate['script'])
if 'imagePath' in estate:
ext = estate['imagePath'].rsplit('.', 1)[1]
data = file(os.path.expanduser(estate['imagePath']), 'rb').read()
node.inport('ImageData', 'data:image/%s;base64,%s' % (ext, base64.encodestring(data).replace('\n', '')))
self.nodes[node.name] = node
deps[node.name] = []
if 'connections' in state:
for v in state['connections'].values():
source = genName(v['sourceNode']), v['sourcePort']
dest = genName(v['destinationNode']), v['destinationPort']
sport = self.nodes[source[0]].outport(source[1])
self.nodes[dest[0]].inport(dest[1], sport)
if source[0] not in deps[dest[0]]:
deps[dest[0]].append(source[0])
# XXX: We should preserve node order in the plist but still handle deps.
self.order = []
for name, v in deps.items():
if self.nodes[name].cls == 'QCClear':
del deps[name]
self.order.append(name)
while len(deps):
for name, v in deps.items():
for dep in v:
if dep not in deps:
v.remove(dep)
if not len(v):
self.order.append(name)
del deps[name]
def code(self):
code = '%s = new QCPatch();\n' % self.name
for name in self.order:
node = self.nodes[name]
if node.cls == 'QCIteratorVariables':
continue
args = {}
if node.format:
args['_format'] = node.format
for pname, value in node.inports.items():
if not isinstance(value, OutPort):
args[pname] = value
if node.sub:
code += node.sub.code()
if node.cls == 'QCPatch':
code += '%s.nodes.%s = %s' % (self.name, node.name, node.code())
else:
if node.cls == 'QCGLSLShader':
ports = []
types = []
for key in node.inports.keys():
if key[0] != '_' and key not in ('VertexShader','FragmentShader'):
if key.endswith('_X') or key.endswith('_Y') or key.endswith('_Z') or key.endswith('_W'):
key = key[:-2]
if key in ports:
continue
if key + '_W' in node.inports:
type = 'vec4'
elif key + '_Z' in node.inports:
type = 'vec3'
else:
type = 'vec2'
ports.append(key)
types.append(type)
else:
ports.append(key)
types.append('scalar')
sargs = ', [%s], [%s]' % (', '.join(map(repr, ports)), ', '.join(map(repr, types)))
else:
sargs = ''
code += '%s.nodes.%s = new %s(%s%s%s%s);\n' % (self.name, name, node.cls, (node.func + ', ') if node.func else '', (node.sub.name + ', ') if node.sub else '', json.dumps(args), sargs)
code += '%s.update(function() {\n' % self.name
for name in self.order:
node = self.nodes[name]
name = node.name
if node.cls == 'QCIteratorVariables':
continue
for pname, value in node.inports.items():
if isinstance(value, OutPort):
code += '\tthis.nodes.%s.params.%s = %s;\n' % (name, pname, value.code())
code += '\tthis.nodes.%s.update();\n' % name
code += '});\n'
return code
classes = yaml.load(file('classes.yaml').read())
for name, elem in classes.items():
body = dict()
if 'in' in elem:
body['_inports'] = elem['in']
if 'out' in elem:
body['_outports'] = elem['out']
globals()['QC' + name] = type('QC' + name, (Node, ), body)
def main(fn, audio='none', debug=False):
global packing
data = readPlist(fn)
try:
del data['templateImageData']
except:
pass
pruneUserInfo(data)
if debug == 'verbose':
print '<pre>'
dump(data)
print '</pre>'
if not debug:
packing = True
root = QCPatch(data['rootPatch'])
print '<style>body { margin: 0; overflow: hidden; }</style>'
if audio != 'none':
print '<audio id="track"',
if debug:
print 'controls',
print 'preload="auto" autobuffer>'
print '<source src="' + audio + '.mp3">'
print '<source src="' + audio + '.ogg">'
print '</audio>'
if debug:
print '<div id="time"></div>'
elif audio == 'none':
print '<span>'
print '<script>'
code = root.code()
deps = ''
for depcls in jsdeps:
if depcls in builtins:
continue
try:
dep = file('js/' + depcls + '.js').read()
if debug and depcls not in basedeps:
print >>sys.stderr, 'Patch used:', depcls
except:
print >>sys.stderr, 'Unsupported patch class:', depcls
else:
deps += processJS(dep)
deps += 'init();'
code = deps + code
if debug:
print code
else:
print jsmin(code)
print 'run(' + root.name + ', document.getElementById("track"), document.getElementById("time"));'
print '</script>'
if __name__=='__main__':
sys.exit(main(*sys.argv[1:]))