-
Notifications
You must be signed in to change notification settings - Fork 4
/
get-pack.py
497 lines (372 loc) · 14.4 KB
/
get-pack.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
MIT = """
MIT License
Copyright (c) 2022 bitrate16
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
EULA = """
End User License Agreement (EULA)
You have the right to distribute and use this software in accordance with the
attached license and agree that you are solely responsible for the use of this
software and developer is not responsible for the use of this software, possible
damage to you, your hardware or software or data, and also to shadertoy.com
service.
This agreement prohibits the use of this software for the purpose of
intentionally damaging the service shadertoy.com in any way or knowingly
circumventing the current license agreements or copyrights on the source codes
of shaders and related software provided by the service and its users, as well
as images, audio, video and other media files and data that can be used as input
to shaders.
This software does not give you the right to use or distribute the source codes
of user shaders without agreeing to the license that is applied by default for
each shader (https://www.shadertoy.com/terms) and other licenses specified by
users in the description of the shader or its source code.
This software does not give you the right to use the downloaded images, audio,
video and other media and data without agreeing to the rules and license for
distribution and use (https://www.shadertoy.com/terms) of the shadertoy.com
service.
This software is intended for personal, non-commercial use, subject to the terms
described.
If you agree with the terms of this agreement, run the program with:
--eula=true
"""
# This utility downloads shader from shadertoy and saves it as single pack in given folder
import os
import json
import string
import argparse
import requests
import traceback
import urllib.parse
import urllib.request
try:
from PIL import Image
except:
print("Install PIL with 'pip install Pillow'")
exit(0)
SHADER_HEADER = """#version 330 core
uniform vec3 iResolution;
uniform float iTime;
uniform float iTimeDelta;
uniform int iFrame;
uniform float iChannelTime[4];
uniform vec3 iChannelResolution[4];
uniform vec4 iMouse;
uniform sampler2D iChannel0;
uniform sampler2D iChannel1;
uniform sampler2D iChannel2;
uniform sampler2D iChannel3;
uniform vec4 iDate;
uniform float iSampleRate;
out vec4 out_FragColor;"""
SHADER_MAIN = """void main(){vec4 color=vec4(0.0,0.,0.,1.);mainImage(color,gl_FragCoord.xy);color.rgb=clamp(color.rgb,0.,1.);color.w=1.0;out_FragColor=color;}"""
DEBUG = True
def get_shader_info(shader_id):
"""
Requests JSON data by shader ID
"""
try:
# GET
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
}
response = requests.get('https://www.shadertoy.com/shadertoy', headers=headers)
# POST
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Referer': f'https://www.shadertoy.com/view/{shader_id}',
'Content-Type': 'application/x-www-form-urlencoded',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Origin': 'https://www.shadertoy.com',
'DNT': '1',
'Connection': 'keep-alive',
}
data = {
's': '{ "shaders" : ["' + shader_id + '"] }',
'nt': '1',
'nl': '1',
'np': '1'
}
response = requests.post('https://www.shadertoy.com/shadertoy', headers=headers, cookies=response.cookies, data=data)
if response.status_code != 200 or len(response.content) == 0 or len(response.json()) == 0:
if DEBUG:
print(f'Invalid shader id "{shader_id}"')
return None
return response.json()
except:
if DEBUG:
traceback.print_exc()
return None
def parse_shader_id(url):
"""
Parse shader id from url similar to https://www.shadertoy.com/view/BAOBAB.
Additinaly performs weak check of shader url.
"""
try:
parsed = urllib.parse.urlparse(url)
if not parsed.path.startswith('/view/'):
return None
return parsed.path[6:]
except:
if DEBUG:
traceback.print_exc()
return None
def get_obj_with_attr(list, name, value):
for obj in list:
if obj[name] == value:
return obj
return None
# Arguments
parser = argparse.ArgumentParser(description='Download shadertoy.com shader')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--url', dest='url', default=None, help='url of the shader, example https://www.shadertoy.com/view/AMSyft')
group.add_argument('--id', dest='id', default=None, help='id of the shader, example AMSyft')
group.add_argument('--clipboard-url', default=False, dest='clipboard_url', action='store_true', help='get shader url from clipboard, requires pyperclip module')
group.add_argument('--clipboard-id', default=False, dest='clipboard_id', action='store_true', help='get shader id from clipboard, requires pyperclip module')
parser.add_argument('--output', dest='output', default=None, help='output folder and pack name, default is shader name')
parser.add_argument('--outside', dest='outside', action='store_true', default=None, help='put pack.json outside of the folder')
parser.add_argument('--eula', dest='eula', type=str, default=None, help='End User License Agreement')
parser.add_argument('--license', dest='license', action='store_true', default=None, help='License Agreement')
args = parser.parse_args()
# Check EULA
if args.eula != 'true' and args.eula != 'yes':
print(EULA)
exit(0)
if args.license:
print(MIT)
exit(0)
if args.url:
shader_url = args.url.strip()
shader_id = parse_shader_id(shader_url)
if args.id:
shader_url = 'https://www.shadertoy.com/view/' + args.id.strip()
shader_id = args.id.strip()
if args.clipboard_url:
try:
import pyperclip
except:
print("Install pyperclip module with 'pip install pyperclip'")
exit(0)
shader_url = pyperclip.paste().strip()
shader_id = parse_shader_id(shader_url)
if args.clipboard_id:
try:
import pyperclip
except:
print("Install pyperclip module with 'pip install pyperclip'")
exit(0)
shader_url = 'https://www.shadertoy.com/view/' + pyperclip.paste().strip()
shader_id = pyperclip.paste().strip()
# Output folder
if args.output:
output = args.output.strip()
else:
output = None # Get from response
if not shader_id:
exit(0)
if DEBUG:
print('Shader id:', shader_id)
shader_info = get_shader_info(shader_id)
if not shader_info:
exit(0)
# Output json
pack_json = {}
# Shader info
shader = shader_info[0]
# Get shader name and escape everything except ascii path safe
if not output:
path_safe = string.digits + string.ascii_letters + '_-.'
output = ''.join([ c for c in shader['info']['name'] if c in path_safe ])
if not output:
output = shader_id
os.makedirs(output, exist_ok=True)
# Save description data
pack_json['Author'] = shader['info']['username']
pack_json['Name'] = shader['info']['name']
pack_json['Id'] = shader['info']['id']
pack_json['Source'] = 'https://www.shadertoy.com/view/' + shader['info']['id']
# Common and other objects
common = get_obj_with_attr(shader['renderpass'], 'name', 'Common')
main = get_obj_with_attr(shader['renderpass'], 'name', 'Image') or get_obj_with_attr(shader['renderpass'], 'name', '')
buffera = get_obj_with_attr(shader['renderpass'], 'name', 'Buffer A') or get_obj_with_attr(shader['renderpass'], 'name', 'Buf A')
bufferb = get_obj_with_attr(shader['renderpass'], 'name', 'Buffer B') or get_obj_with_attr(shader['renderpass'], 'name', 'Buf B')
bufferc = get_obj_with_attr(shader['renderpass'], 'name', 'Buffer C') or get_obj_with_attr(shader['renderpass'], 'name', 'Buf C')
bufferd = get_obj_with_attr(shader['renderpass'], 'name', 'Buffer D') or get_obj_with_attr(shader['renderpass'], 'name', 'Buf D')
buffers = [ buffera, bufferb, bufferc, bufferd ]
# Common source code
common_source = '' if common is None else common['code']
# Write Main shader properties
if main:
main_source = '' if main is None else main['code']
# Write source of Main.glsl
with open(f'{output}/Main.glsl', 'w') as f:
if DEBUG:
print(f'Main shader path: {output}/Main.glsl')
f.write(SHADER_HEADER)
f.write('\n')
f.write(common_source)
f.write('\n')
f.write(main_source)
f.write('\n')
f.write(SHADER_MAIN)
# Collect inputs
inputs = [ None ] * 4
for i in range(4):
input_info = get_obj_with_attr(main['inputs'], 'channel', i)
if not input_info:
if DEBUG:
print('Main Input', i, 'is None')
continue
inputs[i] = {}
# Detect buffer type
if input_info['type'] == 'buffer':
if input_info['filepath'].find('buffer00') != -1:
inputs[i]['type'] = 'BufferA'
if input_info['filepath'].find('buffer01') != -1:
inputs[i]['type'] = 'BufferB'
if input_info['filepath'].find('buffer02') != -1:
inputs[i]['type'] = 'BufferC'
if input_info['filepath'].find('buffer03') != -1:
inputs[i]['type'] = 'BufferD'
if DEBUG:
print('Main Input', i, 'type:', inputs[i]['type'])
# Get image from local storage
elif input_info['type'] == 'texture':
inputs[i]['type'] = 'Image'
inputs[i]['path'] = input_info['filepath'][1:]
# Download texture
media_directory = inputs[i]['path']
media_directory = media_directory[:media_directory.rfind('/')]
os.makedirs(f'{output}/{media_directory}', exist_ok=True)
if DEBUG:
print('Main Input', i, 'type:', inputs[i]['type'])
print('Downloading', f'https://www.shadertoy.com/{inputs[i]["path"]}')
urllib.request.urlretrieve(f'https://www.shadertoy.com/{inputs[i]["path"]}', f'{output}/{inputs[i]["path"]}')
# Convert to png
if not inputs[i]['path'].endswith('.png'):
in_path = f'{output}/{inputs[i]["path"]}'
inputs[i]["path"] = inputs[i]["path"][:inputs[i]["path"].rfind('.')] + '.png'
out_path = f'{output}/{inputs[i]["path"]}'
if DEBUG:
print('Converting', in_path, 'to PNG')
im1 = Image.open(in_path)
im1.save(out_path)
os.remove(in_path)
# Move up a directory
if args.outside:
inputs[i]['path'] = output + '/' + inputs[i]['path']
# TODO: Add other input types
else:
inputs[i] = None
if DEBUG:
print('Main shader Input', i, 'has unsupported type:', input_info['type'])
pack_json['Main'] = {
'inputs': inputs,
'path': f'{output}/Main.glsl' if args.outside else 'Main.glsl'
}
else:
if DEBUG:
print('Missing Main shader')
# Write buffer shaders
for buffer_name, buffer in zip([ 'BufferA', 'BufferB', 'BufferC', 'BufferD' ], buffers):
if buffer:
buffer_source = '' if buffer is None else buffer['code']
# Write source of Main.glsl
with open(f'{output}/{buffer_name}.glsl', 'w') as f:
if DEBUG:
print(f'{buffer_name} shader path: {output}/{buffer_name}.glsl')
f.write(SHADER_HEADER)
f.write('\n')
f.write(common_source)
f.write('\n')
f.write(buffer_source)
f.write('\n')
f.write(SHADER_MAIN)
# Collect inputs
inputs = [ None ] * 4
for i in range(4):
input_info = get_obj_with_attr(buffer['inputs'], 'channel', i)
if not input_info:
if DEBUG:
print(buffer_name, 'Input', i, 'is None')
continue
inputs[i] = {}
# Detect buffer type
if input_info['type'] == 'buffer':
if input_info['filepath'].find('buffer00') != -1:
inputs[i]['type'] = 'BufferA'
if input_info['filepath'].find('buffer01') != -1:
inputs[i]['type'] = 'BufferB'
if input_info['filepath'].find('buffer02') != -1:
inputs[i]['type'] = 'BufferC'
if input_info['filepath'].find('buffer03') != -1:
inputs[i]['type'] = 'BufferD'
if DEBUG:
print(buffer_name, 'Input', i, 'type:', inputs[i]['type'])
# Get image from local storage
elif input_info['type'] == 'texture':
inputs[i]['type'] = 'Image'
inputs[i]['path'] = input_info['filepath'][1:]
# Download texture
media_directory = inputs[i]['path']
media_directory = media_directory[:media_directory.rfind('/')]
os.makedirs(f'{output}/{media_directory}', exist_ok=True)
if DEBUG:
print(buffer_name, 'Input', i, 'type:', inputs[i]['type'])
print('Downloading', f'https://www.shadertoy.com/{inputs[i]["path"]}')
urllib.request.urlretrieve(f'https://www.shadertoy.com/{inputs[i]["path"]}', f'{output}/{inputs[i]["path"]}')
# Convert to png
if not inputs[i]['path'].endswith('.png'):
in_path = f'{output}/{inputs[i]["path"]}'
inputs[i]["path"] = inputs[i]["path"][:inputs[i]["path"].rfind('.')] + '.png'
out_path = f'{output}/{inputs[i]["path"]}'
if DEBUG:
print('Converting', in_path, 'to PNG')
im1 = Image.open(in_path)
im1.save(out_path)
os.remove(in_path)
# Move up a directory
if args.outside:
inputs[i]['path'] = output + '/' + inputs[i]['path']
# TODO: Add other input types
else:
inputs[i] = None
if DEBUG:
print('Main shader Input', i, 'has unsupported type:', input_info['type'])
pack_json[buffer_name] = {
'inputs': inputs,
'path': f'{output}/{buffer_name}.glsl' if args.outside else f'{buffer_name}.glsl'
}
else:
if DEBUG:
print(f'Missing {buffer_name} shader')
# Write pack
if args.outside:
with open(f'{output}.json', 'w') as f:
f.write(json.dumps(pack_json, indent=4, sort_keys=True))
else:
with open(f'{output}/{output}.json', 'w') as f:
f.write(json.dumps(pack_json, indent=4, sort_keys=True))